简体   繁体   English

Java中的setsotimeout

[英]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. 我正在尝试通过DatagramSocket发送数据,并希望这样做,如果发送的数据已超过其确认的超时时间,则应重新发送该数据。

Can we use the option of DatagramSocket.SetSoTimeout for it ?? 我们可以使用DatagramSocket.SetSoTimeout选项吗? 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 ?? 这可能吗?

The documentation seems rather clear about setSoTimeout purpose: 该文档对于setSoTimeout目的似乎很明确:

a call to receive() for this DatagramSocket will block for only this amount of time 对此DatagramSocket的receive()调用将仅在此时间段内阻塞

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. 首先,它与send没有任何关系,其次,只有在阻塞一定时间后才超时。

If you want reliability use TCP. 如果要可靠性,请使用TCP。 If you absolutely need/want to use UDP you'll have to devise your own reliability mechanism. 如果您绝对需要/想要使用UDP,则必须设计自己的可靠性机制。 Here's another SO question about this particular problem: 这是关于此特定问题的另一个SO问题:

What do you use when you need reliable UDP? 当您需要可靠的UDP时使用什么?

Basically it really depend on what you're doing, because if you need a generic solution, you'll end up reinventing TCP! 基本上,这实际上取决于您在做什么,因为如果您需要通用的解决方案,您将最终重新发明TCP!

UDP is unreliable, it has no ack's. UDP不可靠,没有确认。 ie There's no acknowledgement timer that can be exceeded. 即,没有可以超过的确认计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM