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 min开发者_开发问答utes, 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?
and
Is there a way to set UDP timeout setting ...?
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.
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.
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:
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.
Something is not right about the way that the messages are being sent or received.
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 UDP hole punching 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.
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:
- STUN, TURN, ICE library for Java
These solutions all require you to take steps to "refresh" the NAT bindings to keep the firewall "hole" open.
精彩评论