Advertisement
Josif_tepe

Untitled

Mar 31st, 2022
1,260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.DatagramPacket;
  6. import java.net.DatagramSocket;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.util.Scanner;
  10.  
  11. public class Server {
  12.     public static void main(String[] args) {
  13.         try {
  14.             DatagramSocket datagramSocket = new DatagramSocket(1234);
  15.             byte[] recieved_message = new byte[6000];
  16.             DatagramPacket datagramPacket = null;
  17.             while(true) {
  18.                 datagramPacket = new DatagramPacket(recieved_message, recieved_message.length);
  19.                 datagramSocket.receive(datagramPacket);
  20.                 String s = cast(recieved_message).toString();
  21.                 System.out.println(s);
  22.                 if(s.charAt(0) == 'E' && s.charAt(1) == 'N' && s.charAt(2) == 'D') {
  23.                     return;
  24.                 }
  25.                 recieved_message = new byte[6000];
  26.             }
  27.         }
  28.         catch (IOException i) {
  29.             i.printStackTrace();
  30.         }
  31.     }
  32.     private static StringBuilder cast(byte[] b) {
  33.         StringBuilder s = new StringBuilder();
  34.         for(int i = 0; i < b.length; i++) {
  35.             s.append((char) b[i]);
  36.         }
  37.         return s;
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement