在单独的线程上运行的基本tcp / ip(以防止冻结)

时间:2015-08-12 08:27:50

标签: .net vb.net multithreading tcp

我想创建一个基本的tcp / ip服务器,只要它在端口7700上就可以侦听所有IP,根据我的研究我需要的是在另一个线程上运行它以防止我的系统被冻结我合并了MSDN's basic TCP/IP listenerCodeGuru's basic thread tutorial中的2个示例。

对于UI,我只有一个按钮和一个richtextbox。我打算使用此按钮开始监听端口“7700”,然后每当我收到来自客户端的消息时,我希望它附加在RichTextBox1上

我的问题是,每当我开始聆听时,我的系统仍会冻结。

我有这段代码:

Imports System.Net.Sockets
Imports System.Net
Imports System.Threading 'Imports Threading Namespace

Public Class Form1

    ' Set the TcpListener on port 13000. 
    Dim port As Int32 = 7700
    Dim localAddr As IPAddress = IPAddress.Any
    ' Buffer for reading data 
    Dim bytes(1024) As Byte
    Dim data As String = Nothing
    Dim server As TcpListener

    'What Thread 1 Must Do
    Private Sub ThreadProcedure()
        While True
            Console.Write("Waiting for a connection... ")
            ' Perform a blocking call to accept requests. 
            ' You could also user server.AcceptSocket() here. 
            Dim client As TcpClient = server.AcceptTcpClient()
            Console.WriteLine("Connected!")
            data = Nothing
            ' Get a stream object for reading and writing 
            Dim stream As NetworkStream = client.GetStream()
            Dim i As Int32
            ' Loop to receive all the data sent by the client.
            i = stream.Read(bytes, 0, bytes.Length)
            While (i <> 0)
                ' Translate data bytes to a ASCII string.
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                Console.WriteLine("Received: {0}", data)
                ' Process the data sent by the client.
                data = data.ToUpper()
                Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
                ' Send back a response.
                stream.Write(msg, 0, msg.Length)
                Console.WriteLine("Sent: {0}", data)
                i = stream.Read(bytes, 0, bytes.Length)
            End While
            ' Shutdown and end connection
            'client.Close()
        End While
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        server = Nothing
        Try
            server = New TcpListener(localAddr, port)
            ' Start listening for client requests.
            server.Start()
            ' Enter the listening loop. 
            ThreadProcedure()
        Catch haha As SocketException
            Console.WriteLine("SocketException: {0}", haha)
        Finally
            'server.Stop()
        End Try
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Create Thread, and Specify Delegate
        Dim tThread1 As New Thread(AddressOf ThreadProcedure)
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

您已在Form_Load中创建了一个Thread但未启动它,然后在按钮单击中直接调用该方法。

从表单加载中删除代码并将其放入按钮单击:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        server = Nothing
        Try
            server = New TcpListener(localAddr, port)
            ' Start listening for client requests.
            server.Start()
            ' Enter the listening loop. 
            Dim tThread1 As New Thread(AddressOf ThreadProcedure)
            tThread1.Start()
        Catch haha As SocketException
            Console.WriteLine("SocketException: {0}", haha)
        Finally
            'server.Stop()
        End Try
    End Sub