在.NET中丢失TCP数据包但在VB6中丢失

时间:2015-04-20 16:09:53

标签: vb.net tcp

我在VB.NET 2010中遇到了TCP客户端的问题,我正在尝试发送大约300KB的字符串,但另一方面只收到大约200KB的字符串。 远程计算机位于另一个建筑物中,应用程序在VB6中编译。 我的应用程序是在VB.NET 2010中编译的,如果我的两端都运行用VB.NET编译的应用程序或两者都运行在VB6中,它的工作正常 但如果发件人是VB.NET应用程序,则数据包将丢失。

我甚至尝试使用相同的结果添加15秒的延迟。

可能会发生什么?

Private Function SendReport(ByVal Msg As String, ByVal Host As String, ByVal Port As String) As String
        Try

            Using Tx As New TcpClient()
                Dim stream As NetworkStream = Nothing
                Dim CloseStream As Boolean = False
                Dim LingerMode As New LingerOption(True, 5)

                Dim MsgUTF8, MsgUnicode, MsgUTF32 As String

                'Msg = Msg & "*" & Chr(23)
                MsgUTF8 = Msg & "*" & Chr(23)
                '  MsgUTF32 = "UTF32 " & Msg & vbCrLf
                '  MsgUnicode = "Unicode" & Msg & vbCrLf
                ' String to store the response ASCII representation.
                Dim responseData As [String] = [String].Empty

                Tx.LingerState = LingerMode
                Tx.SendTimeout = 10000
                Tx.ReceiveTimeout = 10000
                Tx.NoDelay = True
                Tx.SendBufferSize = 512000

                Tx.Connect(Host, Port)

                ' Translate the passed message into ASCII and store it as a Byte array.
                ' Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(Msg)
                Dim dataUTF8 As [Byte]() = System.Text.Encoding.UTF8.GetBytes(MsgUTF8)
                ' Dim dataUnicode As [Byte]() = System.Text.Encoding.Unicode.GetBytes(MsgUTF32)
                ' Dim dataUTF32 As [Byte]() = System.Text.Encoding.UTF32.GetBytes(MsgUnicode)

                ' Get a client stream for reading and writing.
                '  Stream stream = client.GetStream();
                stream = Tx.GetStream()
                CloseStream = True
                'stream.WriteTimeout = 100
                ' Send the message to the connected TcpServer. 
                ' stream.Write(data, 0, data.Length)
                stream.Write(dataUTF8, 0, dataUTF8.Length)
                'stream.Write(dataUTF32, 0, dataUTF32.Length)
                ' stream.Write(dataUnicode, 0, dataUnicode.Length)

                '' Close everything.

                System.Threading.Thread.Sleep(15000)
                If CloseStream = True Then
                    stream.Close()
                End If

                Tx.Close()
                Return responseData
            End Using

        Catch ex As Exception
            WriteRTBLog(ex.Message, Color.Red)
            WriteRTBLog(ex.StackTrace, Color.DarkRed)
            Return "Error"

        End Try

End Function

0 个答案:

没有答案