N. Achyut java,Network Programming,Uncategorized Multi Client -server Program in Java | Serving Multiple Clients program in Java

Multi Client -server Program in Java | Serving Multiple Clients program in Java

Multi Client -server Program in Java

Multi Client -server Program in Java

  • Previous implementation of TCP Echo Server allows only one client to connect at a time To allow multiple clients to connect, use threads for each client connection in the server
  • We can build chat connection in java between two client using thread and multi client

TCP Socket

  • A socket is one endpoint of a two-way communication link between two programs running on the network
  • A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to

Server Side Program | Multi Client -server Program in Java

/*
 * 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 5442
 */
public class ThreadServer {
  public static void main(String[] args) throws IOException {
        final int PORT = 4040;
        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 input = in.nextLine();
                            if (input.equalsIgnoreCase("exit")) {
                                break;
                            }
                            System.out.println("Received radius from client: " + input);
                            
                            double radius = Double.valueOf(input);
                            double area = Math.PI* radius *radius ;
                            out.println(area);
                        }
                    } catch (IOException e) { }
                }
            };
            t.start();
        }
    }   
}

Server Side Program | Multi Client -server Program in Java

/*
 * 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 5442
 */
public class ThreadClient {
    public static void main(String[] args) throws IOException {
        final String HOST = "127.0.0.1";
        final int PORT = 4040;
        
        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: ");
                String input = s.nextLine();
                out.println(input);
                if (input.equalsIgnoreCase("exit")) break;
                System.out.println("Echoed from server: " + in.nextLine());
            }
        }
    }
 
}

Download the File of Multi- Client Server Program in java

Related Post