简体   繁体   English

DatagramSocket.receive()何时会引发IOException?

[英]When does DatagramSocket.receive() throw IOException?

I'm creating a server which needs to listen for incoming UDP packages. 我正在创建一个服务器,该服务器需要侦听传入的UDP程序包。

My problem is that I don't know how to handle an IOException on DatagramSocket.receive() . 我的问题是我不知道如何处理DatagramSocket.receive()上的IOException Every tutorial I can find either just print its stack trace and continue or throws it away from the method. 我可以找到的每个教程都只是打印其堆栈跟踪并继续进行操作,或者将其从方法中丢弃。 No one actually tells me when or why the exception is thrown. 实际上没有人告诉我何时或为什么引发异常。 Even the Javadoc just states: 甚至Javadoc都只声明:

IOException - if an I/O error occurs when creating the socket.

This gives me a hard time answering what I should do in such event. 这使我很难回答在这种情况下应采取的措施。

  • Can I safely resume reading the socket? 我可以安全地恢复读取套接字吗?
  • Should I close it and try again? 我应该关闭并重试吗?
  • Should I treat it as a fatal exception and quit everything? 我应该将其视为致命的异常并退出所有内容吗?

It throws SocketTimeoutException if you have set a read timeout and it expires. 如果您设置了读取超时,并且超时,它将抛出SocketTimeoutException In this circumstance you can just retry. 在这种情况下,您可以重试。 It throws IOException if something else is wrong: most of these are fatal to the socket and you must close it. 如果有其他错误,它将抛出IOException :大多数错误对套接字来说是致命的,您必须将其关闭。

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

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