Request and Response between Client and Server using Socket Programming

Request and Response between Client and Server using Socket Programming

Develop Simple Application for Request and Response between Client and Server using Socket Programming

Answer :

  1. Server Application (Server.java): This will accept client requests, process them, and send a response back to the client.
  2. Client Application (Client.java): This will send requests to the server and receive responses.

Here’s how to implement it:

Server.java

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

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

            // Wait for client connection
            Socket clientSocket = serverSocket.accept();
            System.out.println("Client connected!");

            // Get input and output streams
            InputStream inputStream = clientSocket.getInputStream();
            OutputStream outputStream = clientSocket.getOutputStream();

            // Create buffered reader and writer for reading and sending data
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            PrintWriter writer = new PrintWriter(outputStream, true);

            // Read request from client
            String clientMessage = reader.readLine();
            System.out.println("Received from client: " + clientMessage);

            // Process the request and send a response
            String response = "Hello from Server: You said, '" + clientMessage + "'";
            writer.println(response);
            System.out.println("Response sent to client: " + response);

            // Close the streams and socket
            reader.close();
            writer.close();
            clientSocket.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Client.java

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

public class Client {
    public static void main(String[] args) {
        try {
            // Connect to the server at localhost and port 12345
            Socket socket = new Socket("localhost", 12345);
            System.out.println("Connected to server!");

            // Create input and output streams
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();

            // Create buffered reader and writer for reading and sending data
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            PrintWriter writer = new PrintWriter(outputStream, true);

            // Send request to server
            String message = "Hello Server!";
            writer.println(message);
            System.out.println("Sent to server: " + message);

            // Read response from server
            String serverResponse = reader.readLine();
            System.out.println("Received from server: " + serverResponse);

            // Close the streams and socket
            reader.close();
            writer.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Steps for Running the Application:

  1. Start the Server:
    • Open a terminal or command prompt.
    • Navigate to the directory containing Server.java.
    • Compile and run the server: javac Server.java java Server
    • The server will now be listening for client connections on port 12345.
  2. Start the Client:
    • Open another terminal or command prompt.
    • Navigate to the directory containing Client.java.
    • Compile and run the client: javac Client.java java Client
    • The client will connect to the server, send a message, and receive a response.

Notes:

  • Server: The Server listens on port 12345 for incoming client connections using ServerSocket. When a client connects, it creates a Socket to communicate with that client. It reads the message sent by the client and sends a response back.
  • Client: The Client connects to the server at localhost and port 12345. It sends a request and waits for the server’s response.