OnJava8-Examples/network/MultiSimpleServer.java
2016-01-25 18:05:55 -08:00

77 lines
2.0 KiB
Java

// network/MultiSimpleServer.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Uses multithreading to handle any number of clients
// {ValidateByHand}
import java.io.*;
import java.net.*;
import onjava.*;
class ServeOneSimple extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServeOneSimple(Socket s)
throws IOException {
socket = s;
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Enable auto-flush:
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
// If any of the above calls throw an
// exception, the caller is responsible for
// closing the socket. Otherwise the thread
// will close it.
start(); // Calls run()
}
@Override
public void run() {
try {
while (true) {
String str = in.readLine();
if(str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
System.out.println("closing...");
} catch (IOException e) {
} finally {
try {
socket.close();
} catch(IOException e) {}
}
}
}
public class MultiSimpleServer {
static final int PORT = 8080;
public static void
main(String[] args) throws IOException {
new TimedAbort(5); // Terminate after 5 seconds
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server Started");
try {
while(true) {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
new ServeOneSimple(socket);
} catch(IOException e) {
// If it fails, close the socket,
// otherwise the thread will close it:
socket.close();
}
}
} finally {
s.close();
}
}
}