Skip to content

Commit 8b00b51

Browse files
committed
Refactored to removed redundant Serve1 class
MultiServer still not working correctly
1 parent c51767d commit 8b00b51

6 files changed

Lines changed: 43 additions & 65 deletions

File tree

network/ChatterClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
import java.io.*;
88

99
public class ChatterClient implements Runnable {
10-
private InetAddress hostAddress;
10+
private InetAddress host;
1111
private byte[] buf = new byte[1000];
1212
private DatagramPacket dp =
1313
new DatagramPacket(buf, buf.length);
1414
private static int counter = 0;
1515
private int id = counter++;
1616

17-
public ChatterClient(InetAddress hostAddress) {
18-
this.hostAddress = hostAddress;
17+
public ChatterClient(InetAddress host) {
18+
this.host = host;
1919
System.out.println(
2020
"ChatterClient #" + id + " starting");
2121
}
@@ -26,7 +26,7 @@ public void sendAndEcho(String msg) {
2626
) {
2727
// Make and send a datagram:
2828
s.send(Dgram.toDatagram(
29-
msg, hostAddress, ChatterServer.INPORT));
29+
msg, host, ChatterServer.INPORT));
3030
// Block until it echoes back:
3131
s.receive(dp);
3232
// Display the echoed contents:

network/MultiServer.java

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,21 @@
77
import java.net.*;
88
import java.util.concurrent.*;
99

10-
class Serve1 implements Runnable {
11-
private ServerSocket ss;
12-
public Serve1(ServerSocket ss) {
13-
this.ss = ss;
14-
}
15-
@Override
16-
public String toString() { return "Serve1: "; }
17-
@Override
18-
public void run() {
19-
System.out.println(this + "Running");
20-
try (
21-
Socket socket = ss.accept();
22-
BufferedReader in =
23-
new BufferedReader(
24-
new InputStreamReader(
25-
socket.getInputStream()));
26-
PrintWriter out =
27-
new PrintWriter(
28-
new BufferedWriter(
29-
new OutputStreamWriter(
30-
// Boolean enables auto-flush
31-
socket.getOutputStream())), true)
32-
) {
33-
in.lines().anyMatch(message -> {
34-
if(message.equals("END")) {
35-
System.out.println(this +
36-
"Received END. Closing Socket.");
37-
return true;
38-
}
39-
System.out.println(
40-
this + "Message: " + message);
41-
out.println(message);
42-
return false;
43-
});
44-
} catch(IOException e) {
45-
throw new RuntimeException(e);
46-
}
47-
}
48-
}
49-
5010
public class MultiServer implements Runnable {
11+
private final int port;
12+
public MultiServer(int port) {
13+
this.port = port;
14+
}
5115
@Override
5216
public void run() {
5317
System.out.println("Server: Running");
5418
try (
55-
ServerSocket ss =
56-
new ServerSocket(SimpleClient.PORT)
19+
ServerSocket ss = new ServerSocket(port)
5720
) {
5821
System.out.println("Server: " + ss);
5922
for(int i = 0; i < 10; i++)
6023
CompletableFuture
61-
.runAsync(new Serve1(ss));
24+
.runAsync(new SimpleServer(ss));
6225
} catch(IOException e) {
6326
throw new RuntimeException(e);
6427
}

network/SimpleClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import java.util.concurrent.atomic.*;
99

1010
public class SimpleClient implements Runnable {
11-
// Choose a port outside of the range 1-1024:
12-
public static final int PORT = 8080;
11+
public final int port;
1312
private static AtomicInteger idcount =
1413
new AtomicInteger(0);
1514
private final int id = idcount.getAndIncrement();
16-
private InetAddress hostAddress;
17-
public SimpleClient(InetAddress hostAddress) {
18-
this.hostAddress = hostAddress;
15+
private InetAddress host;
16+
public SimpleClient(InetAddress host, int port) {
17+
this.host = host;
18+
this.port = port;
1919
}
2020
@Override
2121
public String toString() {
@@ -25,7 +25,7 @@ public String toString() {
2525
public void run() {
2626
System.out.println(this + "Running");
2727
try(
28-
Socket socket = new Socket(hostAddress, PORT);
28+
Socket socket = new Socket(host, port);
2929
BufferedReader in =
3030
new BufferedReader(
3131
new InputStreamReader(

network/SimpleServer.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
import java.net.*;
88

99
public class SimpleServer implements Runnable {
10+
private ServerSocket ss;
11+
public SimpleServer(ServerSocket ss) {
12+
this.ss = ss;
13+
}
1014
@Override
1115
public String toString() { return "Server: "; }
1216
@Override
1317
public void run() {
18+
System.out.println(this + "Running");
1419
try (
15-
ServerSocket s =
16-
new ServerSocket(SimpleClient.PORT);
1720
// Blocks until a connection occurs:
18-
Socket socket = s.accept();
21+
Socket socket = ss.accept();
1922
BufferedReader in =
2023
new BufferedReader(
2124
new InputStreamReader(
@@ -24,7 +27,7 @@ public void run() {
2427
new PrintWriter(
2528
new BufferedWriter(
2629
new OutputStreamWriter(
27-
// Enable auto-flush:
30+
// Boolean enables auto-flush:
2831
socket.getOutputStream())), true)
2932
) {
3033
System.out.println(this.toString() + socket);

network/TestMultiServer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import onjava.Nap;
88

99
public class TestMultiServer {
10+
public static final int port = 8080;
1011
public static void main(String[] args) {
11-
CompletableFuture.runAsync(new MultiServer());
12+
CompletableFuture.runAsync(
13+
new MultiServer(port));
1214
new Nap(1); // Let the server get started
1315
for(int i = 0; i < 10; i++) {
1416
CompletableFuture.runAsync(
15-
new SimpleClient(Local.host()));
17+
new SimpleClient(Local.host(), port));
1618
}
1719
new Nap(4);
1820
// No exceptions mean success

network/TestSimpleServer.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22
// (c)2017 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
5+
import java.io.*;
56
import java.net.*;
67
import java.util.concurrent.*;
78
import onjava.Nap;
89

910
public class TestSimpleServer {
11+
// Choose a port outside of the range 1-1024:
12+
public static final int port = 8080;
1013
public static void main(String[] args) {
11-
CompletableFuture.runAsync(
12-
new SimpleServer());
13-
CompletableFuture.runAsync(
14-
new SimpleClient(Local.host()));
15-
new Nap(1);
16-
// Success if no exceptions happen
14+
try (
15+
ServerSocket ss =
16+
new ServerSocket(port)
17+
) {
18+
CompletableFuture.runAsync(
19+
new SimpleServer(ss));
20+
CompletableFuture.runAsync(
21+
new SimpleClient(Local.host(), port));
22+
new Nap(1);
23+
// Success if no exceptions happen
24+
} catch(IOException e) {
25+
throw new RuntimeException(e);
26+
}
1727
}
1828
}

0 commit comments

Comments
 (0)