FileDocCategorySizeDatePackage
DaytimeUDP.javaAPI DocExample1416Sun Feb 15 20:44:44 GMT 2004None

DaytimeUDP

public class DaytimeUDP extends Object
Simple UDP client - contact the standard ascii time service
author
Ian Darwin, http://www.darwinsys.com/.

Fields Summary
public static final int
DAYTIME_PORT
The UDP port number
protected static final int
PACKET_SIZE
A buffer plenty big enough for the date string
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] argv)
The main program that drives this network client.

param
argv[0] hostname, running daytime/udp server


	              	 
	       
		if (argv.length < 1) {
			System.err.println("usage: java DayTimeUDP host");
			System.exit(1);
		}
		String host = argv[0];
		InetAddress servAddr = InetAddress.getByName(host);
		DatagramSocket sock = new DatagramSocket();
		//sock.connect(servAddr, DAYTIME_PORT);
		byte[] buffer = new byte[PACKET_SIZE];

		// The udp packet we will send and receive
		DatagramPacket packet = new DatagramPacket(
			buffer, PACKET_SIZE, servAddr, DAYTIME_PORT);

		/* Send empty max-length (-1 for null byte) packet to server */
		packet.setLength(PACKET_SIZE-1);
		sock.send(packet);
		Debug.println("net", "Sent request");

		// Receive a packet and print it.
		sock.receive(packet);
		Debug.println("net", "Got packet of size " + packet.getLength());
		System.out.print("Date on " + host + " is " + 
			new String(buffer, 0, packet.getLength()));