I wrote a server (code pasted below) that is supposed to receive ASCII text
files. For each file I receive, I change some header info and save it as
another file on my end. For some reason, the files that I receive come as
garbage characters. However, when I send the same file as a string, it goes
through properly. Is there some extra processing that I shoudl do if my
server is expecting a file, rather than just string data?
Here's the code for my server:
Sub ReceiveReport()
tcpListener = New TcpListener(Config.Port)
tcpListener.Start()
ui.UpdateUI("Server Active...")
Try
While keepListening
If tcpListener.Pending Then
'Accept the pending client connection and return a
TcpClient initialized for communication.
tcpClient = tcpListener.AcceptTcpClient()
CreateLogFile("Connection Accepted")
' Get the stream
networkStream = tcpClient.GetStream()
' Read the stream into a byte array
Dim bytes(BUFFER_SIZE) As Byte
Dim clientdata As String
Do
Dim numBytesRead As Integer =
networkStream.Read(bytes, 0, BUFFER_SIZE)
clientdata += Encoding.ASCII.GetString(bytes, 0,
numBytesRead)
Loop Until Not networkStream.DataAvailable
CreateLogFile(clientdata)
Dim translatedData As String =
ChangeHeaderAndFooter(clientdata)
CreateFile(translatedData)
Else
'Wait for the request
ui.UpdateUI("Server Active...")
Thread.Sleep(100)
End If
End While
tcpClient.Close()
networkStream.Close()
tcpListener.Stop()
Catch ex As Exception
CreateLogFile("Receive Report Error: " & ex.ToString())
End Try
End Sub