Java Reference
In-Depth Information
*/
public
public static
static void
void main ( String [] argv ) throws
throws IOException {
iif ( 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
new DatagramSocket ();
//sock.connect(servAddr, DAYTIME_PORT);
byte
byte [] buffer = new
new byte
byte [ PACKET_SIZE ];
// The udp packet we will send and receive
DatagramPacket packet = new
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 );
System . out . println ( "Sent request" );
// Receive a packet and print it.
sock . receive ( packet );
System . out . println ( "Got packet of size " + packet . getLength ());
System . out . print ( "Date on " + host + " is " +
new
new String ( buffer , 0 , packet . getLength ()));
sock . close ();
}
}
I'll run it to my Unix box just to be sure that it works:
$
$ java network.DaytimeUDP dalai
Sent request
Got packet of size 26
Date on dalai is Sat Feb 8 20:22:12 2014
$
Search WWH ::




Custom Search