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 の 4 つのクラスが含まれています。これらのクラスを使用すると、TCP/IP 機能を VB.NET アプリケーションに即座に追加できるだけでなく、私たちが探している機能のほとんどを備えています。これらのクラスを使用すると、同じポートで複数のクライアントをサーバーに接続できます。クライアントへの帯域幅を調整し、1 つの接続で同時に 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 に答える