Created
May 12, 2015 19:21
-
-
Save brandonto/aaf2351fbe820aee0baa to your computer and use it in GitHub Desktop.
Handling InterruptedIOException for Socket Timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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