In this article, we will make a simple Client-Server application using Java Sockets. The first step is to create the classes. Let’s jump right on and create The Server and the Client classes

public class Client{
}
public class Server{
}

Pretty simple for now, right? Let’s attack the Server 😈😈. We create the class attributes and the initAndStart() method to initialize the server and run it. Basically, running the server is creating an infinite loop.

public class Server {

    private ServerSocket server;

    public void initAndStart() throws Exception {
        server = new ServerSocket(4040);
        System.out.println("Server is running...");
        while (true) {
            Socket clientSocket = server.accept();
            System.out.println("Client is connected");
            readMessageFromSocket(clientSocket);
        }
    }
}

Now we need to add the method that will read the messages sent by the client. In this case, I named it readMessageFromSocket() we’ll also create the method close to close the socket.

private void readMessageFromSocket(Socket clientSocket) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    private void readMessageFromSocket(Socket clientSocket) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        for (int chr = reader.read(); reader.ready(); chr = reader.read()) {
            System.out.print( (char) chr);
        }
    }

    public void close(){
        if(server != null) {
            try {
                server.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

One final touch on the server is the main method

    public static void main(String[] args) {
        Server server = new Server();

        try{
            server.initAndStart();
        }catch (Exception e){
            server.close();
            e.printStackTrace();
        }
    }

Now it’s the client’s turn. We’ll create the init method and then prepare the method that we’ll use to send the message.

    private Socket socket;

    public void init() throws Exception{
        socket = new Socket("127.0.0.1", 4040);
        System.out.println("Connecting to the server");
    }

    public void sendMessage(String msg) throws Exception{
        System.out.println("Sending the message to the server");
        PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
        writer.println(msg);
    }

Final touches on the client are the close and the main methods:

    public void close(){
        if(socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        Client client = new Client();

        try{
            client.init();
            client.sendMessage("Hello World");
        }catch (Exception ignored){
        }finally {
            client.close();
        }

    }

And voilĂ , the codes are ready! The two files should look like so:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    private ServerSocket server;

    public void initAndStart() throws Exception {
        server = new ServerSocket(4040);
        System.out.println("Server is running...");
        while (true) {
            Socket clientSocket = server.accept();
            System.out.println("Client is connected");
            readMessageFromSocket(clientSocket);
        }
    }

    private void readMessageFromSocket(Socket clientSocket) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        for (int chr = reader.read(); reader.ready(); chr = reader.read()) {
            System.out.print( (char) chr);
        }
    }

    public void close(){
        if(server != null) {
            try {
                server.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Server server = new Server();

        try{
            server.initAndStart();
        }catch (Exception e){
            server.close();
            e.printStackTrace();
        }
    }

}
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {

    private Socket socket;

    public void init() throws Exception{
        socket = new Socket("127.0.0.1", 4040);
        System.out.println("Connecting to the server");
    }

    public void sendMessage(String msg) throws Exception{
        System.out.println("Sending the message to the server");
        PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
        writer.println(msg);
    }

    public void close(){
        if(socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        Client client = new Client();

        try{
            client.init();
            client.sendMessage("Hello World");
        }catch (Exception ignored){
        }finally {
            client.close();
        }

    }


}

If you want to watch the video here it isss: