|
|
@@ -1,82 +1,70 @@ |
|
|
|
package xyz.nextn; |
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.BufferedInputStream; |
|
|
|
import java.io.DataInputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.io.PrintWriter; |
|
|
|
import java.net.ServerSocket; |
|
|
|
import java.net.Socket; |
|
|
|
|
|
|
|
public class Server { |
|
|
|
private ServerSocket serverSocket; |
|
|
|
protected int serverPort = 1111; |
|
|
|
protected ServerSocket serverSocket = null; |
|
|
|
protected boolean isStopped = false; |
|
|
|
protected Thread runningThread= null; |
|
|
|
|
|
|
|
public void start(int port){ |
|
|
|
try { |
|
|
|
serverSocket = new ServerSocket(port); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
public Server(int port){ |
|
|
|
this.serverPort = port; |
|
|
|
} |
|
|
|
|
|
|
|
public void run(){ |
|
|
|
synchronized(this){ |
|
|
|
this.runningThread = Thread.currentThread(); |
|
|
|
} |
|
|
|
while (true) { |
|
|
|
openServerSocket(); |
|
|
|
while(! isStopped()){ |
|
|
|
Socket clientSocket = null; |
|
|
|
try { |
|
|
|
new EchoClientHandler(serverSocket.accept()).start(); |
|
|
|
clientSocket = this.serverSocket.accept(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
if(isStopped()) { |
|
|
|
System.out.println("Server Stopped.") ; |
|
|
|
return; |
|
|
|
} |
|
|
|
throw new RuntimeException( |
|
|
|
"Error accepting client connection", e); |
|
|
|
} |
|
|
|
new Thread( |
|
|
|
new WorkerRunnable( |
|
|
|
clientSocket, "Multithreaded Server") |
|
|
|
).start(); |
|
|
|
} |
|
|
|
System.out.println("Server Stopped.") ; |
|
|
|
} |
|
|
|
|
|
|
|
public void stop() throws IOException { |
|
|
|
serverSocket.close(); |
|
|
|
} |
|
|
|
|
|
|
|
private static class EchoClientHandler extends Thread { |
|
|
|
private Socket clientSocket; |
|
|
|
private PrintWriter out; |
|
|
|
private BufferedReader in; |
|
|
|
private synchronized boolean isStopped() { |
|
|
|
return this.isStopped; |
|
|
|
} |
|
|
|
|
|
|
|
public EchoClientHandler(Socket socket) { |
|
|
|
this.clientSocket = socket; |
|
|
|
public synchronized void stop(){ |
|
|
|
this.isStopped = true; |
|
|
|
try { |
|
|
|
this.serverSocket.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException("Error closing server", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void run() { |
|
|
|
try { |
|
|
|
out = new PrintWriter(clientSocket.getOutputStream(), true); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
try { |
|
|
|
in = new BufferedReader( |
|
|
|
new InputStreamReader(clientSocket.getInputStream())); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
String inputLine = null; |
|
|
|
while (true) { |
|
|
|
try { |
|
|
|
if (!((inputLine = in.readLine()) != null)) break; |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
if (".".equals(inputLine)) { |
|
|
|
out.println("bye"); |
|
|
|
break; |
|
|
|
} |
|
|
|
out.println(inputLine); |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
in.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
out.close(); |
|
|
|
try { |
|
|
|
clientSocket.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
private void openServerSocket() { |
|
|
|
try { |
|
|
|
this.serverSocket = new ServerSocket(this.serverPort); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException("Cannot open port 8080", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|