Skip to content

Instantly share code, notes, and snippets.

@lemoce
Created April 14, 2018 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemoce/ecbc4984e2e8170dcd3a39d36ade19b1 to your computer and use it in GitHub Desktop.
Save lemoce/ecbc4984e2e8170dcd3a39d36ade19b1 to your computer and use it in GitHub Desktop.
Multithread Echo Server
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.net.Socket;
import java.net.ServerSocket;
import java.util.List;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
public class Teste {
public static void main(String[] args) throws Exception {
Listener listener = new Listener();
listener.setDaemon(true);
listener.start();
try {
System.out.println("esperando servidor iniciar");
TimeUnit.SECONDS.sleep(30);
} catch (Exception ex) { }
listener.join();
}
}
class Listener extends Thread {
final List<Socket> clientes = new LinkedList<Socket>();
private final ReentrantLock lock = new ReentrantLock();
public void run() {
try (ServerSocket ss = new ServerSocket(10113)){
System.out.println("Iniciei servidor");
System.out.println("Adicionando hooks de finalizacao");
Runtime.getRuntime().addShutdownHook(new ShutdownHook(this));
while(true) {
try {
System.out.println("Esperando cliente");
Socket cliente = ss.accept();
lock.lock();
try {
clientes.add(cliente);
} finally {
lock.unlock();
}
Handler handler = new Handler(cliente, this);
handler.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
} catch (Exception ex) {
ex.printStackTrace();
System.exit(-1);
}
}
public List<Socket> getClientes() {
return clientes;
}
public void removeCliente(Socket cliente) {
lock.lock();
try {
clientes.remove(cliente);
System.out.println("cliente removido");
} finally {
lock.unlock();
}
}
}
class Handler extends Thread {
Listener listener;
Socket client;
public Handler(Socket client, Listener listener) {
this.client = client;
this.listener = listener;
}
public void run() {
try (PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()))) {
while(true) {
String line = reader.readLine();
if (line == null) break;
writer.println(line);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
listener.removeCliente(client);
if (client != null) {
try {
client.close();
} catch (Exception ex) {
}
}
}
}
}
class ShutdownHook extends Thread {
Listener listener;
public ShutdownHook(Listener listener) {
this.listener = listener;
}
public void run() {
System.out.println("Finalizando clientes");
for (Socket client: listener.getClientes()) {
try {
if (client != null)
try {
client.close();
} catch (Exception ex) {}
} catch (Exception ex) {}
System.out.println("Finalizei 1 cliente");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment