EchoClient.java (1053B)
1 import java.net.*; 2 import java.io.*; 3 4 public class EchoClient { 5 public static void main(String[] args) throws IOException { 6 Socket echosock; 7 PrintWriter out; 8 BufferedReader in, stdin; 9 String input; 10 String hostname = "127.0.0.1"; 11 int port = 7777; 12 13 if (args.length == 2) { 14 hostname = args[0]; 15 port = Integer.parseInt(args[1]); 16 } else 17 System.err.println("usage: java EchoClient [hostname] [port]"); 18 try { 19 echosock = new Socket(hostname, port); 20 out = new PrintWriter(echosock.getOutputStream(), true); 21 in = new BufferedReader(new InputStreamReader( 22 echosock.getInputStream())); 23 stdin = new BufferedReader(new InputStreamReader(System.in)); 24 25 System.out.print("> "); 26 while ((input = stdin.readLine()) != null) { 27 out.println(input); 28 System.out.println("echo: " + in.readLine()); 29 System.out.print("> "); 30 } 31 } catch (UnknownHostException e) { 32 System.err.println("unknown host: " + hostname); 33 } catch (IOException e) { 34 System.err.println("io exception: " + e.getMessage()); 35 } 36 } 37 }