ObjectInputStream for tcp/ip gaming, can message B arrive before message A?

I am making a game which has chat and has moves. Each with its separate port for communication. Here is how the object waits for incoming traffic

while (true) {

        Object o = oiStream.readObject();

                    if(o!=null)
                  {
                            blahblahblah = the code here
                 }

The code works but I am curious what happens if the client sends 2 messages and the second message arrive first? Is objectinputstream handling such things or do I need to ID tag the messages?

Your oiStream references a single client, not multiple clients so there wouldn’t be a problem, but depending on implementation you’re likely blocking the second client from connecting or communicating while this is occurring.

What second client (since I do not need one)?

oh…my mistake, I misread what you wrote.

TCP guarantees the order of delivery, so unless you have two threads that are trying to stream data at the same time you shouldn’t run into any issues.

Something you might consider if the above is the case (here comes the advertising) is my networking API JGN (JavaGameNetworking) as it can handle multiple simultaneous streams coming from multiple clients.

http://javagamenetworking.dev.java.net

So, is the multiple streams a problem even if i use different port i.e different socket?

No, but you’ve got to realize that OIS blocks so you’ll have to have a thread for each stream that is going concurrently. JGN resolves this by using non-blocking IO.

I have one thread running for chat and one for other actions and those 2 have separate ports. Despite that, I have experienced crashes that seem to occur when using the chat.

If I have a chat and both players send simultaneously then it woudl not crash?
Meaning, it is probably that the 2 different threads interfer somehow despite on different ports?
I am going to buy a second computer soon so that I can easier provoce these crashes myself and get an answer.

Each connected client will connect on an own socket. If each communication is in a thread, you cannot guarantee in which
order the clients send data. A TCP connection will guarantee:

  • the right order / sequence of data
  • nothing gets lost
  • everything is correct, no data corruption

So each single connection is safe, but if you have multiple clients you need to handle them sequential or parallel.

-JAW

I think I might have found the problem. Will be able to see later on when I buy a second computer. The 2 threads I have all add their results into one OrderThread in game that has vector storing up all stuff and performing it into the game. Since both threads access the same OrderThread some problem may occurr. I have put synchronized keyword on all the public methods in the OrderThread class now, so I will see if that solve it :slight_smile: