Java Language – 55 – Server-Client Communication

Networking – Server-Client Communication
Introduction to Server-Client Communication

Server-client communication is a fundamental aspect of network programming. It allows computers to interact over a network, exchanging data and performing various tasks. In Java, you can implement server-client communication using sockets and streams. This guide provides an overview of the key concepts and a simple example of a Java client communicating with a server.

Sockets and Streams

Sockets are the basic building blocks for network communication in Java. A socket is a software endpoint for sending or receiving data. In server-client communication, a server waits for incoming client connections, and clients initiate connections to servers. Communication between the server and client is achieved using input and output streams, which provide a way to transmit data in both directions.

Server-Client Model

In the server-client model, the server is a program that listens for incoming connections, while clients are programs that initiate connections to the server. When a client connects to the server, they can exchange data. This model is widely used for various applications, such as web servers, chat applications, and online gaming.

Server Code

To create a Java server, you can use the ServerSocket class. The server listens on a specific port, waiting for client connections. When a client connects, the server creates a socket for communication.


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

public class Server {
    public static void main(String[] args) {
        try {
            // Create a ServerSocket that listens on port 12345
            ServerSocket serverSocket = new ServerSocket(12345);
            System.out.println("Server is waiting for clients...");

            while (true) {
                // Accept incoming client connections
                Socket clientSocket = serverSocket.accept();
                System.out.println("Client connected from " + clientSocket.getInetAddress());

                // Create input and output streams for communication
                PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                out.println("Hello, Client!");
                out.close();

                // Close the client socket
                clientSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Client Code

A Java client connects to the server using the Socket class. It specifies the server’s hostname and port to establish a connection. Once connected, the client can send and receive data.


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

public class Client {
    public static void main(String[] args) {
        try {
            // Create a client socket and connect to the server
            Socket socket = new Socket("localhost", 12345);

            // Create an input stream to read data from the server
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String message = in.readLine();
            System.out.println("Message from server: " + message);

            // Close the socket
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Conclusion

Server-client communication is a crucial aspect of network programming. Understanding sockets and streams, as demonstrated in the examples above, is essential for building applications that rely on network interactions. Whether you’re developing a chat application, an online game, or a file transfer system, mastering server-client communication is a valuable skill for Java developers.