import java.net.*; import java.io.*; import java.util.*; /** * This example illustrates the basic method calls for connectionless * datagram socket. * @author M. L. Liu */ public class Example1Sender { // An application which sends a message using connectionless // datagram socket. // Two command line arguments are expected, in order: // // public static void main(String[] args) { if (args.length != 2) System.out.println ("This program requires three command line arguments"); else { try { Scanner sc = new Scanner(System.in); InetAddress receiverHost = InetAddress.getByName(args[0]); int receiverPort = Integer.parseInt(args[1]); System.out.println("Please enter messages to send."); String message = sc.nextLine(); DatagramSocket mySocket = new DatagramSocket(); while(!message.equals("bye")) { // instantiates a datagram socket for sending the data byte[ ] buffer = message.getBytes( ); DatagramPacket datagram = new DatagramPacket(buffer, buffer.length, receiverHost, receiverPort); mySocket.send(datagram); message = sc.nextLine(); } byte [] buffer = message.getBytes(); DatagramPacket datagram = new DatagramPacket(buffer, buffer.length, receiverHost, receiverPort); mySocket.send(datagram); mySocket.close( ); } // end try catch (Exception ex) { ex.printStackTrace( ); } } // end else } // end main } // end class