Maybe someone can shed some light here. I am using sockets and FileStream to send a file from client to server. Everything works fine, except it is REALLY slow. I mean like 2kb every 5 seconds. The basic rundown is:
CLIENT:
fs = new FileStream(@"c:\mailroot\MENU.pdf",FileMode.Open,FileAccess.Read);
byte[] currentBuffer = new byte[16384];
fs.Read(currentBuffer,0,16384);
return Encoding.ASCII.GetString(currentBuffer);
SERVER: (Arg is the string of currentBuffer above)
fs =
new FileStream(@"c:\mailroot\temp_file.txt",FileMode.Create,FileAccess.Write);fs.Write(Encoding.ASCII.GetBytes(Arg),0,Arg.Length);
Upon testing, I found that the read is quick, and the encoding is quick. Milliseconds. But the actual transfer is taking forever.
I am using a standard a AsyncState Socket connection. I was actually thinking the project was going well, until I tried a 5 MB file. Communication between client and server was quick.
Any ideas to point me in the right direction would be greatly appreciated. I am a VB/ASP guy just started with C#, and so far, I love it.
Thank you for your time,

FileStream and Sockets running slow...
XNAConfused
Any chance you could post the Socket code here so we can step through and debug the code
amendez
Hi ahmedilyas,
Thanks for the response. Last night, after hours and hours, and banking my heading on my desk from falling asleep, I stepped through the code, and removed anything that may be bogging it down. I had a FileStream writer that I wrote to log events. I was logging the event every time it received data. I removed that for one (Even though that may not be the problem).
I also found that I was sending out 64 kb to see if that helped instead of sending 1 kb. But the listener on the server was set to receive 1 kb. So I matched them. I went through any other code, and cleaned it up. I then tested it, and it did a 5 mb file in about 4 seconds. It is all on one computer, but still, I thought that was fast.
I am going to test the logger. Maybe the problem was I was opening a new stream when I wrote to the log. I am guessing streams should be opened, and then only closed when you are finished So I can open it when the service starts, and then close it when the service stops.
The only problem now is it changes a couple bits in the file.
%PDF-1.5%aaIO is changed to %PDF-1.5%bcOS as an example. Any idea on that
Reading
fs = new FileStream(@"c:\mailroot\MENU.pdf",FileMode.Open,FileAccess.Read);
return "FEND " + Encoding.ASCII.GetString(currentBuffer);
thisSocket.BeginSend(Encoding.ASCII.GetBytes(Ret)...
Sending
Conn.BeginReceive(BA...
Data = Encoding.ASCII.GetString(BA, 0, DataSize);
fs = new FileStream(@"c:\mailroot\temp_file.pdf",FileMode.OpenOrCreate,FileAccess.Write);
fs.Write(Encoding.ASCII.GetBytes(Arg),0,Arg.Length);
I Will fool with it more after lunch. Thanks for your help!
Michael C. Gates
dstock
What do you mean "bits that are transferred are a little bit different"
If I think I know what you mean, you mean that say if you send a few bits of data, then another batch of data, instead of it recieving it in this order, you may recieve it in 1 big batch rather than the induvidual batches - am I correct in saying this
If so, I have had this many times, its just the TCP/IP natural behaviour I believe.
You could implement a "ping pong" system, in other words, send a custom message from Server to client to indicate that the data sent has been recieve, then send some more and do the same thing until there is no more data left.
While there is data, the server/listener will just keep appending data until file transfer is complete.
WII
Well, at least I know I am doing the ping-ponging correctly. I did that so I knew when I should send the next bit of information...
I did a test and logged the bits SENDING and RECEIVING. I also logged them with encoding both ways so I could make sure it wasn't now it was being encoded.
It is perfect in the logs bit by bit. But when it writes, it is a little different. It is almost like I encoded it, and re-encoded it.
It is for a binary file. I am using the FileStream object. Is that correct I am stepping through my code hoping to find somewhere where I encoded it wrong, but I can't find it, and I have even logged it directly before I write to the file.
Any ideas I will post when I find something.
Thanks for your help!
scott gallimore
Ok, the wierd thing is this: I tried opening a file because I want to do basic file transfer. I want to be able to send ANY type of file (Text, .Doc, .Zip,. Etc.) with the socket. Everything seems to be going as planned, except when I read the file, the bytes are different.
Real Line 1 of File:
+o[C€‥46( § cgZ C)E xn[y"QO Cp
After reading with code below:
+o[C (46( ,' cgZ6C)K xn["QS Cp
It seems I am reading the file wrong. Here is the basic code for reading the file:
fs = File.OpenRead(@"c:\mailroot\MENU.pdf");
br = new BinaryReader(fs);
byte[] currentBuffer = new byte[1019];
currentBuffer = br.ReadBytes(currentBuffer.Length-5);
Logger.Log(Encoding.ASCII.GetString(currentBuffer));
The logger simply does this:
public static void Log(string strLogLine){
StreamWriter writer = File.AppendText(@"C:\MailRoot\BIGmailClient.txt");
writer.WriteLine(DateTime.Now + "\t" + strLogLine);
writer.Close();}
Any ideas
NorCis
Actually, I take that back: The bits are being SENT differently. They are just a little different than the binary file. I am trying to send a PDF file.
Will let you know what I Come up with. Thanks!
hrubesh
Ori'
Great! I will give that a try.
Any idea why the bits that are transferred are a little different The wierd thing is if I look at the file as a whole, most of the bits are the same but every once in a while, they are off. For example:
1: 18171817181718171XYZ123123123123ASY@DF@WE@EFEFE
2: 18171817181718171ABC123123123123ASY@DF@WE@WWFE
Thanks for your help!!!
Michael C. Gates