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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.