Вопрос

I'm creating a server which needs to listen for incoming UDP packages.

My problem is that I don't know how to handle an IOException on DatagramSocket.receive(). 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:

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. 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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top