设置TCP客户端以接收广播数据

时间:2017-01-31 16:43:24

标签: vb.net tcp

我正在VB.net中编写一个连接到服务器的程序,向服务器发送一个字节数组,然后接收一个字节数组作为答案,然后需要从服务器接收连续的比特流。

发送位阵列是84字节,在第一次从服务器接收时应该是相同的,并且每次服务器在此之后将其发送回来时大小会有所不同。

消息的开头和结尾总是相同的,但发送的位数组的大小会有所不同。

我的问题是我可以将这些位写入服务器,但我无法接收任何广播的后续位。

任何建议

Public Sub connect(TcpClient As TcpClient)
    Dim NetworkStream As NetworkStream = TcpClient.GetStream()
    If networkStream.CanWrite And networkStream.CanRead Then
        ' Do a simple write.
        Dim sendBytes As [Byte]() = IO.File.ReadAllBytes(startupFile)
        networkStream.Write(sendBytes, 0, sendBytes.Length)

        ' Read the NetworkStream into a byte buffer.
        Dim bytes(1024) As Byte
        NetworkStream.Read(bytes, 0, bytes.Length)

        ' Output the data received from the host to the console.
        Dim returndata As String = Encoding.ASCII.GetString(bytes)

        Dim fileWrite As New IO.StreamWriter(writeFile)
        fileWrite.Write(returndata)
        fileWrite.Close()

    Else
        If Not networkStream.CanRead Then
            Console.WriteLine("cannot not write data to this stream")
            TcpClient.Close()
        Else
            If Not networkStream.CanWrite Then
                Console.WriteLine("cannot read data from this stream")
                TcpClient.Close()
            End If
        End If
    End If

End Sub

0 个答案:

没有答案