私は現在、いくつかのロギング機能を備えたファイル転送アプリケーションを構築しています。クライアントが接続または切断するたびに、ログ(文字列メッセージ)がサーバーに送信されます。ロギング部分は正常に機能していますが、ファイルを送信しようとすると、プログラムが混乱します。
これは純粋にサーバー側の問題のようです。何が起こるかというと、以前のデータです。これはロギングの文字列メッセージであり、クライアントから送信されたものがネットワークストリームでスタックしているようです。サーバーに接続した後にファイルを送信しようとすると、パスに不正な文字が含まれているというエラーが表示されます。
これがエラーのスクリーンショットです。
これは、上のスクリーンショットの変数でわかるように、FileName
接続されたクライアントがネットワークストリームにスタックしたときに送信された文字列の一部(「接続されています。」)が原因で発生すると思います。hello.cppは、送信されるファイルの名前です。
これがコードです。
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
なぜこれが起こるのか興味があります。ここで何かが足りませんか?これを整理するための説明や提案をいただければ幸いです。教えてください。:)