0
正在運行在我的代碼兩個TCP服務器(一個在50010和其他在10250)無論是在單獨的線程傳遞數據/參數使用VB .NET來套接字線程
Private Sub frmMainScreen_Load(ByVal eventSender As Object, ByVal eventArgs As EventArgs) Handles MyBase.Load
Dim Local_PortNum As String = Read_GlobalINI_File("TCP-IP CONFIGURATION", "LOCAL_PORT", "TCPIP_Config")
Read_FileLocation()
Dim s As New ClsAutomation_TcpClient
Dim t As New ClsAutomation_TcpClient
trd = New Thread(Sub() s.Main()) ' listens 55000 port
trd1 = New Thread(Sub() t.Main())' listens 10250 port
trd.IsBackground = True
trd.Start()
trd1.IsBackground = True
trd1.Start()
End Sub
,這裏是我的tcpserver類
Public Class ClsAutomation_TcpClient
Dim clientsList As New Hashtable
Public WithEvents status_bar1 As System.Windows.Forms.Label
Sub Main()
Dim serverSocket As New TcpListener(IPAddress.Any, 10250)
Dim clientSocket As TcpClient
Dim counter As Integer
Dim clientIPAddress As String
serverSocket.Start()
MsgBox("Chat Server Started ....")
counter = 0
While (True)
counter += 1
clientSocket = serverSocket.AcceptTcpClient()
Dim bytesFrom(10024) As Byte
Dim dataFromClient As String
Dim networkStream As NetworkStream = clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
Dim ipend As Net.IPEndPoint = clientSocket.Client.RemoteEndPoint
clientIPAddress = ipend.Address.ToString()
clientsList(clientIPAddress) = clientSocket
broadcast(clientIPAddress + " Joined ", dataFromClient, False)
' MsgBox(dataFromClient + " Joined chat room ")
Dim client As New handleClinet
client.startClient(clientSocket, dataFromClient, clientsList)
End While
clientSocket.Close()
serverSocket.Stop()
MsgBox("exit")
End Sub
Sub msg(ByVal mesg As String)
mesg.Trim()
MsgBox(" >> " + mesg)
End Sub
Private Sub broadcast(ByVal msg As String, ByVal uName As String, ByVal flag As Boolean)
Dim Item As DictionaryEntry
For Each Item In clientsList
Dim broadcastSocket As TcpClient
broadcastSocket = CType(Item.Value, TcpClient)
Dim broadcastStream As NetworkStream = broadcastSocket.GetStream()
Dim broadcastBytes As [Byte]()
If flag = True Then
broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg)
Else
broadcastBytes = Encoding.ASCII.GetBytes(msg)
End If
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
broadcastStream.Flush()
Next
End Sub
End Class
Public Class handleClinet
Public WithEvents status_bar1 As System.Windows.Forms.Label
Dim clientSocket As TcpClient
Dim clNo As String
Dim clientsList As Hashtable
Public Sub startClient(ByVal inClientSocket As TcpClient, ByVal clineNo As String, ByVal cList As Hashtable)
Me.clientSocket = inClientSocket
Me.clNo = clineNo
Me.clientsList = cList
Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
ctThread.Start()
End Sub
Private Sub broadcast(ByVal msg As String, ByVal uName As String, ByVal flag As Boolean)
'Dim Item As DictionaryEntry
'For Each Item In clientsList
Dim broadcastSocket As TcpClient
'broadcastSocket = CType(Item.Value, TcpClient)
broadcastSocket = Me.clientSocket
Dim broadcastStream As NetworkStream = broadcastSocket.GetStream()
Dim broadcastBytes As [Byte]()
If flag = True Then
broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg)
Else
broadcastBytes = Encoding.ASCII.GetBytes(msg)
End If
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
broadcastStream.Flush()
'Next
End Sub
Private Sub doChat()
'Dim infiniteCounter As Integer
Dim requestCount As Integer
Dim bytesFrom(10024) As Byte
Dim dataFromClient As String
Dim sendBytes As [Byte]()
Dim serverResponse As String
Dim rCount As String
requestCount = 0
While (True)
Try
requestCount = requestCount + 1
Dim networkStream As NetworkStream = clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = _
dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
'MsgBox("From client - " + clNo + " : " + dataFromClient)
rCount = Convert.ToString(requestCount)
broadcast(dataFromClient, clNo, True)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While
End Sub
End Class
這裏我需要做的是從btn_click將數據發送到(TRD)55000例如線程... 如何將我做