1

我昨天开始学习 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
4

2 回答 2

2

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

http://www.codeproject.com/Articles/307315/Reusable-multithreaded-tcp-client-and-server-class

于 2013-05-28T09:46:37.877 回答
0

好吧,我了解到 BackgroundWorkers 可以做完全相同的事情,现在一切正常。

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
于 2013-05-29T12:45:39.923 回答