Created
April 1, 2015 14:28
-
-
Save pumpkincouture/a3fc299b454df8137edc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EchoClient { | |
public static void main(String[] args) throws IOException { | |
if (args.length != 2) { | |
System.err.println("Usage: java EchoClient <host name> <port number>"); | |
System.exit(1); | |
} | |
String hostName = args[0]; | |
int portNumber = Integer.parseInt(args[1]); | |
Socket echoSocket = new Socket(hostName, portNumber); | |
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true); | |
BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); | |
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); | |
while (true) { | |
String userInput; | |
while ((userInput = stdIn.readLine()) != null) { | |
out.println(userInput); | |
System.out.println("echo: " + in.readLine()); | |
} | |
} | |
} | |
} | |
public class EchoServer { | |
public static void main(String[] args) throws IOException { | |
if (args.length != 1) { | |
System.err.println("Usage: java EchoServer <port number>"); | |
System.exit(1); | |
} | |
int portNumber = Integer.parseInt(args[0]); | |
ServerSocket serverSocket = new ServerSocket(portNumber); | |
Socket clientSocket = serverSocket.accept(); | |
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); | |
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | |
while (true) { | |
String inputLine; | |
while ((inputLine = in.readLine()) != null) { | |
System.out.println("INFO: " + inputLine); | |
out.println(inputLine); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment