user506710
user506710

Reputation:

setsotimeout in java

I am trying to send data through DatagramSocket and would like to do this that if the data sent has exceeded the timeout of its acknowledgement then it should be resend.

Can we use the option of DatagramSocket.SetSoTimeout for it ?? If yes how can I ??

For eg

try
{
  while(true)
   {
    socket.send(data);
   }
}catch (SocketTimeoutException e)
{
 // resend for which it occured
}

IS this possible to do ??

Upvotes: 0

Views: 876

Answers (2)

Nicolas Buduroi
Nicolas Buduroi

Reputation: 3584

The documentation seems rather clear about setSoTimeout purpose:

a call to receive() for this DatagramSocket will block for only this amount of time

First it doesn't have anything to do with send and second it only timeout if it's blocking for a certain amount of time.

If you want reliability use TCP. If you absolutely need/want to use UDP you'll have to devise your own reliability mechanism. Here's another SO question about this particular problem:

What do you use when you need reliable UDP?

Basically it really depend on what you're doing, because if you need a generic solution, you'll end up reinventing TCP!

Upvotes: 3

nos
nos

Reputation: 229344

UDP is unreliable, it has no ack's. i.e. There's no acknowledgement timer that can be exceeded.

Upvotes: 2

Related Questions