import java.net.* ; import java.util.* ; public class EchoClient { static final int ECHOPORT = 8000; static final String LOCALSERVER = "localhost"; public static void main(String[] args) throws Exception { if( args.length > 1 ) { System.err.printf("usage: java EchoClient [server_computer] \n") ; System.exit(0); } // Get server IP address and create an UDP socket String server = LOCALSERVER; if( args.length == 1) server = args[0]; InetAddress serverAddress = InetAddress.getByName( server ) ; DatagramSocket socket = new DatagramSocket(); // Read a message from the input console Scanner in = new Scanner( System.in ); System.out.printf("Echo client - please type a messsage: ") ; String request = in.nextLine(); // Prepare a datagram to send to the server in a compact way // send it, wait for the reply, print it DatagramPacket datagram = new DatagramPacket( request.getBytes(), request.length(), serverAddress, ECHOPORT ) ; socket.send( datagram ); socket.receive( datagram ); socket.close() ; System.out.printf("Got: \"%s\"\n", new String( datagram.getData(), 0, datagram.getLength() ) ) ; } // main }