Hi everyone, I need some advice on the best way to receive an unknown amount of packets.
I'm receiving a packet from a game server. The above code works fine as long as the game server sends back only one packet. Sometimes the game server will break the response up into two packets and I'll only get the first.
What is the best way to handle this situation? I have come up with solutions but they seem more like work-arounds.
Have you considered using Stream (TCP) sockets instead of Datagram (UDP) sockets? A datagram socket is connectionless, meaning packets can arrive in any order or not at all. Stream sockets are connection-oriented which guarantees ordered delivery and a persistent connection between the client and server. You would use Socket for the client and ServerSocket on the server.
Something similar to Madhan's solution. Will this work for you:
With this, your assuming that the socket connection will always timeout at some point. I'm not sure if this is true or not for your situation. I'm also assuming that the server will send an empty or null packet when it is done. If that isn't the case, then that whole if/else block can be changed to just "received+=rec;"
Hi, unfortunately I don't think the game server gives any indication for when it's splitting the reply and it won't send an empty packet either. I had hoped not to have to rely on the timeout but I'll give that a shot.