VB.NET TCP服务器正确接收数据

时间:2014-12-29 12:41:21

标签: vb.net tcp server

我有一个多线程(使用Backgroundworker)TCP服务器,由VB.NET开发。当客户端和服务器在同一台机器上时,它工作得很好。但是,当我从同一局域网的另一台计算机连接到服务器时,它的工作方式不同。在这种情况下,它可以正常工作,直到我开始连续发送消息(每秒3-4条消息)。我发送: 你好 你好 你好 你好 喜

和服务器获取此消息:

您好 的HiHi 你好你好你好 你好 等

非常有趣的是,只有当客户端位于局域网的其他计算机上时才会出现此问题。

这是我的听力Sub:

Sub listen_port6(ByVal b As BackgroundWorker)
    Dim server As TcpListener
    server = Nothing
    Try

        Dim port As Int32 = 8085

        server = New TcpListener(IP, port)

        server.Start()

        Dim bytes(1024) As Byte
        Dim data As String = Nothing

        While True
            Dim client As Sockets.TcpClient = server.AcceptTcpClient()
            Dim ipend As Net.IPEndPoint = client.Client.RemoteEndPoint
            PublicIP = ""
            If Not ipend Is Nothing Then
                PublicIP = ipend.Address.ToString
            End If
            b.ReportProgress(1)
            Dim stream As NetworkStream = client.GetStream()

            Dim i As Int32
            Dim k As Short
            For k = 0 To 1024
                bytes(k) = 0
            Next
            i = stream.Read(bytes, 0, bytes.Length)

            While (i <> 0)
                data = ""
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                Debug.Print(data)
                i = stream.Read(bytes, 0, bytes.Length)
            End While
            client.Close()
        End While
    Catch errore As Exception
        Error_Print(errore.Message)
    Finally
        server.Stop()
    End Try
End Sub

提前致谢

0 个答案:

没有答案