# Java Networking impossible and erratic errors

## Recommended Posts

I'm pretty much at a total loss here.  I tried all day yesterday and all night last night to figure out what the hell is wrong with my code but now I'm thinking there's actually a little gremlin in my computer nibbling on the CPU.  I was following along with this tutorial series:

until I decided to pause it and make my own custom Packet class.  It was working fine for a while but then some really weird stuff started to happen.  Basically what's happening is the data sent between client and server is getting jumbled up somehow when it is received (I confirmed that the data was correct before it was sent).  This happens at seemingly random points and now I'm baffled

For example I had the line:

System.out.println( m_Game.m_aConnectedPlayers[ intPlayerID ].GetUserName( ) + " has joined." );

and changed it to

System.out.println( m_Game.m_aConnectedPlayers[ intPlayerID ].GetUserName( ) + "(" + intPlayerID + ") has joined." );

to include the player ID, and then the packet data was getting scrambled up when I connected to the server with a third client.  Also even as I typed this I decided to go back and test it again and the error stopped happening.  I'll grant +100 internet points to whoever can somehow figure this out.  I've attached my entire project (excuse my messy code)

##### Share on other sites

Sorry here is the project

##### Share on other sites

There is no guarantee that UDP packets arive in the order in which they were sent (they might even get lost or come in duplicated). Not sure if this is causing your problem though. Instead of uploading the whole project you could rather simplify it until you know where the error is comming from and upload just that specific file which can reproduce the problem.

##### Share on other sites

1.  (This is the hard part)  Take a deep breath and accept the fact the problem isn't the OS, or Java, or the network.  Your have made a mistake somewhere.  I have had dozens of problems with networking code, and it always seems like there is a problem with something other than my code.  Do you know how many times it turned out to be me?  100%.

2.  Wireshark - www.wireshark.org and tcpview - http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx are your friends.  Take some time to filter and capture packets.  Make sure they are really getting there.  And make sure connections are getting closed, and there is nothing really weird happening.

3.  As EVIL_ENT said, recreate the problem with the smallest code example you can.  Most likely while trying to strip away unneeded code you will figure it out.

For example, I once had a UDP broadcast going out, and every so often, some responses were getting dropped.  I was sure it was not my code.  I think I played with it for six months on and off before I realized that there were so many responses coming back that the UDP receive buffer was overflowing.  I up'ed the buffer size, and fixed the problem.  I also, because of testing to recreate the problem, was able to figure out how many devices could be supported until there were more errors.

Good Luck!

##### Share on other sites

There is no guarantee that UDP packets arive in the order in which they were sent (they might even get lost or come in duplicated).

This. 100x.

You're sending raw bytes using UDP with no way to a) deal with lost packets, b) deal with duplicated packets, or c) deal with out of order packets. You aren't wrapping your bytes up into a proper datagram either, but instead are just sending the raw bytes (how does the receiving end know what the bytes are? There's no "header" in the datagram that the receiving end can read to know how to interpret the data it just received).

Typically, in networking, you have your data wrapped up in some type of structure or prefixed with some header. Like this:

//Datagram Packet (this is purely for example, and may vary from program to program in real life)

First two bytes: Type of the datagram
Next 8 bytes: UUID of the datagram (to detect duplicated, lost, or out of order packets)
Remaining bytes: Payload data, which the program knows how to interpret based on the type of this datagram



##### Share on other sites

Unfortunately as I said the errors just pop up and vanish mysteriously so the only possible way for me to catch one would be to record my screen while working and upload the video of it happening I guess.

But based on what I'm reading, it sounds like a bad idea to send multiple packets in quick succession that I need to arrive in order with UDP, which is one thing I'm doing.  Is it a better idea to condense data into one UDP packet then split it up after it's received?  For example something like

byte[ ] { Packet ID, value 1, value 1, value 1, value 2, value 2, value 2, }

as opposed to

byte[ ] { Packet ID }

byte[ ] { value 1, value 1, value 1 }

byte[ ] { value 2, value 2, value 2, }

If so then what kind of conventions should I use to indicate the data type and the start/end of a value?  Also, can you help me understand how sending a UUID will let the program know that the packets were duplicated, lost or out of order?

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628361
• Total Posts
2982264

• 10
• 9
• 13
• 24
• 11