Bill's changes and formatting

This commit is contained in:
Bruce Eckel 2016-09-03 12:18:15 -06:00
parent 11610b8270
commit 704bac09f4
4 changed files with 75 additions and 135 deletions

View File

@ -2,75 +2,50 @@
// (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.
// Testing MultiSimpleServer with multiple clients
// Testing MultiSimpleServer with multiple clients.
// {ValidateByHand}
import java.net.*;
import java.io.*;
import onjava.*;
class SimpleClientThread extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
class SimpleClientThread implements Runnable {
private InetAddress address;
private static int counter = 0;
private int id = counter++;
private static int threadcount = 0;
public static int threadCount() {
return threadcount;
}
public SimpleClientThread(InetAddress addr) {
public SimpleClientThread(InetAddress address) {
System.out.println("Making client " + id);
this.address = address;
threadcount++;
try {
socket =
new Socket(addr, MultiSimpleServer.PORT);
} catch(IOException e) {
// If the creation of the socket fails,
// nothing needs cleanup.
}
try {
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Enable auto-flush:
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
start();
} catch(IOException e) {
// The socket should be closed on any
// failures other than the socket
// constructor:
try {
socket.close();
} catch(IOException e2) {
throw new RuntimeException(e2);
}
}
// Otherwise the socket will be closed by
// the run() method of the thread.
}
@Override
public void run() {
try {
for(int i = 0; i < 25; i++) {
try (
Socket socket =
new Socket(address, MultiSimpleServer.PORT);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
// Enable auto-flush:
socket.getOutputStream())), true)
) {
for (int i = 0; i < 25; i++) {
out.println("Client " + id + ": " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} catch(IOException e) {
throw new RuntimeException(e);
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
// Always close it:
try {
socket.close();
} catch(IOException e) {
throw new RuntimeException(e);
}
threadcount--; // Ending this thread
}
}
@ -82,10 +57,10 @@ public class MultiSimpleClient {
main(String[] args) throws IOException,
InterruptedException {
new TimedAbort(5); // Terminate after 5 seconds
InetAddress addr = InetAddress.getByName(null);
InetAddress address = InetAddress.getByName(null);
while(true) {
if(SimpleClientThread.threadCount() < MAX_THREADS)
new SimpleClientThread(addr);
new SimpleClientThread(address);
Thread.sleep(100);
}
}

View File

@ -2,52 +2,42 @@
// (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 threads to handle any number of clients
// Uses threads 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 closes it.
start(); // Calls run()
class ServeOneSimple implements Runnable {
private ServerSocket ss;
public
ServeOneSimple(ServerSocket ss) throws IOException {
this.ss = ss;
}
@Override
public void run() {
try {
try (
Socket socket = ss.accept();
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
// Enable auto-flush:
socket.getOutputStream())), true)
) {
while (true) {
String str = in.readLine();
if(str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
System.out.println("closing...");
System.out.println("closing socket...");
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
socket.close();
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}
}
@ -57,22 +47,12 @@ public class MultiSimpleServer {
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 {
try (ServerSocket ss = new ServerSocket(PORT)){
// Block until a connection occurs:
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();
}
new ServeOneSimple(ss);
}
} finally {
s.close();
}
}
}

View File

@ -3,7 +3,7 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Sends lines to the server and
// reads lines the server sends
// reads lines the server sends.
// {ValidateByHand}
import java.net.*;
import java.io.*;
@ -16,32 +16,27 @@ public class SimpleClient {
InetAddress addr =
InetAddress.getLoopbackAddress();
System.out.println("addr = " + addr);
Socket socket =
new Socket(addr, SimpleServer.PORT);
// Guard everything in a try-finally to make
// sure that the socket is closed:
try {
System.out.println("socket = " + socket);
// TWR will sure that the socket is closed:
try (
Socket socket = new Socket(addr, SimpleServer.PORT);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
// Enable auto-flush:
socket.getOutputStream())), true);
) {
System.out.println("socket: " + socket);
for(int i = 0; i < 10; i ++) {
out.println("hello " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} finally {
System.out.println("closing...");
socket.close();
}
}
}

View File

@ -2,7 +2,7 @@
// (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.
// Echoes what the client sends
// Echoes what the client sends.
// {ValidateByHand}
import java.io.*;
import java.net.*;
@ -12,38 +12,28 @@ public class SimpleServer {
public static final int PORT = 8080;
public static void
main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
try (
ServerSocket s = new ServerSocket(PORT);
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.println(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
while (true) {
String str = in.readLine();
if(str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
// Always close the two sockets...
} finally {
System.out.println("closing...");
socket.close();
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
// Enable auto-flush:
socket.getOutputStream())), true)
) {
System.out.println("Connection: " + socket);
while (true) {
String str = in.readLine();
if(str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
} finally {
s.close();
}
}
}