在我的代码中运行两个TCP服务器(一个在50010中,另一个在10250中)都在单独的线程中
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实例线程...... 我该怎么做
答案 0 :(得分:0)
你的问题很广泛,但简单来说就是:
您需要创建一个NetworkStream对象。
使用TcpClient.GetStream方法对此进行分配,然后调用NetworkStream.Write
像这样(未经测试的)代码:
Dim stream as NetworkStream
stream = clientSocket.GetStream
Dim buffer() as byte = System.Text.Encoding.ASCII.GetBytes("hello")
If stream.CanWrite Then stream.Write(buffer, 0, buffer.Length)