Still refactoring, trying to get them to work right

This commit is contained in:
Bruce Eckel 2017-01-23 14:41:17 -08:00
parent bd0d56076c
commit c51767d5d5
8 changed files with 48 additions and 81 deletions

View File

@ -42,7 +42,7 @@ public class ChatterClient implements Runnable {
}
@Override
public void run() {
for(int i = 0; i <= 5; i++)
for(int i = 0; i < 10; i++)
sendAndEcho(
"Client #" + id + ", message #" + i);
}

View File

@ -27,9 +27,8 @@ public class ChatterServer implements Runnable {
System.out.println(rcvd);
String echoString =
"Echoed: " + rcvd;
// Extract the address and port from the
// received datagram to find out where to
// send it back to:
// Extract address and port from received
// datagram and send it back there:
DatagramPacket echo =
Dgram.toDatagram(echoString,
dp.getAddress(), dp.getPort());

View File

@ -7,15 +7,16 @@ import java.io.*;
import java.net.*;
import java.util.concurrent.*;
class ServeOne implements Runnable {
static final int PORT = 8080;
class Serve1 implements Runnable {
private ServerSocket ss;
public ServeOne(ServerSocket ss) {
public Serve1(ServerSocket ss) {
this.ss = ss;
}
@Override
public String toString() { return "Serve1: "; }
@Override
public void run() {
System.out.println("Starting ServeOne");
System.out.println(this + "Running");
try (
Socket socket = ss.accept();
BufferedReader in =
@ -31,12 +32,12 @@ class ServeOne implements Runnable {
) {
in.lines().anyMatch(message -> {
if(message.equals("END")) {
System.out.println(
System.out.println(this +
"Received END. Closing Socket.");
return true;
}
System.out.println(
"Message : " + message);
this + "Message: " + message);
out.println(message);
return false;
});
@ -49,13 +50,15 @@ class ServeOne implements Runnable {
public class MultiServer implements Runnable {
@Override
public void run() {
System.out.println("Running MultiServer");
System.out.println("Server: Running");
try (
ServerSocket ss =
new ServerSocket(ServeOne.PORT)
new ServerSocket(SimpleClient.PORT)
) {
while(true)
CompletableFuture.runAsync(new ServeOne(ss));
System.out.println("Server: " + ss);
for(int i = 0; i < 10; i++)
CompletableFuture
.runAsync(new Serve1(ss));
} catch(IOException e) {
throw new RuntimeException(e);
}

View File

@ -2,22 +2,30 @@
// (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.
// Sends lines to the server and
// reads lines the server sends.
// Sends to the server, reads from the server
import java.net.*;
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;
private static AtomicInteger idcount =
new AtomicInteger(0);
private final int id = idcount.getAndIncrement();
private InetAddress hostAddress;
public SimpleClient(InetAddress hostAddress) {
this.hostAddress = hostAddress;
}
@Override
public String toString() {
return "Client[" + id + "]: ";
}
@Override
public void run() {
System.out.println("hostAddress = " + hostAddress);
try (
Socket socket =
new Socket(hostAddress, SimpleServer.PORT);
System.out.println(this + "Running");
try(
Socket socket = new Socket(hostAddress, PORT);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
@ -29,11 +37,11 @@ public class SimpleClient implements Runnable {
// Enable auto-flush:
socket.getOutputStream())), true);
) {
System.out.println("socket: " + socket);
System.out.println(this.toString() + socket);
for(int i = 0; i < 10; i ++) {
out.println("hello " + i);
String str = in.readLine();
System.out.println("client Message : " + str);
out.println(this + "(*" + i + "*)");
System.out.println(this +
"Received '" + in.readLine() + "'");
}
out.println("END");
} catch(IOException e) {

View File

@ -1,43 +0,0 @@
// network/SimpleClient2.java
// (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.net.*;
import java.io.*;
public class SimpleClient2 implements Runnable {
private InetAddress address;
private static int counter = 0;
private int id = counter++;
public SimpleClient2(InetAddress address) {
System.out.println("Making client " + id);
this.address = address;
}
@Override
public void run() {
System.out.println("Running client " + id);
try (
Socket socket =
new Socket(address, ServeOne.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 ex) {
throw new RuntimeException(ex);
}
}
}

View File

@ -7,12 +7,13 @@ import java.io.*;
import java.net.*;
public class SimpleServer implements Runnable {
// Choose a port outside of the range 1-1024:
public static final int PORT = 8080;
@Override
public String toString() { return "Server: "; }
@Override
public void run() {
try (
ServerSocket s = new ServerSocket(PORT);
ServerSocket s =
new ServerSocket(SimpleClient.PORT);
// Blocks until a connection occurs:
Socket socket = s.accept();
BufferedReader in =
@ -26,16 +27,14 @@ public class SimpleServer implements Runnable {
// Enable auto-flush:
socket.getOutputStream())), true)
) {
System.out.println("Connection: " + socket);
in.lines().anyMatch(message->{
if (message.equals("END")) {
System.out.println(
System.out.println(this.toString() + socket);
in.lines().anyMatch(message -> {
if(message.equals("END")) {
System.out.println(this +
"Received END. Closing Socket.");
return true;
}
System.out.println(
"Server Response: " + message);
out.println(message);
out.println(this + message);
return false;
});
} catch(IOException e) {

View File

@ -9,11 +9,12 @@ import onjava.Nap;
public class TestMultiServer {
public static void main(String[] args) {
CompletableFuture.runAsync(new MultiServer());
new Nap(1); // Let the server get started
for(int i = 0; i < 10; i++) {
CompletableFuture.runAsync(
new SimpleClient2(Local.host()));
new SimpleClient(Local.host()));
}
new Nap(2);
new Nap(4);
// No exceptions mean success
}
}

View File

@ -1,4 +1,4 @@
// network/TestSimpleServerClient.java
// network/TestSimpleServer.java
// (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.
@ -6,7 +6,7 @@ import java.net.*;
import java.util.concurrent.*;
import onjava.Nap;
public class TestSimpleServerClient {
public class TestSimpleServer {
public static void main(String[] args) {
CompletableFuture.runAsync(
new SimpleServer());