Reputation: 88
I am creating a java multiplayer game which resolves around constant sending of DatagramPackets between client and server on a LAN.
Randomly, the client's DatagramSocket will stop receiving packets from the server and thus the client does not receive updates from the server.
It appears that the DatagramSocket stops receiving completely and will not resume receiving. This problem has happened in every client-server situation.
If anyone has any ideas, please say as I would am out of ideas!
Windows PC, Java 1.8
listening = true
PACKET_SIZE = 3096
buffer = new byte[PACKET_SIZE]
addr = InetAddress.getLocalHost()
port = new Random().nextInt(65536);
processor = Object
while(listening) {
DatagramPacket p = new DatagramPacket(buffer, buffer.length, addr, port);
try {
socket.receive(p); // Code stops here
processor.add(p); // sends to queue to be processed
} catch(IOException e) {
e.printStackTrace();
}
buffer = new byte[PACKET_SIZE];
}
I would expect the program to run indefinitely (assuming that the server continues to send packets). However the socket stops receiving packets after a random amount of time (isn't the same random amount of time every time).
Upvotes: 1
Views: 612
Reputation: 718798
Short answer: either the remote server has stopped sending UDP datagrams or the network has stopped delivering them for some reason.
I assume that you have some way of letting the server know the random port and IP that you are using to listen for datagrams. If the server doesn't use the same port and IP to send, the UDP datagrams will be silently disappear. (But you say that your client code works for a bit and then stops. That implies that the IP and port are correct.)
I also assume that you would have told us if you were getting exceptions and seeing stacktraces(!)
Apart from the above, I see nothing wrong with your code. If I haven't missed something, that implies that the problem is something else. The only other explanations I can think of are:
The server has stopped sending.
The network has stopped passing UDP packets. It is hard to know why:
As suggested, try using Wireshark at both ends to see if UDP packets are still being sent by the server and are still arriving at the client. That will help you narrow down the problem.
Note that if you are trying to implement a 2-way or multi-way communication over UDP you need to allow for messages getting randomly dropped. Your application-level protocols (implemented using UDP transport) need to be able to detect1 and recover from this, in all scenarios where a message might be dropped. If you get this wrong, one of the possibilities will be that the protocol "locks up".
1 - This will typically involve implementing timeouts at some level. However just setting a timeout on receive
(as suggested by another answer) does not solve the problem.
Upvotes: 2
Reputation: 2163
Some suggestions to help debug:
Set a reasonable timeout for the socket with setSoTimeout(int milliseconds) and also catch SocketTimeoutException. The server may have sent a packet and for some reason it never arrived. If the server is waiting for a response before sending another packet, your client will be waiting indefinitely. You can then catch the SocketTimeoutException and send/resend an appropriate packet to the server.
Install Wireshark to monitor packets sent from the server, and also the packets received by the client. You should be able to see if the packet actually gets sent by the server, if the packet is never received by the client, or if the packet is actually received by the client but something goes wrong at the client's end.
Upvotes: 0