aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes DatagramSocket, receive multiple packets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "DatagramSocket, receive multiple packets" Watch "DatagramSocket, receive multiple packets" New topic
Author

DatagramSocket, receive multiple packets

Louis Bros
Ranch Hand

Joined: Jun 03, 2011
Posts: 54

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.

Thanks


OCA7
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

You may loop until you have null or empty packet and then do the needful. While looping, adjust the Socket timeout appropriately.


S.D. MADHAN
Not many get the right opportunity !
Louis Bros
Ranch Hand

Joined: Jun 03, 2011
Posts: 54

Hi,

Thanks for reply but in the case that only one packet was coming wouldn't that mean that the socket would timeout waiting for a second packet?

Cheers
Brian Ringers
Greenhorn

Joined: Jul 27, 2011
Posts: 2

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.
Louis Bros
Ranch Hand

Joined: Jun 03, 2011
Posts: 54

Hi,

Thanks for the suggestion. I don't think I can do that though as I have no control over what the game server code and it uses UDP.

I've found that the game server is splitting the packets once they're larger than 1kb, so for now I'll just check the pack length and receive again if needed.

Cheers
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

That could lead to problems if the server sends only one message and that message is exactly 1KB in size.

Is there not something inside the data that indicates if more data will arrive or not?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Ringers
Greenhorn

Joined: Jul 27, 2011
Posts: 2

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;"
Louis Bros
Ranch Hand

Joined: Jun 03, 2011
Posts: 54

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.

Thanks for the help
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Brian Ringers wrote:

rec will never be null - you just created it as a new String.

Also, don't use String concatenation. Use a StringBuilder and its append methods. It's more efficient.
 
jQuery in Action, 2nd edition
 
subject: DatagramSocket, receive multiple packets