Guest User

Untitled

a guest
Jan 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. public class PingClient
  6. {
  7. public static void main(String[] args)
  8. {
  9. int size = 64;
  10. int port;
  11. byte[] sendBuf = new byte[size];
  12. Scanner scan = new Scanner(System.in);
  13.  
  14.  
  15. if(args.length != 1)
  16. {
  17. System.out.println("For use with PingServer Client");
  18. return;
  19. }
  20.  
  21. System.out.println("Enter packet size(default = 64): ");
  22. size = scan.nextInt();
  23.  
  24. System.out.println("Enter server Port(default = 4445): ");
  25. port = scan.nextInt();
  26.  
  27. DatagramSocket socket = new DatagramSocket();
  28. byte[] buf = new byte[size];
  29. InetAddress address = InetAddress.getByName(args[0]);
  30. DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port);
  31.  
  32. socket.send(packet);
  33.  
  34. packet = new DatagramPacket(buf, buf.length);
  35. socket.receive(packet);
  36. String received = new String(packet.getData(), 0, packet.getLength());
  37. System.out.println(received);
  38. }
  39. }
Add Comment
Please, Sign In to add comment