Guest User

Untitled

a guest
Apr 29th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. public class MultiThreadServer {
  2.  
  3.     public static void main (String [] args) throws Exception {
  4.  
  5.         DatagramSocket serverSocket = new DatagramSocket(6666);
  6.  
  7.         while (true) {
  8.             byte[] receiveData = new byte[1024];
  9.             byte[] sendData = new byte[1024];
  10.  
  11.             DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  12.             serverSocket.receive(receivePacket);
  13.  
  14.             String message = new String(receivePacket.getData());
  15.             InetAddress clientAddress = receivePacket.getAddress();
  16.             int clientPort = receivePacket.getPort();
  17.  
  18.             // Create a new thread to handle the client connection
  19.             Thread t = new Thread(new ClientHandler(clientAddress, clientPort));
  20.             t.start();
  21.         }
  22.     }
  23. }
  24.  
  25. public class ClientHandler implements Runnable {
  26.     private static List<Integer> usedPorts = new ArrayList<>();
  27.     private DatagramSocket socket;
  28.     private InetAddress clientAddress;
  29.     private int clientPort;
  30.  
  31.     public ClientHandler(InetAddress clientAddress, int clientPort) throws Exception {
  32.         this.clientAddress = clientAddress;
  33.         this.clientPort = clientPort;
  34.         this.socket = getSocket();
  35.     }
  36.  
  37.     public DatagramSocket getSocket() throws SocketException {
  38.         int port = 0;
  39.         do {
  40.             port = 6666 + (int)(Math.random()*2222);
  41.         } while(usedPorts.contains(port));
  42.  
  43.         return new DatagramSocket(port);
  44.     }
  45.  
  46.     public void run() {
  47.         try {
  48.             send("HELLO");
  49.  
  50.             while(true) {
  51.                 String text  = new String(receive());
  52.                 send(text);
  53.             }
  54.         } catch (Exception e) {
  55.             e.printStackTrace();
  56.         }
  57.     }
  58.  
  59.     private void send(String msg) throws IOException {
  60.         DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.getBytes().length, clientAddress, clientPort);
  61.         socket.send(packet);
  62.     }
  63.  
  64.     private byte[] receive() throws IOException {
  65.         byte[] buffer = new byte[1024];
  66.         DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  67.         socket.receive(packet);
  68.  
  69.         return packet.getData();
  70.     }
  71. }
  72.  
  73.  
  74. public class Client {
  75.     private int myPort = 4444+(int)(Math.random()*4444); // to be able to run few clients on same OS
  76.     private int remotePort = 6666;
  77.     private DatagramSocket socket;
  78.  
  79.  
  80.     public static void main(String[] args) throws IOException {
  81.         new Client().go();
  82.     }
  83.  
  84.     private void go() throws IOException {
  85.         System.out.println("My port is " + myPort);
  86.         this.socket = new DatagramSocket(myPort);
  87.         remotePort = getConnection(socket);
  88.         System.out.println("Received remote port is " + remotePort);
  89.  
  90.         Scanner scanner = new Scanner(System.in);
  91.         String line = scanner.nextLine();
  92.         while (!line.equals("q")) {
  93.  
  94.             send(line);
  95.  
  96.             String response = new String(receive());
  97.             System.out.println(response);
  98.  
  99.             line = scanner.nextLine();
  100.         }
  101.  
  102.         socket.close();
  103.     }
  104.  
  105.     private int getConnection(DatagramSocket socket) throws IOException {
  106.         // sending first message on server
  107.         String msg = "HELLO";
  108.         InetAddress remoteAddress = InetAddress.getLocalHost();
  109.         DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.getBytes().length, remoteAddress, remotePort);
  110.         socket.send(packet);
  111.  
  112.         // getting first message from server and returning the port
  113.         byte[] buffer = new byte[1024];
  114.         DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
  115.         socket.receive(receivePacket);
  116.  
  117.         return receivePacket.getPort();
  118.     }
  119.  
  120.     private void send(String msg) throws IOException {
  121.         DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.getBytes().length, InetAddress.getLocalHost(), remotePort);
  122.         socket.send(packet);
  123.     }
  124.  
  125.     private byte[] receive() throws IOException {
  126.         byte[] buffer = new byte[1024];
  127.         DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  128.         socket.receive(packet);
  129.  
  130.         return packet.getData();
  131.     }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment