2013-05-28 61 views
1

我開始學習TCP /套接字昨天決定做一個客艙的朋友和我多線程和插座/ TCP [VB.NET]

不幸的是,我有一些多線程困難。

每當我使用它,我不能再收到我的朋友的消息。

但是,如果我禁用它,那麼,一切正常。

我不知道這裏發生了什麼,有人可以幫忙嗎?

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 

回答

2

此示例項目包含四個類 - TcpCommServer,TcpCommClient,clsAsyncUnbuffWriter和CpuMonitor。通過這些類,您不僅能夠立即爲您的VB.NET應用程序添加TCP/IP功能,而且還可以獲得大部分我們都在尋找的功能。使用這些類,您將能夠將多個客戶端連接到同一端口上的服務器。您將能夠輕鬆地:爲客戶端節制帶寬,並在單個連接上同時沿着250個提供的通道發送和接收文件和數據(文本?)。

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

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