这可以被认为是我之前线程的延续。
我在我的文件共享应用程序上做了一些开发和修复,但现在我面临另一个问题。我想这个并不难,只是我目前的脑力已经耗尽来解决这个问题。我相信这可以以更好的方式完成。
这是我收到的错误的屏幕截图。
正如您在屏幕截图中看到的那样,FileName
变量中有一些值。它是我要发送的文件的名称。如果您注意到,变量包含ello.cpp,它必须是hello.cpp。hello.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
建议将不胜感激。
提前致谢!:)