简体   繁体   English

在Java中为DatagramSocket()检查setSoTimeout

[英]Checking setSoTimeout for DatagramSocket() in Java

I'm fairly new to java and I have a, probably, very easy question to answer. 我对Java还是很陌生,可能有一个很容易回答的问题。

I have a DatagramSocket and i have set the setSoTimeout to be around 2 seconds. 我有一个DatagramSocket,我将setSoTimeout设置为2秒左右。 I know from the Java library that if a recieve() is issued to this socket and 2 seconds passes and it doesn't recieve a reply, an exception is raised (java.net.SocketTimeoutException). 我从Java库知道,如果向此套接字发出了recieve()并且2秒钟过去了并且它没有收到答复,则会引发异常(java.net.SocketTimeoutException)。 Now for me, when this exception is raised, my entire client shuts down. 现在对我来说,当引发此异常时,我的整个客户端都将关闭。 I need a way to see if this exception has occurred and move to another section of my code rather than closing the entire client. 我需要一种方法来查看是否发生了此异常,然后移至代码的另一部分,而不是关闭整个客户端。

Thanks in advance for your help. 在此先感谢您的帮助。

Wrap the line that throws java.net.SocketTimeoutException with try/catch block. try/catch块包装引发java.net.SocketTimeoutException的行。

try {
  socket.receive(p);
} catch (SocketTimeoutException ste) {
  ste.printStackTrace();
}

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

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