1

这可以被认为是我之前线程的延续。

我在我的文件共享应用程序上做了一些开发和修复,但现在我面临另一个问题。我想这个并不难,只是我目前的脑力已经耗尽来解决这个问题。我相信这可以以更好的方式完成。

这是我收到的错误的屏幕截图。

正如您在屏幕截图中看到的那样,FileName变量中有一些值。它是我要发送的文件的名称。如果您注意到,变量包含ello.cpp,它必须是hello.cpphello.cpp是正在发送的文件的名称。尾随字符是文件的内容。尾随字符不再是问题,只要我能正确解决这个问题,文件名将是唯一存储在变量中的一个。

该错误发生在网络流的读取过程中。我想要实现的是,我想防止网络流在不是日志消息时被读取。我需要把它放在networkstream.read某个地方,这样它就不会占用文件的第一个字母。也许某种过滤?或者,如果可能只有一个代码可以获取日志消息和文件名,那就太好了。文件名是使用二进制写入器发送的,我尝试使用网络流来检索文件名,但它不起作用。我在这里错过了什么吗?

这是服务器部分的代码。

    Try

        While FileSharingStarted

            If CBool(ClientSocket.Available) Then

                Dim ByteData(ClientSocket.ReceiveBufferSize) As Byte

                'This reading of the stream here causes the problem. That's why the
                'file name isn't read properly. This reading part here is actually
                'intended for the logging part. What happens is that when the string
                'for the logging part is sent by the client side, this is being read
                'using this. But when a file is sent, it is read here in this part
                'that's why when the algorithm for retrieving the file name below
                'kicks in, the first letter of the file name is missing, and ruins
                'the whole thing.
                networkStream.Read(ByteData, 0, CInt(ClientSocket.ReceiveBufferSize))

                fileLogMessage = Encoding.ASCII.GetString(ByteData)
                Dim ConnectionStatus() As String
                ConnectionStatus = fileLogMessage.Split(CChar(" "))

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

                    'Creates a log when the user connects.

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

                   'Creates a log when the user disconnects.

                Else

                    'Algorithm for receiving the files.

                    Dim FileName, FilePath As String
                    Dim FileLength As Long

                    'This part here conflicts with the `networkstream.read` above
                    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

                            If ReadBytes = 0 Then
                                fileStream.Close()
                                FileTransferInterrupted = True
                                Exit Do
                            Else
                                ReadBytes = ClientSocket.GetStream.Read(FileData, 0, FileData.Length())
                                fileStream.Write(FileData, 0, ReadBytes)
                                TotalData += ReadBytes
                            End If

                        Loop

                    End Using

                    If FileTransferInterrupted Then

                        MessageBox.Show("File transfer interrupted.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        FileSharingStatusBar.Panels.Item(1).Text = "Idle."

                    Else

                        MessageBox.Show("File received.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        FileSharingStatusBar.Panels.Item(1).Text = "Idle."

                    End If

                End If

            End If

        End While

这是客户端的代码。

Try

        Dim fileInfo As New FileInfo(txtFilePath.Text)
        Dim binaryWriter As New BinaryWriter(fileClientSocket.GetStream())

        'Here's the part where it writes the file name on the
        'stream using the binary writer
        binaryWriter.Write(fileInfo.Name)
        binaryWriter.Write(fileInfo.Length)

        Using fileStream As New FileStream(txtFilePath.Text, IO.FileMode.Open, IO.FileAccess.Read)

            Dim FileData(8092) As Byte
            Dim ReadBytes As Integer = -1
            FileSharingStatusBar.Panels.Item(1).Text = "Sending file . . ."

            Do Until ReadBytes = 0

                If CBool(fileClientSocket.Available) Then

                    MessageBox.Show("File transfer interrupted.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub

                Else

                    ReadBytes = fileStream.Read(FileData, 0, FileData.Length)
                    fileClientSocket.GetStream.Write(FileData, 0, ReadBytes)

                End If

            Loop

        End Using

        txtFilePath.Text = ""
        MessageBox.Show("File sent.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
        FileSharingStatusBar.Panels.Item(1).Text = "Idle."

    Catch ex As Exception

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

End Try

建议将不胜感激。

提前致谢!:)

4

2 回答 2

0

我建议始终读取网络流中的所有数据并将其附加到通信缓冲区/队列中。每次向队列追加更多数据时,分析整个队列中的数据以查看它是否包含任何完整的消息。如果是这样,请将它们从队列中删除,然后处理它们。例如,您可以创建一个实现如下接口的类:

Public Interface IIncomingDataBuffer
    Sub Append(ByVal data() As Byte)
    Event MessageReceived As EventHandler(Of MessageEventArgs)
End Interface

如果消息是固定宽度或由预定义的分隔符分隔,则更容易实现此类功能。另外,我建议在每条消息的开头有一个标题块,如果没有别的,它包含一个消息类型字符。

于 2013-02-22T15:24:47.540 回答
0

我想出了如何解决它。感谢我在 facebook 上的一位朋友的建议,一位程序员。我所做的是发送一些字符串,指示服务器部分接收文件。现在一切都好。:)

我还要感谢 Steven Doggart 所做的所有努力和建议,尤其是关于 SOAP。

非常感谢好先生!:)

于 2013-02-23T09:47:40.593 回答