/** * UdpClientDemo13 shows typical UDP/IP client programming in Java. It * sends an empty packet to UDP port 13 on another machine, waits to * receive one packet, prints it and quits. * /* Copyright (c) 1999-2002 E. W. Grundke. All rights reserved. * @author E. W. Grundke */ import java.io.*; import java.net.*; public class UdpClientDemo13 { private final static int PORT=13; // the time-of-day port public static void main (String args[]) { BufferedReader input=null; if (args.length < 1) { // must have 1+ command-line parameters System.out.println("Usage: java UdpClientDemo13 "); System.out.println("Example: java UdpClientDemo13 borg.cs.dal.ca"); System.out.println("Example: java UdpClientDemo13 129.173.66.61"); System.exit(1); } try { DatagramSocket s = new DatagramSocket(); InetAddress addr = InetAddress.getByName(args[0]); byte[] sendBuffer = new byte[0]; // send 0 bytes! DatagramPacket pkt = new DatagramPacket(sendBuffer, 0, addr, PORT); s.send(pkt); byte[] rbuffer = new byte[1500]; DatagramPacket rpkt = new DatagramPacket(rbuffer, rbuffer.length); s.receive(rpkt); System.out.println ("Received " + rpkt.getLength() + " bytes" + " from host " + rpkt.getAddress().getHostName() + " port " + rpkt.getPort() + ":"); System.out.println(new String(rpkt.getData())); } catch (Exception e) {System.out.println(e);} } }