Skip to content

Instantly share code, notes, and snippets.

@brandonto
Created May 12, 2015 19:21
Show Gist options
  • Save brandonto/aaf2351fbe820aee0baa to your computer and use it in GitHub Desktop.
Save brandonto/aaf2351fbe820aee0baa to your computer and use it in GitHub Desktop.
Handling InterruptedIOException for Socket Timeout
// Socket used for receiving packets
DatagramSocket receiveSocket = new DatagramSocket(Constants.SERVER_PORT);
// Socket will time out after 1 second
receiveSocket.setSoTimeout(1000);
try
{
// Creates and start loop to quit on input q
Thread quitThread = new Thread(
new QuitLoop(),
"Quit thread");
quitThread.start();
while (!quit)
{
DatagramPacket requestPacket = Packet.createPacket();
// Receives response packet through socket
try
{
receiveSocket.receive(requestPacket);
}
catch (InterruptedIOException e)
{
// Time out (no packet received)
System.out.println("EXCEPTION!!!!!");
continue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment