多线程和套接字/TCP[VB.NET]

多线程和套接字/TCP[VB.NET],vb.net,multithreading,sockets,tcpclient,Vb.net,Multithreading,Sockets,Tcpclient,我昨天开始学习TCP/Sockets,并决定为我和一位朋友制作一个聊天室 不幸的是,我在使用多线程时遇到了一些困难 无论何时我使用它,我都无法再收到朋友的信息 但是,如果我禁用它,那么一切都会完美地工作 我不知道这里发生了什么,有人能帮忙吗 Imports System.Net.Sockets Imports System.Net Public Class ServerClient Dim _TCPServer As Socket Dim _TCPListener As Tc

我昨天开始学习TCP/Sockets,并决定为我和一位朋友制作一个聊天室

不幸的是,我在使用多线程时遇到了一些困难

无论何时我使用它,我都无法再收到朋友的信息

但是,如果我禁用它,那么一切都会完美地工作

我不知道这里发生了什么,有人能帮忙吗

Imports System.Net.Sockets
Imports System.Net

Public Class ServerClient

    Dim _TCPServer As Socket
    Dim _TCPListener As TcpListener

    Dim _ListenerThread As System.Threading.Thread

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

        'When Submit is pressed, send some text to the client

        Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(txtInput.Text)
        txtBox.AppendText(vbCrLf & "Server: " & txtInput.Text)
        txtInput.Clear()
        _TCPServer.Send(bytes)

    End Sub

    Private Sub TCPListen()

        'If somebody calls port 2424, accept it, unblock the socket and start the timer

        _TCPListener = New TcpListener(IPAddress.Any, 2424)
        _TCPListener.Start()
        _TCPServer = _TCPListener.AcceptSocket()
        btnSend.Enabled = True
        txtBox.AppendText("Connection Established" & vbCrLf)
        _TCPServer.Blocking = False
        _Timer.Enabled = True

    End Sub

    Private Sub _Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Timer.Tick

        'If data has been sent, receive it

        Try

            Dim rcvdbytes(_TCPServer.ReceiveBufferSize) As Byte
            _TCPServer.Receive(rcvdbytes)
            txtBox.AppendText(vbCrLf & "Client: " & System.Text.Encoding.ASCII.GetString(rcvdbytes) & vbCrLf)

        Catch ex As Exception
        End Try

    End Sub

    Private Sub ServerClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Link the new thread to TCPListen(), allow access to all threads and wait for a call

        _ListenerThread = New Threading.Thread(AddressOf TCPListen)
        Control.CheckForIllegalCrossThreadCalls = False

        txtBox.AppendText("Waiting for connection.." & vbCrLf)
        btnSend.Enabled = False
        _ListenerThread.Start()

    End Sub

End Class

此示例项目包含四个类-TcpCommServer、TcpCommClient、clsAsyncUnbuffWriter和CpuMonitor。有了这些类,您不仅可以立即将TCP/IP功能添加到VB.NET应用程序中,而且还可以获得我们所需要的大部分功能。使用这些类,您将能够将多个客户端连接到同一端口上的服务器。您将能够轻松地:限制客户端的带宽,并在单个连接上沿250个提供的通道同时发送和接收文件和数据(文本?)


嗯,我了解到后台工作人员可以做完全相同的事情,现在一切都正常了

Private Sub ServerClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Wait for a call

    BackgroundWorker1.RunWorkerAsync()

End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    TCPListen()

End Sub


Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

    _Timer.Enabled = True

End Sub