Source of EchoClient.java


  1: import java.io.*;
  2: import java.net.*;

  4: public class EchoClient {
  5:   public static void main(String[] args) {
  6:     try {
  7:       String host;
  8:       if (args.length>0) {
  9:         host=args[0];
 10:       } else {
 11:         host="localhost";
 12:       }

 14:       Socket socket=new Socket(host, 8008);
 15:       BufferedReader in
 16:           = new BufferedReader(new InputStreamReader(socket.getInputStream()));

 18:       PrintWriter out
 19:           = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

 21:       // send data to the server
 22:       for (int i=1; i<=10; i++) {
 23:         System.out.println("Sending: line "+i);
 24:         out.println("line "+i);
 25:         out.flush();
 26:       }
 27:       out.println("BYE");
 28:       out.flush();

 30:       // receive data from the server
 31:       while (true) {
 32:         String str=in.readLine();
 33:         if (str==null) {
 34:           break;
 35:         } else {
 36:           System.out.print(str);
 37:         }
 38:       }
 39:     } catch (Exception e) {
 40:       e.printStackTrace();
 41:     }
 42:   }
 43: }