Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 704bac0

Browse files
author
Bruce Eckel
committed
Bill's changes and formatting
1 parent 11610b8 commit 704bac0

File tree

4 files changed

+72
-132
lines changed

4 files changed

+72
-132
lines changed

network/MultiSimpleClient.java

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,50 @@
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
// Testing MultiSimpleServer with multiple clients
5+
// Testing MultiSimpleServer with multiple clients.
66
// {ValidateByHand}
77
import java.net.*;
88
import java.io.*;
99
import onjava.*;
1010

11-
class SimpleClientThread extends Thread {
12-
private Socket socket;
13-
private BufferedReader in;
14-
private PrintWriter out;
11+
class SimpleClientThread implements Runnable {
12+
private InetAddress address;
1513
private static int counter = 0;
1614
private int id = counter++;
1715
private static int threadcount = 0;
1816
public static int threadCount() {
1917
return threadcount;
2018
}
21-
public SimpleClientThread(InetAddress addr) {
19+
public SimpleClientThread(InetAddress address) {
2220
System.out.println("Making client " + id);
21+
this.address = address;
2322
threadcount++;
24-
try {
25-
socket =
26-
new Socket(addr, MultiSimpleServer.PORT);
27-
} catch(IOException e) {
28-
// If the creation of the socket fails,
29-
// nothing needs cleanup.
30-
}
31-
try {
32-
in =
23+
}
24+
@Override
25+
public void run() {
26+
try (
27+
Socket socket =
28+
new Socket(address, MultiSimpleServer.PORT);
29+
BufferedReader in =
3330
new BufferedReader(
3431
new InputStreamReader(
3532
socket.getInputStream()));
36-
// Enable auto-flush:
37-
out =
33+
PrintWriter out =
3834
new PrintWriter(
3935
new BufferedWriter(
4036
new OutputStreamWriter(
41-
socket.getOutputStream())), true);
42-
start();
43-
} catch(IOException e) {
44-
// The socket should be closed on any
45-
// failures other than the socket
46-
// constructor:
47-
try {
48-
socket.close();
49-
} catch(IOException e2) {
50-
throw new RuntimeException(e2);
51-
}
52-
}
53-
// Otherwise the socket will be closed by
54-
// the run() method of the thread.
55-
}
56-
@Override
57-
public void run() {
58-
try {
59-
for(int i = 0; i < 25; i++) {
37+
// Enable auto-flush:
38+
socket.getOutputStream())), true)
39+
) {
40+
for (int i = 0; i < 25; i++) {
6041
out.println("Client " + id + ": " + i);
6142
String str = in.readLine();
6243
System.out.println(str);
6344
}
6445
out.println("END");
65-
} catch(IOException e) {
66-
throw new RuntimeException(e);
46+
} catch (IOException ex) {
47+
throw new RuntimeException(ex);
6748
} finally {
68-
// Always close it:
69-
try {
70-
socket.close();
71-
} catch(IOException e) {
72-
throw new RuntimeException(e);
73-
}
7449
threadcount--; // Ending this thread
7550
}
7651
}
@@ -82,10 +57,10 @@ public class MultiSimpleClient {
8257
main(String[] args) throws IOException,
8358
InterruptedException {
8459
new TimedAbort(5); // Terminate after 5 seconds
85-
InetAddress addr = InetAddress.getByName(null);
60+
InetAddress address = InetAddress.getByName(null);
8661
while(true) {
8762
if(SimpleClientThread.threadCount() < MAX_THREADS)
88-
new SimpleClientThread(addr);
63+
new SimpleClientThread(address);
8964
Thread.sleep(100);
9065
}
9166
}

network/MultiSimpleServer.java

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,42 @@
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
// Uses threads to handle any number of clients
5+
// Uses threads to handle any number of clients.
66
// {ValidateByHand}
77
import java.io.*;
88
import java.net.*;
99
import onjava.*;
1010

11-
class ServeOneSimple extends Thread {
12-
private Socket socket;
13-
private BufferedReader in;
14-
private PrintWriter out;
15-
public ServeOneSimple(Socket s)
16-
throws IOException {
17-
socket = s;
18-
in =
19-
new BufferedReader(
20-
new InputStreamReader(
21-
socket.getInputStream()));
22-
// Enable auto-flush:
23-
out =
24-
new PrintWriter(
25-
new BufferedWriter(
26-
new OutputStreamWriter(
27-
socket.getOutputStream())), true);
28-
// If any of the above calls throw an exception,
29-
// the caller is responsible for closing the
30-
// socket. Otherwise the thread closes it.
31-
start(); // Calls run()
11+
class ServeOneSimple implements Runnable {
12+
private ServerSocket ss;
13+
public
14+
ServeOneSimple(ServerSocket ss) throws IOException {
15+
this.ss = ss;
3216
}
3317
@Override
3418
public void run() {
35-
try {
19+
try (
20+
Socket socket = ss.accept();
21+
BufferedReader in =
22+
new BufferedReader(
23+
new InputStreamReader(
24+
socket.getInputStream()));
25+
PrintWriter out =
26+
new PrintWriter(
27+
new BufferedWriter(
28+
new OutputStreamWriter(
29+
// Enable auto-flush:
30+
socket.getOutputStream())), true)
31+
) {
3632
while (true) {
3733
String str = in.readLine();
3834
if(str.equals("END")) break;
3935
System.out.println("Echoing: " + str);
4036
out.println(str);
4137
}
42-
System.out.println("closing...");
38+
System.out.println("closing socket...");
4339
} catch (IOException e) {
4440
throw new RuntimeException(e);
45-
} finally {
46-
try {
47-
socket.close();
48-
} catch(IOException e) {
49-
throw new RuntimeException(e);
50-
}
5141
}
5242
}
5343
}
@@ -57,22 +47,12 @@ public class MultiSimpleServer {
5747
public static void
5848
main(String[] args) throws IOException {
5949
new TimedAbort(5); // Terminate after 5 seconds
60-
ServerSocket s = new ServerSocket(PORT);
6150
System.out.println("Server Started");
62-
try {
51+
try (ServerSocket ss = new ServerSocket(PORT)){
52+
// Block until a connection occurs:
6353
while(true) {
64-
// Blocks until a connection occurs:
65-
Socket socket = s.accept();
66-
try {
67-
new ServeOneSimple(socket);
68-
} catch(IOException e) {
69-
// If it fails, close the socket,
70-
// otherwise the thread will close it:
71-
socket.close();
72-
}
54+
new ServeOneSimple(ss);
7355
}
74-
} finally {
75-
s.close();
7656
}
7757
}
7858
}

network/SimpleClient.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55
// Sends lines to the server and
6-
// reads lines the server sends
6+
// reads lines the server sends.
77
// {ValidateByHand}
88
import java.net.*;
99
import java.io.*;
@@ -16,32 +16,27 @@ public class SimpleClient {
1616
InetAddress addr =
1717
InetAddress.getLoopbackAddress();
1818
System.out.println("addr = " + addr);
19-
Socket socket =
20-
new Socket(addr, SimpleServer.PORT);
21-
// Guard everything in a try-finally to make
22-
// sure that the socket is closed:
23-
try {
24-
System.out.println("socket = " + socket);
19+
// TWR will sure that the socket is closed:
20+
try (
21+
Socket socket = new Socket(addr, SimpleServer.PORT);
2522
BufferedReader in =
2623
new BufferedReader(
2724
new InputStreamReader(
2825
socket.getInputStream()));
29-
// Output is automatically flushed
30-
// by PrintWriter:
3126
PrintWriter out =
3227
new PrintWriter(
3328
new BufferedWriter(
3429
new OutputStreamWriter(
35-
socket.getOutputStream())),true);
30+
// Enable auto-flush:
31+
socket.getOutputStream())), true);
32+
) {
33+
System.out.println("socket: " + socket);
3634
for(int i = 0; i < 10; i ++) {
3735
out.println("hello " + i);
3836
String str = in.readLine();
3937
System.out.println(str);
4038
}
4139
out.println("END");
42-
} finally {
43-
System.out.println("closing...");
44-
socket.close();
4540
}
4641
}
4742
}

network/SimpleServer.java

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
// Echoes what the client sends
5+
// Echoes what the client sends.
66
// {ValidateByHand}
77
import java.io.*;
88
import java.net.*;
@@ -12,38 +12,28 @@ public class SimpleServer {
1212
public static final int PORT = 8080;
1313
public static void
1414
main(String[] args) throws IOException {
15-
ServerSocket s = new ServerSocket(PORT);
16-
System.out.println("Started: " + s);
17-
try {
15+
try (
16+
ServerSocket s = new ServerSocket(PORT);
1817
// Blocks until a connection occurs:
1918
Socket socket = s.accept();
20-
try {
21-
System.out.println(
22-
"Connection accepted: "+ socket);
23-
BufferedReader in =
24-
new BufferedReader(
25-
new InputStreamReader(
26-
socket.getInputStream()));
27-
// Output is automatically flushed
28-
// by PrintWriter:
29-
PrintWriter out =
30-
new PrintWriter(
31-
new BufferedWriter(
32-
new OutputStreamWriter(
33-
socket.getOutputStream())), true);
34-
while (true) {
35-
String str = in.readLine();
36-
if(str.equals("END")) break;
37-
System.out.println("Echoing: " + str);
38-
out.println(str);
39-
}
40-
// Always close the two sockets...
41-
} finally {
42-
System.out.println("closing...");
43-
socket.close();
19+
BufferedReader in =
20+
new BufferedReader(
21+
new InputStreamReader(
22+
socket.getInputStream()));
23+
PrintWriter out =
24+
new PrintWriter(
25+
new BufferedWriter(
26+
new OutputStreamWriter(
27+
// Enable auto-flush:
28+
socket.getOutputStream())), true)
29+
) {
30+
System.out.println("Connection: " + socket);
31+
while (true) {
32+
String str = in.readLine();
33+
if(str.equals("END")) break;
34+
System.out.println("Echoing: " + str);
35+
out.println(str);
4436
}
45-
} finally {
46-
s.close();
4737
}
4838
}
4939
}

0 commit comments

Comments
 (0)