// This example is from the book _Java in a Nutshell_ by David Flanagan. // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. // You may study, use, modify, and distribute this example for any purpose. // This example is provided WITHOUT WARRANTY either expressed or implied. import java.io.*; import java.net.*; // This class sends the specified text as a datagram to port 6010 of the // specified host. public class UDPSend { static final int port = 6010; public static void main(String args[]) throws Exception { if (args.length != 2) { System.out.println("Usage: java UDPSend "); System.exit(0); } // Get the internet address of the specified host InetAddress address = InetAddress.getByName(args[0]); // Convert the message to an array of bytes int msglen = args[1].length(); byte[] message = new byte[msglen]; args[1].getBytes(0, msglen, message, 0); // Initilize the packet with data and address DatagramPacket packet = new DatagramPacket(message, msglen, address, port); // Create a socket, and send the packet through it. DatagramSocket socket = new DatagramSocket(); socket.send(packet); } }