ConnectException: Connection Refused on Java
- 1). Begin the section of your program that will create a socket connection by writing a "try" function, which will contain the code that creates the connection. Create the function by writing" try {
your_connection_code
}" - 2). Add the code to connect to the target machine or server between the brackets that demarcate your "try" function.
- 3). Add a "catch" function immediately after the closing bracket of the "try" function. The code should look something like this:
"try {
your_connection_code
} catch (Exception e){
catch_function_code
}"
The "catch" function will engage if the connection your "try" function tries to establish is refused. - 4). Write what you want your program to do in case the connection is refused between the brackets of the "catch" function. By catching and handling the exception a connection error throws, the program can continue to function instead of crashing on the spot.
Source...