Refactored to removed redundant Serve1 class

MultiServer still not working correctly
This commit is contained in:
Bruce Eckel 2017-01-24 09:26:37 -08:00
parent c51767d5d5
commit 8b00b51c6f
6 changed files with 43 additions and 65 deletions

View File

@ -7,15 +7,15 @@ import java.net.*;
import java.io.*;
public class ChatterClient implements Runnable {
private InetAddress hostAddress;
private InetAddress host;
private byte[] buf = new byte[1000];
private DatagramPacket dp =
new DatagramPacket(buf, buf.length);
private static int counter = 0;
private int id = counter++;
public ChatterClient(InetAddress hostAddress) {
this.hostAddress = hostAddress;
public ChatterClient(InetAddress host) {
this.host = host;
System.out.println(
"ChatterClient #" + id + " starting");
}
@ -26,7 +26,7 @@ public class ChatterClient implements Runnable {
) {
// Make and send a datagram:
s.send(Dgram.toDatagram(
msg, hostAddress, ChatterServer.INPORT));
msg, host, ChatterServer.INPORT));
// Block until it echoes back:
s.receive(dp);
// Display the echoed contents:

View File

@ -7,58 +7,21 @@ import java.io.*;
import java.net.*;
import java.util.concurrent.*;
class Serve1 implements Runnable {
private ServerSocket ss;
public Serve1(ServerSocket ss) {
this.ss = ss;
}
@Override
public String toString() { return "Serve1: "; }
@Override
public void run() {
System.out.println(this + "Running");
try (
Socket socket = ss.accept();
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
// Boolean enables auto-flush
socket.getOutputStream())), true)
) {
in.lines().anyMatch(message -> {
if(message.equals("END")) {
System.out.println(this +
"Received END. Closing Socket.");
return true;
}
System.out.println(
this + "Message: " + message);
out.println(message);
return false;
});
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}
public class MultiServer implements Runnable {
private final int port;
public MultiServer(int port) {
this.port = port;
}
@Override
public void run() {
System.out.println("Server: Running");
try (
ServerSocket ss =
new ServerSocket(SimpleClient.PORT)
ServerSocket ss = new ServerSocket(port)
) {
System.out.println("Server: " + ss);
for(int i = 0; i < 10; i++)
CompletableFuture
.runAsync(new Serve1(ss));
.runAsync(new SimpleServer(ss));
} catch(IOException e) {
throw new RuntimeException(e);
}

View File

@ -8,14 +8,14 @@ import java.io.*;
import java.util.concurrent.atomic.*;
public class SimpleClient implements Runnable {
// Choose a port outside of the range 1-1024:
public static final int PORT = 8080;
public final int port;
private static AtomicInteger idcount =
new AtomicInteger(0);
private final int id = idcount.getAndIncrement();
private InetAddress hostAddress;
public SimpleClient(InetAddress hostAddress) {
this.hostAddress = hostAddress;
private InetAddress host;
public SimpleClient(InetAddress host, int port) {
this.host = host;
this.port = port;
}
@Override
public String toString() {
@ -25,7 +25,7 @@ public class SimpleClient implements Runnable {
public void run() {
System.out.println(this + "Running");
try(
Socket socket = new Socket(hostAddress, PORT);
Socket socket = new Socket(host, port);
BufferedReader in =
new BufferedReader(
new InputStreamReader(

View File

@ -7,15 +7,18 @@ import java.io.*;
import java.net.*;
public class SimpleServer implements Runnable {
private ServerSocket ss;
public SimpleServer(ServerSocket ss) {
this.ss = ss;
}
@Override
public String toString() { return "Server: "; }
@Override
public void run() {
System.out.println(this + "Running");
try (
ServerSocket s =
new ServerSocket(SimpleClient.PORT);
// Blocks until a connection occurs:
Socket socket = s.accept();
Socket socket = ss.accept();
BufferedReader in =
new BufferedReader(
new InputStreamReader(
@ -24,7 +27,7 @@ public class SimpleServer implements Runnable {
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
// Enable auto-flush:
// Boolean enables auto-flush:
socket.getOutputStream())), true)
) {
System.out.println(this.toString() + socket);

View File

@ -7,12 +7,14 @@ import java.util.concurrent.*;
import onjava.Nap;
public class TestMultiServer {
public static final int port = 8080;
public static void main(String[] args) {
CompletableFuture.runAsync(new MultiServer());
CompletableFuture.runAsync(
new MultiServer(port));
new Nap(1); // Let the server get started
for(int i = 0; i < 10; i++) {
CompletableFuture.runAsync(
new SimpleClient(Local.host()));
new SimpleClient(Local.host(), port));
}
new Nap(4);
// No exceptions mean success

View File

@ -2,17 +2,27 @@
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.io.*;
import java.net.*;
import java.util.concurrent.*;
import onjava.Nap;
public class TestSimpleServer {
// Choose a port outside of the range 1-1024:
public static final int port = 8080;
public static void main(String[] args) {
CompletableFuture.runAsync(
new SimpleServer());
CompletableFuture.runAsync(
new SimpleClient(Local.host()));
new Nap(1);
// Success if no exceptions happen
try (
ServerSocket ss =
new ServerSocket(port)
) {
CompletableFuture.runAsync(
new SimpleServer(ss));
CompletableFuture.runAsync(
new SimpleClient(Local.host(), port));
new Nap(1);
// Success if no exceptions happen
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}