FileDocCategorySizeDatePackage
UDPPoke.javaAPI DocExample1315Thu Apr 03 15:25:56 BST 1997None

UDPPoke

public class UDPPoke extends Object

Fields Summary
protected static int
defaultPort
protected static int
bufferLength
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)


       

    String hostname;
    int port;
    int len;

    if (args.length > 0) {
      hostname = args[0];
    }
    else {
      hostname = "localhost";
      port = defaultPort;
      len = bufferLength;
    }
    try {
      port = Integer.parseInt(args[1]);
    }
    catch (Exception e) {
      port = defaultPort;
    }
    try {
      len = Integer.parseInt(args[2]);
    }
    catch (Exception e) {
      len = bufferLength;
    }

    try {
      DatagramSocket ds = new DatagramSocket(0);
      InetAddress ia = InetAddress.getByName(hostname);
      DatagramPacket outgoing = new DatagramPacket(new byte[512], 1, ia, port);
      DatagramPacket incoming = new DatagramPacket(new byte[len], len);
      ds.send(outgoing);
      ds.receive(incoming);
      System.out.println(new String(incoming.getData(), 0, 0, incoming.getLength()));
    }  // end try
    catch (UnknownHostException e) {
      System.err.println(e);
    }  // end catch
    catch (SocketException e) {
      System.err.println(e);
    }  // end catch
    catch (IOException e) {
      System.err.println(e);
    }  // end catch