previous的数据保留在网络流 [英] Previous Data Remains on the Network Stream

查看:158
本文介绍了previous的数据保留在网络流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建设的一些日志记录功能的文件传输应用程序。它所做的是,每一个客户端连接或断开发送日志(字符串消息)到服务器的时间。该记录的部分工作正常,但是当我尝试发送的文件,程序弄乱了。

I'm currently building a file transfer application with some logging features. What it does is, every time a client connects or disconnects it sends logs (string message) to the server. The logging part is working fine but when I try to send files, the program messes up.

看来,这纯粹是一个服务器端的问题。什么情况是,previous数据;这是记录的字符串消息,从客户端发送似乎被卡住的网络数据流。当我尝试连接到服务器后发送一个文件,我得到一个错误,说的路径中的非法字符的。

It seems that this is purely a server side issue. What happens is that the previous data; which is the string message for logging, sent from the client seems get stuck on the network stream. When I try to send a file after connecting to the server, I get an error which says, illegal characters in path.

下面是错误的截图。

我认为,这是因为,你可以在上面的文件名变量的截图中看到,该字符串的一部分(连接),这是当连接在客户端被卡在网络上的流发送。 HELLO.CPP是正在发送的文件的名称

I believe that this happens because, as you can see on the screenshot above in the FileName variable, a part of the string ("is connected.") which was sent when the client connected was stuck on the network stream. hello.cpp is the name of the file being sent.

这里的code。

Dim ClientSocket As TcpClient = CType(tcpSocket, TcpClient)

Dim networkStream As NetworkStream = ClientSocket.GetStream() 'This stream is
'for the logging part. This part here, I think causes the error because when I
'remove this and the conditions for the logging part, leaving the file sharing
'algorithm alone, the whole program works.

While FileSharingStarted

    If CBool(ClientSocket.Available) Then
        Dim ByteData(ClientSocket.ReceiveBufferSize) As Byte
        networkStream.Read(ByteData, 0, CInt(ClientSocket.ReceiveBufferSize))
        fileLogMessage = Encoding.ASCII.GetString(ByteData)

        If fileLogMessage.Contains("is connected." & Environment.NewLine) Then

            'This block here is for logging purposes. It receives the string 
            'message sent by the client when it connects and does some stuffs.

        ElseIf fileLogMessage.Contains("is disconnected." & Environment.NewLine) Then

            'This block here is for logging purposes again. It receives the 
            'string message sent by the client when it disconnects and then 
            'does some stuffs.

        Else

            'This part is for receiving the file sent by the client.

            Dim FileName, FilePath As String
            Dim FileLength As Long
            Dim binaryReader As New BinaryReader(ClientSocket.GetStream())

            FileName = binaryReader.ReadString()
            FileLength = binaryReader.ReadInt64()
            FilePath = Path.Combine(System.Environment.CurrentDirectory & "\home", FileName)

            Dim FileData(8092) As Byte
            Dim TotalData As Long = 0
            Dim ReadBytes As Integer = -1

            Using FileStream As New FileStream(FilePath, FileMode.Create, FileAccess.Write)
                FileSharingStatusBar.Panels.Item(1).Text = "Receiving file . . ."

                Do Until TotalData = FileLength
                    ReadBytes = ClientSocket.GetStream.Read(FileData, 0, FileData.Length())
                    FileStream.Write(FileData, 0, ReadBytes)
                    TotalData += ReadBytes
                Loop
            End Using

            MessageBox.Show("File received.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)

            FileSharingStatusBar.Panels.Item(1).Text = "Idle."
        End If
    End If

End While

我只是好奇,为什么会发生这种情况。我失去了一些东西呢?任何解释或建议进行梳理这一个是非常美联社preciated。请赐教。 :)

I'm just curious why does this happen. Am I missing something here? Any explanation or suggestion to sort this one out would be highly appreciated. Please enlighten me. :)

推荐答案

从我的经验与网络流,我相信你是对你的流的假设清算本身出[1]

From my experience with network streams, I believe you are right on your assumption of the stream clearing itself out [1].

我观察到与网络流另一件事是,如果一个或多个连续写操作被执行,这些写入都放在一起,不会被清除,直到在接收端读取整个流进行到底。这意味着为你的是,它可能不是可能的消息之间进行区分,如果收件人读取流之前一个以上的消息被发送。

One other thing I have observed with network streams is that if one or more successive writes are performed, those writes are all put together and will not be cleared until the receiving end reads the entire stream to the end. What this means for you is that it may not be possible to distinguish between messages if more than one message is sent before the recipient reads the stream.

解决此的一种方式是插入占位符的信息流中,以纪念不同消息的开始和/或结束。如果做到这一点,在那里的一个数据块开始,其在此结束程序就能确定。实施占位符,在我看来,应该释放目前您所遇到的麻烦你了。

One way around this is to insert placeholders in your stream to mark the start and/or end of distinct messages. If that is done, your program will be able to determine where one chunk of data starts and where it ends. Implementing placeholders, in my opinion, should free you of the trouble you are currently experiencing.

样品实施
注: code斜体可能不正确

Sample Implementation
Note: Code in italics may be incorrect

Dim placeholder As Byte() = New Byte() {&H00, &H01, &HFE, &HFF}
Dim message As New List(Of Byte)()
Dim data As Byte()

Do While True
    data = stream.ReadBytes(1024)
    If data.Skip(data.Length - placeholder.Length).SequenceEquals(placeholder) Then
        message.AddRange(data.Take(data.Length - placeholder.Length)
        Exit Do
    Else
        message.AddRange(data)
    End If
Loop

' do something with the message read

这里做的事情是,它会读取流,直到它读取数据块它与占位符签名结束,然后停止和做一些事已经从流中读取消息。

What this does is that it reads the stream until it reads a chunk of data which ends with the placeholder signature then it stops and does something with the message that has been read from the stream.

当然,在你执行你可以有多个链接的消息,因此你必须有preserving个字节的下一个信息,即是'意外'阅读方式。你也将不得不做数据块的模式匹配读,而不是如果开始/结束的占位符匹配检查。

Of course in your implementation you could have multiple concatenated messages so you'll have to have a way of preserving bytes from the next message that were 'accidentally' read. You also will have to do a pattern matching of the chunk of data read instead of checking if the beginning/end matches the placeholder.

这篇关于previous的数据保留在网络流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆