Client-server communication in Java


Client-server communication in Java involves the exchange of data between a client program and a server program over a network connection. The client sends requests to the server and receives responses, while the server processes the requests and sends back responses.

Here is an example of a simple client-server communication program in Java:

Server program

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(8000);
            System.out.println("Server started");

            while (true) {
                Socket socket = server.accept();
                System.out.println("Client connected: " + socket);

                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
                String message = in.readLine();
                System.out.println("Message from client: " + message);
                out.println("Hello from server");

                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we create a ServerSocket object on port 8000 and wait for incoming connections using the accept() method. When a client connects, we create a Socket object and get the input and output streams for sending and receiving data. We then read a message from the client using a BufferedReader and send a response using a PrintWriter. Finally, we close the socket.

Client program

import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 8000);
            System.out.println("Connected to server");

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
            out.println("Hello from client");

            String message = in.readLine();
            System.out.println("Message from server: " + message);

            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we create a Socket object and connect it to the server using the server’s hostname and port number. We then get the input and output streams and send a message to the server using a PrintWriter. We read the server’s response using a BufferedReader and close the socket.

This is a simple example of how client-server communication can be achieved in Java. More complex applications can use protocols such as HTTP, FTP, or SMTP to exchange data between the client and server. Java provides libraries such as the java.net package for building network applications that can communicate across different machines and platforms.

A quick recap of Java