I’ve developed a nice free game for Windows Phone 7, which is called Domination, and which is, despite the early release, quite a success!
Now, I’m developing an Online Multiplayer Version, which regards interesting features, and now that I’ve almost reached the end, I’m encountering a BIG problem.
WEIRD packet loss, or something like that.
I’ve a sample for reproducing the problem.
I’ve a Server.
I’ve a Win Form Client
I’ve a XNA Client
steps to reproduce the problem:
1) you start the server, the win form and the game ( you need an emulator and WP7 SDK )
2) now, you press the GO button, and the form will open the TCP channel to server
3) now, you press the screen on emulator, and the form will open the TCP channel to server
4) now, each time you press the screen emulator, or the GO button on win form, the server will send you back 50 messages on proper client
well, the problem is that
1) win form usually receives 50 messages, RARELY loses 10 packets on one communication, but it’s RARE
2) emulator, ALWAYS loses 30-40-45 messages!!!!!
I’ve tried other ways, but nothing changed..
one tip, if i put a Thread.Sleep(10) which 10 is 10 milliseconds, for each Server Send, it works perfect!!
Can anyone help me please? I just don’t know where to put my head!
samples can be found here:
No messages are being lost. They are all being received. Your code is just not correctly interpreting the received data. If you look at the number of bytes received, it will be correct. If you look at the data in the bytes received, it will be correct. The rest is up to your code.
TCP provides a byte stream service. That means you get out the same bytes you send. If you need to “glue” those bytes together into messages, then you must write code to do that. If you send 30 bytes, you might receive 30 bytes, or 10 bytes and then 20 bytes, or 1 byte 30 times, or anything in-between. If you send 5 bytes and then 3 bytes, you might receive 5 bytes and then 3 bytes. Or you might receive 8 bytes. Or you might receive 1 byte 8 times.
You must define a message format. You must code the sender to send messages in that format. You must code the receiver to identify when it has received a complete message.
What’s happening is that you are sending “FOO” and then “BAR” and receiving “FOOBAR”. Nothing has been lost in this process except the boundary between “FOO” and “BAR”. However, TCP has no concept of a message boundary. If you need them, you must code them. For example, you could send “3FOO3BAR”. Now, no matter how that gets received, the receiver knows that “FOO” is one message and “BAR” is another. Another choice is “foo\nbar\n” (foo newline bar newline). The receiver knows that it has a complete message when it receives a newline character.
But however you define an application-level message, you must actually write the code to do it. It won’t happen by itself.