Skip to content

Instantly share code, notes, and snippets.

@opiethehokie
Created March 8, 2014 17:34
Show Gist options
  • Select an option

  • Save opiethehokie/9435533 to your computer and use it in GitHub Desktop.

Select an option

Save opiethehokie/9435533 to your computer and use it in GitHub Desktop.
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MultiThreadedSyncServer {
public static void main(String[] args) throws IOException {
final ServerSocket listener = new ServerSocket(8080);
while (true) {
new EchoThread(listener.accept()).start();
}
}
private static class EchoThread extends Thread {
private Socket connection;
public EchoThread(Socket socket) {
this.connection = socket;
}
@Override
public void run() {
try (final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
final PrintWriter out = new PrintWriter(connection.getOutputStream(), true)) {
out.println(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment