471,337 Members | 1,139 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Socket question

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

Jul 29 '05 #1
0 895

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gonçalo Rodrigues | last post: by
1 post views Thread by John Sheppard | last post: by
2 posts views Thread by djc | last post: by
reply views Thread by Macca | last post: by
4 posts views Thread by Engineerik | last post: by
1 post views Thread by =?Utf-8?B?UmFqbmk=?= | last post: by
2 posts views Thread by Ali Hamad | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.