Server.java

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

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(8080); // 8080 is the port number
            System.out.println("Server is running... waiting for client to connect");
            Socket socket = server.accept();// wait for client to connect
            System.out.println("Client connected" + socket.getInetAddress().getHostAddress());// get the ip address of the client
            server.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Cilent.java

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Cilent {
    public static void main(String[] args) {
    try(Socket socket = new Socket()) {
        socket.connect(new InetSocketAddress("localhost", 8080));
        System.out.println("Connected to server");
    }catch (IOException e) {
        e.printStackTrace(); //
    }
    }
}