Echo Client : Socket Client « Network « Java Tutorial






import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class MainClass {
  public static void main(String[] args) throws Exception {
    String hostname = "localhost";

    Socket theSocket = new Socket(hostname, 7);
    BufferedReader networkIn = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
    BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(theSocket.getOutputStream());
    System.out.println("Connected to echo server");

    while (true) {
      String theLine = userIn.readLine();
      if (theLine.equals("."))
        break;
      out.println(theLine);
      out.flush();
      System.out.println(networkIn.readLine());
    }
    networkIn.close();
    out.close();
  }
}








19.12.Socket Client
19.12.1.Create a socket without a timeout
19.12.2.Create a socket with a timeout
19.12.3.Read and write through socket
19.12.4.Send string to each connected client
19.12.5.Daytime Client
19.12.6.Echo Client
19.12.7.Whois Client
19.12.8.Time Client
19.12.9.Finger Client
19.12.10.Compressed socket
19.12.11.Day time Client (Getting the time)
19.12.12.Zip socket