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

UDPPoke.java

import java.net.*;
import java.io.*;

public class UDPPoke {

  protected static int defaultPort = 0;
  protected static int bufferLength = 8192;

  public static void main(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

  }  // end main

}