Advertisement
fahimanzam

Socket Programming using Threading

Dec 12th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. public class Server {
  2.     private static int clientCount = 0;
  3.     private static final int MAX_CLIENTS = 5;
  4.    
  5.     public static void main(String[] args) throws IOException {
  6.         ServerSocket handshake =  new ServerSocket(4000);
  7.         System.out.println("Server started at: " + handshake.getLocalPort());
  8.         System.out.println("Waiting for Clients...");
  9.        
  10.         while (clientCount < MAX_CLIENTS) {
  11.             Socket socket = handshake.accept();
  12.             clientCount++;
  13.             System.out.println("A new client is connected" + socket);
  14.             DataOutputStream output_stream = new DataOutputStream(socket.getOutputStream());
  15.             DataInputStream input_stream = new DataInputStream(socket.getInputStream());
  16.             System.out.println("A new thread is assigning");
  17.             Thread new_tunnel = new ClientHandler(socket, input_stream, output_stream);
  18.             new_tunnel.start();
  19.         }
  20.        
  21.         System.out.println("Sever has reached it's maximum capacity. Shutting down!");
  22.         handshake.close();
  23.     }
  24. }
  25.  
  26. public class ClientHandler extends Thread {
  27.     final Socket com_tunnel;
  28.     final DataInputStream dis_tunnel;
  29.     final DataOutputStream dos_tunnel;
  30.  
  31.     public ClientHandler(Socket s, DataInputStream dis, DataOutputStream dos)
  32.     {
  33.         this.com_tunnel = s;
  34.         this.dis_tunnel = dis;
  35.         this.dos_tunnel = dos;
  36.     }
  37.  
  38.     public void run() {
  39.         while(true){
  40.             try {
  41.                 dos_tunnel.writeUTF("Send a number or type 'Exit' to disconnect:");
  42.                 String received = dis_tunnel.readUTF();
  43.                
  44.                 if(received.equalsIgnoreCase("Exit")){
  45.                     System.out.println("Client " + this.com_tunnel + " sends exit");
  46.                     System.out.println("Closing the connection");
  47.                     this.com_tunnel.close();
  48.                     break;
  49.                 }
  50.  
  51.                 try {
  52.                     int number = Integer.parseInt(received);
  53.                     List<Integer> primes = findPrimes(number);
  54.                     dos_tunnel.writeUTF("The prime numbers are: " + primes);
  55.                 } catch (NumberFormatException e) {
  56.                     dos_tunnel.writeUTF("Invalid input. Please send a valid number.");
  57.                 }
  58.  
  59.             } catch (IOException ex) {
  60.                 System.err.println("Error: " + ex.getMessage());
  61.                 break;
  62.             }
  63.         }
  64.         try {
  65.             this.dos_tunnel.close();
  66.             this.dis_tunnel.close();
  67.         } catch (IOException ex) {
  68.             System.err.println("Error closing resources: " + ex.getMessage());
  69.         }
  70.     }
  71.    
  72.     private List<Integer> findPrimes(int num) {
  73.         List<Integer> primes = new ArrayList<>();
  74.         for (int i = 2; i <= num; i++) {
  75.             if (isPrime(i)) {
  76.                 primes.add(i);
  77.             }
  78.         }
  79.         return primes;
  80.     }
  81.  
  82.     private boolean isPrime(int n) {
  83.         if (n <= 1) return false;
  84.         for (int i = 2; i <= Math.sqrt(n); i++) {
  85.             if (n % i == 0) return false;
  86.         }
  87.         return true;
  88.     }
  89. }
  90.  
  91. public class Client {
  92.     public static void main(String[] args)throws IOException {
  93.         try {
  94.             Socket clientSocket = new Socket("localhost", 4000);
  95.             System.out.println("Connected at server Handshaking port " + clientSocket.getPort());
  96.             System.out.println("Client is connecting at Communication Port " + clientSocket.getLocalPort());
  97.             System.out.println("Client is Connected");  
  98.  
  99.             Scanner scn = new Scanner(System.in);
  100.             DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
  101.             DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
  102.  
  103.             while(true){
  104.                 String inLine = dis.readUTF();
  105.                 System.out.println(inLine);
  106.                 String outLine = scn.nextLine();
  107.                 dos.writeUTF(outLine);
  108.  
  109.                 if(outLine.equalsIgnoreCase("Exit")){
  110.                     System.out.println("Closing the connection "+ clientSocket);
  111.                     clientSocket.close();
  112.                     System.out.println("Connection Closed");
  113.                     break;
  114.                 }
  115.                     String received = dis.readUTF();
  116.                     System.out.println(received);
  117.             }
  118.             dos.close();
  119.             dis.close();
  120.         } catch (Exception ex) {
  121.             System.err.println("Error: " + ex.getMessage());
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement