Advertisement
Josif_tepe

Untitled

Apr 23rd, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. import java.net.DatagramPacket;
  2. import java.net.DatagramSocket;
  3. import java.sql.SQLSyntaxErrorException;
  4.  
  5. public class Server {
  6.     public static void main(String[] args) {
  7.         try {
  8.             DatagramSocket datagramSocket = new DatagramSocket(4567);
  9.             byte[] recieved_message = new byte[60000];
  10.             DatagramPacket datagramPacket = null;
  11.             while(true) {
  12.                 datagramPacket = new DatagramPacket(recieved_message, recieved_message.length);
  13.                 datagramSocket.receive(datagramPacket);
  14.                 String s = from_bytes_to_string(recieved_message).toString();
  15.  
  16.                 System.out.println(s);
  17.                 if(s.charAt(0) == 'E' && s.charAt(1) == 'N' && s.charAt(2) == 'D') {
  18.                     return;
  19.                 }
  20.                 recieved_message = new byte[60000];
  21.             }
  22.         }
  23.         catch (Exception e) {
  24.             e.printStackTrace();
  25.         }
  26.  
  27.     }
  28.     public static StringBuilder from_bytes_to_string(byte[] b) {
  29.         StringBuilder s = new StringBuilder();
  30.         for(int i = 0; i < b.length; i++) {
  31.            s.append((char)b[i]);
  32.         }
  33.         return s;
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement