マックの趣味

UDP サーバープログラムon VB.NET Form

IImports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading

Public Class Form1

    Private m_Listener As Socket
    Private m_Worker As Socket
    Private m_buffer(1023) As Byte
    Private m_NewForm As Form
    Private recieved As Integer
    Private connection As Integer = True
    Private prevData As Integer
    Private nowData As Integer
    Private match As Integer = 0
    Private unmatch As Integer = 0
    Private keep As Integer = False
    Private starting As Integer = True


    Declare Sub ExitProcess Lib "Kernel32.dll" (ByVal uExitCode As Integer)


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'CountUpメソッドを別スレッドで処理する
        'ThreadStart()
        TextBox1.ReadOnly = True
        TextBox2.ReadOnly = True
        TextBox3.ReadOnly = False
        TextBox4.ReadOnly = False
        TextBox5.ReadOnly = True
    End Sub
    '別処理をするためのスレッド
    Private thread As System.Threading.Thread

    Public Sub ThreadStart()
        'CountUpメソッドを別スレッドで処理する
        connection = True
        thread = New System.Threading.Thread( _
        New System.Threading.ThreadStart(AddressOf CountUp))
        thread.IsBackground = True
        TextBox1.Text = "Starting"
        starting = True
        Me.TextBox2.Text = ""
        thread.Start()
        '       If connection = False Then
        'thread.Abort()
        'End If
    End Sub

    Private Sub CountUp()
        Dim millisecondsTimeout As Integer = 100
        Control.CheckForIllegalCrossThreadCalls = False
        connection = True
        '待機するIP(自アドレス)を指定
        Dim host As String = Me.TextBox3.Text
        ' Dim ipAdd As System.Net.IPAddress = System.Net.Dns.GetHostEntry(host).AddressList(0)

        Dim portNum As Integer

        '待機するポート番号を指定
        portNum = Val(Me.TextBox4.Text)

            Dim listener As New UdpClient(portNum)
            Dim groupEP As New IPEndPoint(IPAddress.Any, portNum)
            Me.TextBox1.Text = "Connection Waiting"


        While True
            While keep
                'Me.TextBox1.Text = keep
                thread.Sleep(millisecondsTimeout)
            End While

            Dim bytes As Byte() = listener.Receive(groupEP)
            Me.TextBox1.Text = "Connection accepted."
            '----送信部分----
            ' Dim word As Byte() = System.Text.Encoding.Default.GetBytes("Hello")
            'ns.Write(word, 0, word.Length)
            '            '       MessageBox.Show("Sending")
            '              Me.TextBox2.Text = " Sending"
            '----受信部分----
            Dim getWord(1024) As Byte
            Me.TextBox2.Text = groupEP.ToString()
            'Me.TextBox5.Text = Encoding.ASCII.GetString(bytes, 0, bytes.Length)
            TextBox5.AppendText(Encoding.ASCII.GetString(bytes, 0, bytes.Length))
            'TextBox5.AppendText(",")
        End While
        'listener.Close()
        'Me.TextBox1.Text = " Connection closed."
        'connection = False
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ExitProcess(0)
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.Button1.Text = "開始" Then
            TextBox3.ReadOnly = True
            TextBox4.ReadOnly = True
            Me.Button1.Text = "起動中"
            'CountUpメソッドを別スレッドで処理する
            ThreadStart()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.TextBox5.Text = ""
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If (Me.Button4.Text = "停止") Then
            keep = True
            Me.Button4.Text = "開始"
        Else
            keep = False
            Me.Button4.Text = "停止"
        End If

    End Sub
    Public Shared Sub Sleep(ByVal millisecondsTimeout As Integer)
    End Sub
End Class



ランキングに参加中、クリックをお願いします。

名前:
コメント:

※文字化け等の原因になりますので顔文字の投稿はお控えください。

コメント利用規約に同意の上コメント投稿を行ってください。

 

  • Xでシェアする
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

最新の画像もっと見る

最近の「FreeBSD」カテゴリーもっと見る

最近の記事
バックナンバー
人気記事