Advertisement
Mohammad_Daoud

UDP Client pojo

Aug 23rd, 2021
1,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. package homeworks.requestUDP;
  2. /**
  3.  * @TEAM_MEMBERS:
  4.  * MOHAMMAD ABDALLAH RAJA DAOUD - 0173632
  5.  * HAMZA MONTHER MOSTAFA AMIRAH - 0189136
  6.  *
  7.  * @instructor:
  8.  * Dr. Wesam Al Mobaideen
  9.  */
  10. import java.io.IOException;
  11. import java.net.DatagramPacket;
  12. import java.net.DatagramSocket;
  13. import java.net.InetAddress;
  14. import java.nio.charset.StandardCharsets;
  15. import java.util.Scanner;
  16.  
  17.  
  18. public class Client {
  19.  
  20.     public static void main(String[] args) throws IOException {
  21.         DatagramSocket client = new DatagramSocket();// create client udp socket
  22.         InetAddress ip = InetAddress.getLocalHost();
  23.         Scanner in = new Scanner(System.in);
  24.         System.out.println("You are on device: " + ip);
  25.         System.out.println("Enter the RequestType then the id\n"+
  26.                 "Example : \n" +
  27.                 "************\n" +
  28.                 "Name 0123456\n" +
  29.                 "************\n\n" +
  30.                 "PRESS ENTER TO CONTINUE . . .");
  31.  
  32.         byte data[];
  33.         String toParse = in.nextLine();
  34.         data = toParse.getBytes();
  35.         DatagramPacket to_send = new DatagramPacket(data, data.length, ip, 5555);////for sending packets
  36.         DatagramPacket to_receive ;//for receiving packets
  37.         client.send(to_send);
  38.         String id= "";
  39.         String requestInfo ;
  40.         while (true) {
  41.  
  42.             System.out.print("> ");
  43.             toParse = in.nextLine();
  44.             data = toParse.getBytes();
  45.             to_send = new DatagramPacket(data, data.length, ip, 5555);
  46.             client.send(to_send);
  47.             System.out.println("Sent!");
  48.             if(toParse.equalsIgnoreCase("end")) break;
  49.  
  50.             data = new byte[4546];
  51.             to_receive = new DatagramPacket(data, data.length);
  52.             client.receive(to_receive);
  53.             toParse = new String(to_receive.getData(), StandardCharsets.UTF_8).trim();
  54.  
  55.             // TODO: Check if there is such id, if not print the warning message from the server
  56.             try {
  57.                 id = toParse.substring(toParse.indexOf(':') + 1, toParse.indexOf(','));
  58.             }catch (NumberFormatException e) {
  59.                 System.out.println(to_receive.getSocketAddress() + " --> "+ id);
  60.                 continue;
  61.  
  62.             }catch (StringIndexOutOfBoundsException e){
  63.                 System.out.println(toParse);
  64.                 continue;
  65.             }
  66.             /**
  67.                 taking requested information and id by '|' as a substrings from server message (toParse)
  68.                 string form:
  69.                     requestedInformation:RequestedInfo, StudentNumber:id
  70.                 after split:
  71.                     requestedInformation:|RequestedInfo|, StudentNumber:|id|
  72.                     |RequestedInfo| |id|
  73.             */
  74.             requestInfo = toParse.substring(toParse.lastIndexOf(':') + 1, toParse.length());
  75.  
  76.             //TODO: print output in requested form studentNumber/requestedInformation
  77.             System.out.println(to_receive.getSocketAddress() + " --> "+ id + "/ " + requestInfo);
  78.         }
  79.         client.close();
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement