FileDocCategorySizeDatePackage
ThreadUDPPoke.javaAPI DocExample2979Sun Dec 12 10:53:48 GMT 2004None

UDPPoke

public class UDPPoke extends Object

Fields Summary
private int
bufferSize
private int
timeout
private DatagramSocket
ds
private DatagramPacket
outgoing
Constructors Summary
public UDPPoke(InetAddress host, int port, int bufferSize, int timeout)

    outgoing = new DatagramPacket(new byte[1], 1, host, port);
    this.timeout = timeout;
    this.bufferSize = bufferSize;
    ds = new DatagramSocket(0);
    ds.connect(host, port); // requires Java 2
  
public UDPPoke(InetAddress host, int port, int bufferSize)

    this(host, port, bufferSize, 120000);
  
public UDPPoke(InetAddress host, int port)

    this(host, port, 8192, 120000);
  
Methods Summary
public static voidmain(java.lang.String[] args)


    InetAddress host;
    int port = 0;

    try {
      host = InetAddress.getByName(args[0]); 
      port = Integer.parseInt(args[1]);
      if (port < 0 || port > 65535) throw new Exception();
    }
    catch (Exception e) {
      System.out.println("Usage: java UDPPoke host port");
      return;
    }

    try {
      UDPPoke poker = new UDPPoke(host, port);
      byte[] response = poker.poke();
      if (response == null) {
      	System.out.println("No response within allotted time");
      	return;
      }
      String result = "";
      try {
        result = new String(response, "ASCII");
      }
      catch (UnsupportedEncodingException e) {
      	// try a different encoding
      	result = new String(response, "8859_1");
      }
      System.out.println(result);
    }
    catch (Exception e) {
      System.err.println(e);	
      e.printStackTrace();
    }

  
public byte[]poke()

  	
    PokeThread pt = new PokeThread();
    pt.start();
    try {
      pt.join(timeout);
    }
    catch (InterruptedException e) {
    } 
    pt.stop();
    // may return null 
    return pt.getResponse();