Java.net.SocketException
Exception
The problem occurred during the execution of the program. If an exception occurs in the program, the program gets terminated. To skip the exception occurring statements, we have to handle the exception.
To handle the exception, we have four keywords:
- try
- catch
- throw
- throws
- finally
Exceptions are of two types.
- Checked Exception
Ex: IOException, SQlException, ClassNotFoundException,FileNotFoundException, etc. - Unchecked Exception
Ex: ArithematicException, ArrayIndexOutOfBoundException, NullPointerException, ClassCaseException.
Java.net.SocketException
IOException is the parent class of the SocketException class.SocketException occurs when a problem is trying to open or access the socket. SocketException class inherits the properties of the IOEception class. All Exception classes are present in the Exception class, and this class is a subclass of the Object class.
As you would be aware, it's unequivocally encouraged to utilize the most unambiguous attachment exemption class that assigns the issue all the more precisely. It is additionally important that SocketException typically accompanies a blunder message that is extremely enlightening about the circumstance that caused the special case.
Socket Exception hierarchy:

What is Socket Programming?
A programming idea utilizes attachments to lay out associations and empowers various projects to connect with one another utilizing an organization. Attachments give a point of interaction to lay out correspondence utilizing the organization convention stack and empower projects to share messages over the organization.
Attachments are endpoints in network correspondences. An attachment server is normally a multi-strung server that can acknowledge attachment association demands. An attachment client is a program/process that starts an attachment correspondence demand.

java.net.SocketException: Connection reset
When the client shuts the attachment association in the client-side app the SocketException happens on the server-side before the reaction can be returned over the attachment. For instance, by stopping the program before the reaction was recovered. Association reset essentially implies that a TCP RST was gotten. TCP RST parcel is the remote side letting you know the association on which the past TCP bundle is sent isn't perceived, perhaps the association has shut, perhaps the port isn't open, and something like this. A reset parcel is basically one with no payload and with the RST bit set in the TCP header banners.
Presently as of execution, plainly, we want two projects, one dealing with the client and the other taking care of the server. They are as per the following:
Example 1: Server-side
// java program to implement SocketException in server-side App
// Importing required packages and class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class HelloWorld {
// Main driver method
public static void main(String[] args)
throws InterruptedException
{
new Thread(new Server()).start(); // calling the constructor and running the thread
}
static class Server implements Runnable {
// run() method for thread
@Override public void run()
{
ServerSocket s = null;
// To check exception we are using try block
try {
s = new ServerSocket(3333);
s.setSoTimeout(0);
// the loop iterates till the condition is true
while (true) {
try {
Socket client
= s.accept();
// Object creation to the BufferReader class
BufferedReader inputReader
= new BufferedReader(
new InputStreamReader(
client
.getInputStream()));
System.out.println(
"Client said :"
+ inputReader.readLine());
}
// Exception handling
catch (SocketTimeoutException e) {
e.printStackTrace();
}
}
}
// Exception handling using catch block
catch (IOException e1) {
e1.printStackTrace();
}
finally {
try {
if (s != null) {
s.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Example 2: Client side
// java program to implement SocketException in client-side App
// Importing required packages and class
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
public class HelloWorld{
public static void main(String[] args)
{
// anonymous inner class
// calling the constructor and running the thread
new Thread(new Client()).start();
}
static class Client implements Runnable {
@Override public void run()
{
// initializing value to the Socket s
Socket s = null;
// checking Exception using try block
try {
s= new Socket("localhost", 3333);
// object creation to the PrintWriter class
PrintWriter outWriter = new PrintWriter(
s.getOutputStream(), true);
// Display message
System.out.println("Wait");
// making thread to sleep for 1500 nanoseconds
Thread.sleep(15000);
// Display message
outWriter.println("Hello Mr. Server!");
}
// Handling the Exceptions using the catch block
// Catch block 1 for Exception handling
catch (SocketException e) {
// displaying the line where the Exception has occured
e.printStackTrace();
}
// Catch block 2for Exception handling
catch (InterruptedException e) {
e.printStackTrace();
}
// Catch block 3for Exception handling
catch (UnknownHostException e) {
e.printStackTrace();
}
// Catch block 4for Exception handling
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
// If the value of the socket is NULL then close the Socket
if (s != null)
// Close the socket
s.close();
}
// catching the IOException
catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Output:
