
import java.net.*;

/**
 * This example illustrates a process which sends then receives
 * using a datagram socket.
 * @author M. L. Liu
 */
public class Example2SenderReceiver_name {
// An application which sends then receives a message using
// connectionless datagram socket.
// Four command line arguments are expected, in order: 
//    <domain name or IP address of the receiver>
//    <port number of the receiver's datagram socket>
//    <port number of this process's datagram socket>
//    <message, a string, to send>

   public static void main(String[] args) {
      if (args.length != 4)
         System.out.println
            ("This program requires four command line arguments");
      else {
         try {      
  	    int myPort = Integer.parseInt(args[1]);
	    Contact contact = new Contact(args[2]);
  	    InetAddress receiverHost = InetAddress.getByName(contact.host());
            int receiverPort = Integer.parseInt(contact.port());	
            String message = args[3];
   	      MyDatagramSocket mySocket = new MyDatagramSocket(myPort);  
               // instantiates a datagram socket for both sending
               // and receiving data
            mySocket.sendMessage( receiverHost, receiverPort, message);
				// now wait to receive a datagram from the socket
            System.out.println(mySocket.receiveMessage());
				mySocket.close( );
         } // end try
	      catch (Exception ex) {
            ex.printStackTrace( );
	      } //end catch
      } //end else
   } //end main 

} //end class
