views:

33

answers:

1

Hi. I'm fairly new to java and I have a, probably, very easy question to answer.

I have a DatagramSocket and i have set the setSoTimeout to be around 2 seconds. I know from the Java library that if a recieve() is issued to this socket and 2 seconds passes and it doesn't recieve a reply, an exception is raised (java.net.SocketTimeoutException). Now for me, when this exception is raised, my entire client shuts down. I need a way to see if this exception has occurred and move to another section of my code rather than closing the entire client.

Thanks in advance for your help.

A: 

Wrap the line that throws java.net.SocketTimeoutException with try/catch block.

try {
  socket.receive(p);
} catch (SocketTimeoutException ste) {
  ste.printStackTrace();
}
Boris Pavlović
can you be more specific (like can you write the code you refer to)?