0

私のコードでは、2 つの TCP サーバー (1 つは 50010 に、もう 1 つは 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 インスタンス スレッドにデータを送信する必要があります。

4

1 に答える 1

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)
于 2012-08-29T07:30:28.807 に答える