0
Reply

Binary Data & .Net Sockets, URGENT

John Fawzy

John Fawzy

Jan 31 2006 3:43 PM
1.8k
Hello every one,

After 2 hard week of work, finally i accomplished an reliable server and client classes which look like the VB Winsock, those 2 classes are reliable, also server enable multiple connections, as i test it accept 5000 connection and Handel them,

When i try to create an demo app. on these 2 powerful classes, all goes fine, i faced an problem in sending files, as I'm using one channel only between server and client, i.e. each client have only one channel, this channel Handel both data and commands, we can distinguished between them, that command will always contain [#<CMD>#] tag before the command itself, so in the OnDataReceived event, we convert the byte array to ASCII string to search inside it, if we find the prev. tag then this a command else we just use that array to put on file or something, the logic is tested before on my prev. vb6 app.

Here an scenario of connection,
1- Client connected to server, server add its info to listview,
2- Server request from client to capture desktop screen
3- Client receive the command and capture the image convert it to JPEG, and then add to that array the following tag at the end of array [#<EOF>#], which help us to detect the end of file.
4- Client tell the server that it ready to send, given to server the length of image array without the length of tag,
5- When server receive readytosend, it the client socket info to helper class which contain the socket id, image size, and memory stream, which contain image chunks,
6- then Server send the client an message ready to receive, in this moment client start sending data chunks in order of 1024 Byte per time,
7- Every time server receive data it check if its EOF or not, if its, remove the EOF tag, and all nulls, and write the last chunk to the image,

The problem is the image in coming deformed, also some times length of incoming data become greater than length saved from client in early steps, i.e. an **** on line collected while getting data,

After hard work, i try to put an execution delayer, using Thread.Sleep(0), and the surprise that its finally work, and the image size is correctly, and image is nice, But all this in LAN, when test app on Wan all goes bad again, i think the problem not the delay, but delay here prevent some bad thing to happen, i want to know why server put extra data on the image stream, from where these extra data come, Am I Missing Something.?!?!?!??!?!?!?

All i need, Any one test this code, its amazing, very exciting and commented, will learn you a lot, all i need is to detect the reason of that bugs,

To know what I'm talking about, In the server side in the OnDataReceived even, try to comment the Thread.Sleep(0); and try to get an image, you will got ****, if you put that line again all goes fine, and if you put it, and test it on Wan it will cause problems again, So there is something wrong, and i don't know it

Please any body help me finding that error.

Here is the link of the code, its written using C# 2005.
Source Code & Binary Files.

Thanks every one and hope i explain the problem well, and hope for fast response.
Thanks in advanced for whatever help.