Blog

Client and server programs in java which a server program accepts the length and breadth of a rectangle from the client program

Multi Client -server Program in Java

Write client and server programs in java which a server program accepts the length and breadth of a rectangle from the client program, computes area, sends the computed area to the client program, and displays it by client program. The server should be able to handle multiple clients.

Server Program : Client Server Program

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 *
 * @author achyut1324
 */
public class ThreadServer2 {
    public static void main(String[] args) throws IOException {
        final int PORT = 4041;
        ServerSocket serverSocket = new ServerSocket(PORT);
        
        System.out.println("Server started...");
        System.out.println("Wating for clients...");
        
        while (true) {
            Socket clientSocket = serverSocket.accept();
            Thread t = new Thread() {
                public void run() {
                    try (
                        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                        Scanner in = new Scanner(clientSocket.getInputStream());
                    ) {
                        while (in.hasNextLine()) {
                     
                            String length = in.nextLine();
                            String breadth = in.nextLine();
                            
                            if (length.equalsIgnoreCase("exit")) {
                                break;
                            }
                          System.out.println("Length Received from client: " + length);  
                          System.out.println("Breadth Received from client: " + breadth);  
                            
                            double l = Double.valueOf(length);
                            double b = Double.valueOf(breadth);
                            double area = l*b ;
                            out.println(area);
                        }
                    } catch (IOException e) { }
                }
            };
            t.start();
        }
    }   
}

Client Program – Client server Program

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 *
 * @author achyut1324
 */
public class ThreadClient2 {
    public static void main(String[] args) throws IOException {
        final String HOST = "127.0.0.1";
        final int PORT = 4041;
        
        System.out.println("Client started.");
        
        try (
            Socket socket = new Socket(HOST, PORT);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            Scanner in = new Scanner(socket.getInputStream());
            Scanner s = new Scanner(System.in);
        ) {
            while (true) {
                System.out.print("Input: Lenghth and Breadth ");
                String length = s.nextLine();
                String breath = s.nextLine();
                out.println(length);
                out.println(breath);
                System.out.println("Echoed from server: " + in.nextLine());
            }
        }
    }
    
}

Output of the Program :

Server
Client 1
Client 2