I want to know how I can catch a 'connection refused' exception in Java when I am using socket. (which would happen when server is down or not responding.)
Below is how I have implemented so far.
try {
sockfd = new Socket(host.getHostName(),heart_port);
sockfd.setReuseAddress(true);
BufferedReader message = new BufferedReader(new InputStreamReader ( sockfd.getInputStream() ) );
message.close();
sockfd.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Add ConnectException before IOException
catch (ConnectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With