none
Tcpclient.GetStream 如何針對某一個client .send 資料 RRS feed

  • 問題

  • 我使用一個server端-> tcplisten

    2個client 端 ->tcpclient 

    同時連線後

    server讀取client所send過來的資料時沒問題

    但是server端在send 資料給client時 卻只有最後一個連接上server端的 client 會接收到資料

    請問我該如何指定某一個client送資料呢?   

    -----------------------Public sub 讀資料--------------

    Dim Server As TcpClient
     Dim Stream As NetworkStream

         Try
                Server = session.theClient
                atClient = Server
                Stream = Server.GetStream()
          Catch ex As Exception
                           Exit Sub
          End Try

          Try
                Do
                    If Stream.CanRead Then
                        Dim myReadBuffer(1024) As Byte
                        Dim myCompleteMessage As StringBuilder = New StringBuilder()
                        Dim numberOfBytesRead As Integer = 0                 

                        numberOfBytesRead = Stream.Read(myReadBuffer, 0, myReadBuffer.Length)
                        myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0,  numberOfBytesRead))
                        If numberOfBytesRead > 0 Then
                            dataChannel = 1
                        End If
                        '  Dim aaa As String = myCompleteMessage.ToString
                        ' SystemMessage(myCompleteMessage.ToString)
                        RcvBytes(myReadBuffer, session.sessionID, dataChannel)

                    End If
                Loop 

            Catch ex As Exception
                Debug.WriteLine("ReadErr: " & ex.Message)
            End Try

    End Sub

    ---------------------------------------------

    ---------------------------------------

    Public sub 寫資料
            Try

                Server = AAclient
                Stream = Server.GetStream()
            Catch ex As Exception
                           Exit Sub
            End Try
            Try
                If Stream.CanWrite Then
                    'TextBox.CheckForIllegalCrossThreadCalls = False
                    Dim myWriteBuffer As Byte() = data
                    Stream.Write(myWriteBuffer, 0, myWriteBuffer.Length)
                Else
                End If

            Catch ex As Exception
            End Try

    End Sub

    -----------------------------------------------

    2012年9月6日 上午 04:27

解答

所有回覆