I am working on a application which sends UDP packet to initiate a Processing and needs to send back response on same socket after 3-5 minutes, but issue is that when response is sent back, it never reaches client. I am using java. Any suggestions, how we can keep the socket alive over longer period of time?
Any suggestions, how we can keep the socket alive over longer period of time?</blockquote>
Is there a way to set UDP timeout setting ...?</blockquote>
UDP is connectionless. If an application binds to a UDP server-socket and listens, it can wait for messages to arrive indefinitely.
The flipside is that there is no way to detect a lost UDP message at the protocol / socket level. If one machine sends a UDP message to another one, the first machine has no direct way of knowing if the message has arrived or not? There are no transport level timeouts to tell the sender to retransmit.
If you need reliability, timeouts, automatic retransmission, etc, you are better of using a connection-based transport protocol such as TCP.<blockquote>
Issue as suggested by a colleague, might be that firewall closes UDP connections if there is no activity for certain period of time, for communication to be reliable one need to implement timeout probe.</blockquote>
On the face of it, that explanation is nonsensical. UDP is connectionless, so there is no connection to close, or to keep alive.
The real explanation is likely to be one of the following:<ul><li>
The UDP requests or the UDP replies are being blocked by firewall software on one or other of the machines, or somewhere in the network.</li> <li>
Something is not right about the way that the messages are being sent or received.</li> <li>
You are trying to send UDP messages through a NAT gateway; e.g. to a machine whose IP address cannot be routed from the other. Special measures such as <a href="http://en.wikipedia.org/wiki/UDP_hole_punching" rel="nofollow">UDP hole punching</a> are required to get UDP traffic to a NATed host, and it may be necessary to send keep-alive packets to keep a "UDP hole" from timing out.</li> </ul>
If you are trying to send UDP through a NAT firewall from Java, there are various (3rd-party) Java libraries for doing this. Search for "stun" and "turn" or "ice" together with "java" and "udp", or read the following for a started:<ul><li><a href="https://stackoverflow.com/questions/2039147/stun-turn-ice-library-for-java" rel="nofollow">STUN, TURN, ICE library for Java</a></li> </ul>
These solutions all require you to take steps to "refresh" the NAT bindings to keep the firewall "hole" open.