   Socket - Packet Loss (TCP)
  Hello every1!Well, I'm very new to this, there are many things I really don't know & I'm just learning on the fly.I'm having a problem of packet loss when sending data from one pc to another in a LAN. I have a server (PC wired to my router) & a client (laptop wirelessly connected to the router).I guess though, it's a programming problem, not hardware. But it's just a guess. (C# Visual Studio 2003).I did kinda chat (because I planned it to be one server & many clients), where clients send data to the server & it broadcasts to all clients (BUT the original client sender). If the server sends data, it broadcasts it to everyone (it's not a dedicated server).The data I send is a an object (I put the [Serializable] thingie to be able to send it). It has only 6 INT private attributes with the properties. I fill this attributes & I serialize it (System.Runtime.Serialization.IFormatter). Then I send it Async (BeginSend). After it: EndSend.BeginReceive looks like this:socket.BeginReceive(socketPacket.DataBuffer,0,socketPacket.DataBuffer.Length,SocketFlags.None,this.pCallbackReceivedMsg,socketPacket);The DataBuffer in the socketPacket, is a byte[500].The lenght of the data I send (after serializing) is 201.If I use a DataBuffer of 201 or 300 approx it crashes when getting the first or second packet :-SI put in server & client:-SendBuffer,506-ReceiveBuffer,506-DontLinger,1-NoDelay,1(i tried with values like 201, 500, 1000, 2000, 2000000, 4000000 & seems to be more laggy as I increase it but actually there is no BIG difference, i'm not very conscient of the buffers length usage).I need speed in the system. The objects I send represent keys that i press (with some more data). Logically, if u play a game, u press like 3 or 4 keys at the same time plus mouse movement & buttons... Well, i need to send this keys data. Let's say sometimes it can be 6 keys together & i need to receive them in order & synchronized. If I press keys slowly, it works. If i hurry, there is lag & some keys doesn't come AT ALL to the client. If I press 2 keys together, i get first one & then (half a sec? a bit more? 1 sec?) the other. If I press MANY keys together (14,15 or so) altogether I get an error in the client: "The input stream is not a valid binary format. The starting contents (in bites) are:..." & a bunch of numbers like: 43-75-6C-74-75-72...I thought about:-creating buffers by myself together with a timer to send & receive things in order, handling evrth "manually".-changing the object that i transmit the data in into a structure.I don't know if any of this choices may help or if the problem is in another place...! What do u suggest me? (how to write code sections here in my posts?)THANK YOU VERY VERY MUCH!Synthesizer.   
  hm, no one...?!I'm considering many things but there are risks in all the possible paths...I have the impression that it may be a speed problem so I'm considering to move it to UDP. The worry I have is the packet loss in UDP.I basically send some packets telling "start" & others telling "stop". If a "stop" packet is loss then there will be an endless action in the client...! & I *really* don't wanna do a very sophisticated system for this... I guess i may have to do kinda "u got it?" "yes" else -> resend, or stuff like that.Another option would be go on in TCP & make an "user buffer" but that may involve timers in the server & timers in the client (to store evrth in the correct time & upack it later the same way) & i think it will *really* be tangled... In the same hand I don't know how big the packet can be... Today I'm sending an object with 6 int attributes. Can I send for example a container with 6 of this objects? 8? that would be too much? Better only 1 object thru UDP?!!! Better struct & no object??? :)Suggestions? Advices? Moderators any idea please...??!   ;  When you do a Socket.Receive, you're not guaranteed to get all of the data you sent from the other side in a single call (even in TCP).  You need to determine how much you want to receive and then keep calling Receive until you actually finish receiving the amount of data you expect.There are two really simple ways to know how much data to expect:- Always use a fixed size buffer that is as large as the largest message you'll ever send.  Since you're using serialization, you probably won't have a well-defined maximum size.- Send the size of the message as a uint before the rest of the data.   ;  Thank you very much Nypyren!I didn't know that & it's a key info. I'll change my code in order to get all the sent data expecting to probably receive it all after multiple calls.Thanks again, i'll let u know the results later.   ;  Allright, I've modified the app & now I'm sending the size of the packet in the 1st byte.It works, when I receive the packet, I check if the size is the same. When things are quiet, size matches & evrth is great. When I send many messages quickly, I detect incomplete packets (size that I read is less than size in the 1st byte of the packet).In the beginning, I made evrth manually: built a buffer & tried to fill it with the partial info I was receiving. It was a mess & it didn't work...Then I re read ur tip slowly & decided to literally "keep calling Receive until I actually finish receiving the amount of data I expect". But it seems to crash after I receive the incomplete data & make a new call to get more.Here I put some of the code://this function is in the main class, (outside server or client)// & is called when the server/client receives a message (event):private void manageMsgReceived(IAsyncResult result){SocketPacket socketPacket=(SocketPacket)result.AsyncState;int iBytesRead=socketPacket.CurrentSocket.EndReceive(result); if((iBytesRead-1)>=System.Convert.ToInt16(socketPacket.DataBuffer[0])){this.processFullPacketReceived(socketPacket,iBytesRead);}else{if(server!=null) //If it is not a server, it's a client{ //this sucks but i just did it quickly   //to make it workserver.resumeReceive(socketPacket);}else{client.resumeReceive(socketPacket);}}//this code is in the Server/Client:public void waitForData(System.Net.Sockets.Socket socket,int iClientIndex){if(this.pCallbackReceivedMsg==null){this.pCallbackReceivedMsg=new AsyncCallback(resolveReceivedMsg);}SocketPacket socketPacket=new SocketPacket(socket,iClientIndex);socket.BeginReceive(socketPacket.DataBuffer,0,socketPacket.DataBuffer.Length,SocketFlags.None,this.pCallbackReceivedMsg,socketPacket);} public void resumeReceive(SocketPacket socketPacket){socketPacket.CurrentSocket.BeginReceive(socketPacket.DataBuffer,0,socketPacket.DataBuffer.Length,SocketFlags.None,this.pCallbackReceivedMsg,socketPacket);}public void endSend(System.IAsyncResult result){System.Net.Sockets.Socket socket=(System.Net.Sockets.Socket)result.AsyncState;socket.EndSend(result);}It's actually hard to debug as I have to run the thing in 2 comps & getting the incomplete msgs is rather random.I guess I'm not doing the correct thing when trying to complete the data. Maybe u can gimme a tip?Thank u very much!;  To debug this, the easiest thing to do is to modify the sending side. If you know you want to send the byte 20 followed by 20 bytes, then send the byte 20, then send 10 bytes, then sleep for a second, then send another 10 bytes.You can also debug it on the same machine, running client and server on the same machine, as long as you don't bind the client to a specific port.When you say "it appears to crash," it's hard to give any more suggestions, because you're not giving any more information. Does it lock up? If so, where is the stack when you break into the program from the debugger? Does it throw an exception? If so, what exception, and from what point in the code? Something else?