Skip to content

Instantly share code, notes, and snippets.

@pumpkincouture
Created April 1, 2015 14:28
Show Gist options
  • Save pumpkincouture/a3fc299b454df8137edc to your computer and use it in GitHub Desktop.
Save pumpkincouture/a3fc299b454df8137edc to your computer and use it in GitHub Desktop.
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