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 @Override
public void run() { public void run() {
for(int i = 0; i <= 5; i++) for(int i = 0; i < 10; i++)
sendAndEcho( sendAndEcho(
"Client #" + id + ", message #" + i); "Client #" + id + ", message #" + i);
} }

View File

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

View File

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

View File

@ -2,22 +2,30 @@
// (c)2017 MindView LLC: see Copyright.txt // (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Sends lines to the server and // Sends to the server, reads from the server
// reads lines the server sends.
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import java.util.concurrent.atomic.*;
public class SimpleClient implements Runnable { 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; private InetAddress hostAddress;
public SimpleClient(InetAddress hostAddress) { public SimpleClient(InetAddress hostAddress) {
this.hostAddress = hostAddress; this.hostAddress = hostAddress;
} }
@Override @Override
public String toString() {
return "Client[" + id + "]: ";
}
@Override
public void run() { public void run() {
System.out.println("hostAddress = " + hostAddress); System.out.println(this + "Running");
try( try(
Socket socket = Socket socket = new Socket(hostAddress, PORT);
new Socket(hostAddress, SimpleServer.PORT);
BufferedReader in = BufferedReader in =
new BufferedReader( new BufferedReader(
new InputStreamReader( new InputStreamReader(
@ -29,11 +37,11 @@ public class SimpleClient implements Runnable {
// Enable auto-flush: // Enable auto-flush:
socket.getOutputStream())), true); socket.getOutputStream())), true);
) { ) {
System.out.println("socket: " + socket); System.out.println(this.toString() + socket);
for(int i = 0; i < 10; i ++) { for(int i = 0; i < 10; i ++) {
out.println("hello " + i); out.println(this + "(*" + i + "*)");
String str = in.readLine(); System.out.println(this +
System.out.println("client Message : " + str); "Received '" + in.readLine() + "'");
} }
out.println("END"); out.println("END");
} catch(IOException e) { } 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.*; import java.net.*;
public class SimpleServer implements Runnable { public class SimpleServer implements Runnable {
// Choose a port outside of the range 1-1024: @Override
public static final int PORT = 8080; public String toString() { return "Server: "; }
@Override @Override
public void run() { public void run() {
try ( try (
ServerSocket s = new ServerSocket(PORT); ServerSocket s =
new ServerSocket(SimpleClient.PORT);
// Blocks until a connection occurs: // Blocks until a connection occurs:
Socket socket = s.accept(); Socket socket = s.accept();
BufferedReader in = BufferedReader in =
@ -26,16 +27,14 @@ public class SimpleServer implements Runnable {
// Enable auto-flush: // Enable auto-flush:
socket.getOutputStream())), true) socket.getOutputStream())), true)
) { ) {
System.out.println("Connection: " + socket); System.out.println(this.toString() + socket);
in.lines().anyMatch(message -> { in.lines().anyMatch(message -> {
if(message.equals("END")) { if(message.equals("END")) {
System.out.println( System.out.println(this +
"Received END. Closing Socket."); "Received END. Closing Socket.");
return true; return true;
} }
System.out.println( out.println(this + message);
"Server Response: " + message);
out.println(message);
return false; return false;
}); });
} catch(IOException e) { } catch(IOException e) {

View File

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

View File

@ -1,4 +1,4 @@
// network/TestSimpleServerClient.java // network/TestSimpleServer.java
// (c)2017 MindView LLC: see Copyright.txt // (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
@ -6,7 +6,7 @@ import java.net.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import onjava.Nap; import onjava.Nap;
public class TestSimpleServerClient { public class TestSimpleServer {
public static void main(String[] args) { public static void main(String[] args) {
CompletableFuture.runAsync( CompletableFuture.runAsync(
new SimpleServer()); new SimpleServer());