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(Con fig.Port)
tcpListener.Sta rt()
ui.UpdateUI("Se rver Active...")
Try
While keepListening
If tcpListener.Pen ding Then
'Accept the pending client connection and return a
TcpClient initialized for communication.
tcpClient = tcpListener.Acc eptTcpClient()
CreateLogFile(" Connection Accepted")
' Get the stream
networkStream = tcpClient.GetSt ream()
' Read the stream into a byte array
Dim bytes(BUFFER_SI ZE) As Byte
Dim clientdata As String
Do
Dim numBytesRead As Integer =
networkStream.R ead(bytes, 0, BUFFER_SIZE)
clientdata += Encoding.ASCII. GetString(bytes , 0,
numBytesRead)
Loop Until Not networkStream.D ataAvailable
CreateLogFile(c lientdata)
Dim translatedData As String =
ChangeHeaderAnd Footer(clientda ta)
CreateFile(tran slatedData)
Else
'Wait for the request
ui.UpdateUI("Se rver Active...")
Thread.Sleep(10 0)
End If
End While
tcpClient.Close ()
networkStream.C lose()
tcpListener.Sto p()
Catch ex As Exception
CreateLogFile(" Receive Report Error: " & ex.ToString())
End Try
End Sub
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(Con fig.Port)
tcpListener.Sta rt()
ui.UpdateUI("Se rver Active...")
Try
While keepListening
If tcpListener.Pen ding Then
'Accept the pending client connection and return a
TcpClient initialized for communication.
tcpClient = tcpListener.Acc eptTcpClient()
CreateLogFile(" Connection Accepted")
' Get the stream
networkStream = tcpClient.GetSt ream()
' Read the stream into a byte array
Dim bytes(BUFFER_SI ZE) As Byte
Dim clientdata As String
Do
Dim numBytesRead As Integer =
networkStream.R ead(bytes, 0, BUFFER_SIZE)
clientdata += Encoding.ASCII. GetString(bytes , 0,
numBytesRead)
Loop Until Not networkStream.D ataAvailable
CreateLogFile(c lientdata)
Dim translatedData As String =
ChangeHeaderAnd Footer(clientda ta)
CreateFile(tran slatedData)
Else
'Wait for the request
ui.UpdateUI("Se rver Active...")
Thread.Sleep(10 0)
End If
End While
tcpClient.Close ()
networkStream.C lose()
tcpListener.Sto p()
Catch ex As Exception
CreateLogFile(" Receive Report Error: " & ex.ToString())
End Try
End Sub