Guest User

Untitled

a guest
Jan 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. package Courses.CCN.Prog_Assn1;
  2.  
  3. import java.io.IOException;
  4. import java.net.DatagramPacket;
  5. import java.net.DatagramSocket;
  6. import java.net.InetAddress;
  7. import java.net.SocketException;
  8. import java.net.UnknownHostException;
  9.  
  10. public class PingClient {
  11.  
  12. /**
  13. * Client to send pings to a port
  14. * @throws SocketException
  15. */
  16. public static void main(String[] args) throws SocketException {
  17. if (args.length != 2) {
  18. System.out.println("Required arguments: <host-name> <port-number>");
  19. return;
  20. }
  21.  
  22. InetAddress address = null;
  23. try {
  24. address = InetAddress.getByName(args[0]);
  25. } catch (UnknownHostException e) {
  26. System.out.println("Hostname resolution error.");
  27. e.printStackTrace();
  28. }
  29. int port = Integer.parseInt(args[1]);
  30.  
  31. System.out.println(address);
  32. System.out.println(port);
  33.  
  34. DatagramPacket packet = new DatagramPacket(new byte[128], 128, address, port);
  35. DatagramSocket clientSocket = new DatagramSocket();
  36.  
  37. for(int i = 0; i <= 9; i++) {
  38. try {
  39. clientSocket.send(packet);
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }
  45.  
  46. }
Add Comment
Please, Sign In to add comment