Refactored to removed redundant Serve1 class
MultiServer still not working correctly
This commit is contained in:
parent
c51767d5d5
commit
8b00b51c6f
@ -7,15 +7,15 @@ import java.net.*;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class ChatterClient implements Runnable {
|
public class ChatterClient implements Runnable {
|
||||||
private InetAddress hostAddress;
|
private InetAddress host;
|
||||||
private byte[] buf = new byte[1000];
|
private byte[] buf = new byte[1000];
|
||||||
private DatagramPacket dp =
|
private DatagramPacket dp =
|
||||||
new DatagramPacket(buf, buf.length);
|
new DatagramPacket(buf, buf.length);
|
||||||
private static int counter = 0;
|
private static int counter = 0;
|
||||||
private int id = counter++;
|
private int id = counter++;
|
||||||
|
|
||||||
public ChatterClient(InetAddress hostAddress) {
|
public ChatterClient(InetAddress host) {
|
||||||
this.hostAddress = hostAddress;
|
this.host = host;
|
||||||
System.out.println(
|
System.out.println(
|
||||||
"ChatterClient #" + id + " starting");
|
"ChatterClient #" + id + " starting");
|
||||||
}
|
}
|
||||||
@ -26,7 +26,7 @@ public class ChatterClient implements Runnable {
|
|||||||
) {
|
) {
|
||||||
// Make and send a datagram:
|
// Make and send a datagram:
|
||||||
s.send(Dgram.toDatagram(
|
s.send(Dgram.toDatagram(
|
||||||
msg, hostAddress, ChatterServer.INPORT));
|
msg, host, ChatterServer.INPORT));
|
||||||
// Block until it echoes back:
|
// Block until it echoes back:
|
||||||
s.receive(dp);
|
s.receive(dp);
|
||||||
// Display the echoed contents:
|
// Display the echoed contents:
|
||||||
|
@ -7,58 +7,21 @@ import java.io.*;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.concurrent.*;
|
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 {
|
public class MultiServer implements Runnable {
|
||||||
|
private final int port;
|
||||||
|
public MultiServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
System.out.println("Server: Running");
|
System.out.println("Server: Running");
|
||||||
try (
|
try (
|
||||||
ServerSocket ss =
|
ServerSocket ss = new ServerSocket(port)
|
||||||
new ServerSocket(SimpleClient.PORT)
|
|
||||||
) {
|
) {
|
||||||
System.out.println("Server: " + ss);
|
System.out.println("Server: " + ss);
|
||||||
for(int i = 0; i < 10; i++)
|
for(int i = 0; i < 10; i++)
|
||||||
CompletableFuture
|
CompletableFuture
|
||||||
.runAsync(new Serve1(ss));
|
.runAsync(new SimpleServer(ss));
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -8,14 +8,14 @@ import java.io.*;
|
|||||||
import java.util.concurrent.atomic.*;
|
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 final int port;
|
||||||
public static final int PORT = 8080;
|
|
||||||
private static AtomicInteger idcount =
|
private static AtomicInteger idcount =
|
||||||
new AtomicInteger(0);
|
new AtomicInteger(0);
|
||||||
private final int id = idcount.getAndIncrement();
|
private final int id = idcount.getAndIncrement();
|
||||||
private InetAddress hostAddress;
|
private InetAddress host;
|
||||||
public SimpleClient(InetAddress hostAddress) {
|
public SimpleClient(InetAddress host, int port) {
|
||||||
this.hostAddress = hostAddress;
|
this.host = host;
|
||||||
|
this.port = port;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
@ -25,7 +25,7 @@ public class SimpleClient implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
System.out.println(this + "Running");
|
System.out.println(this + "Running");
|
||||||
try(
|
try(
|
||||||
Socket socket = new Socket(hostAddress, PORT);
|
Socket socket = new Socket(host, port);
|
||||||
BufferedReader in =
|
BufferedReader in =
|
||||||
new BufferedReader(
|
new BufferedReader(
|
||||||
new InputStreamReader(
|
new InputStreamReader(
|
||||||
|
@ -7,15 +7,18 @@ import java.io.*;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
public class SimpleServer implements Runnable {
|
public class SimpleServer implements Runnable {
|
||||||
|
private ServerSocket ss;
|
||||||
|
public SimpleServer(ServerSocket ss) {
|
||||||
|
this.ss = ss;
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public String toString() { return "Server: "; }
|
public String toString() { return "Server: "; }
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
System.out.println(this + "Running");
|
||||||
try (
|
try (
|
||||||
ServerSocket s =
|
|
||||||
new ServerSocket(SimpleClient.PORT);
|
|
||||||
// Blocks until a connection occurs:
|
// Blocks until a connection occurs:
|
||||||
Socket socket = s.accept();
|
Socket socket = ss.accept();
|
||||||
BufferedReader in =
|
BufferedReader in =
|
||||||
new BufferedReader(
|
new BufferedReader(
|
||||||
new InputStreamReader(
|
new InputStreamReader(
|
||||||
@ -24,7 +27,7 @@ public class SimpleServer implements Runnable {
|
|||||||
new PrintWriter(
|
new PrintWriter(
|
||||||
new BufferedWriter(
|
new BufferedWriter(
|
||||||
new OutputStreamWriter(
|
new OutputStreamWriter(
|
||||||
// Enable auto-flush:
|
// Boolean enables auto-flush:
|
||||||
socket.getOutputStream())), true)
|
socket.getOutputStream())), true)
|
||||||
) {
|
) {
|
||||||
System.out.println(this.toString() + socket);
|
System.out.println(this.toString() + socket);
|
||||||
|
@ -7,12 +7,14 @@ import java.util.concurrent.*;
|
|||||||
import onjava.Nap;
|
import onjava.Nap;
|
||||||
|
|
||||||
public class TestMultiServer {
|
public class TestMultiServer {
|
||||||
|
public static final int port = 8080;
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
CompletableFuture.runAsync(new MultiServer());
|
CompletableFuture.runAsync(
|
||||||
|
new MultiServer(port));
|
||||||
new Nap(1); // Let the server get started
|
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 SimpleClient(Local.host()));
|
new SimpleClient(Local.host(), port));
|
||||||
}
|
}
|
||||||
new Nap(4);
|
new Nap(4);
|
||||||
// No exceptions mean success
|
// No exceptions mean success
|
||||||
|
@ -2,17 +2,27 @@
|
|||||||
// (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.
|
||||||
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import onjava.Nap;
|
import onjava.Nap;
|
||||||
|
|
||||||
public class TestSimpleServer {
|
public class TestSimpleServer {
|
||||||
|
// Choose a port outside of the range 1-1024:
|
||||||
|
public static final int port = 8080;
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
CompletableFuture.runAsync(
|
try (
|
||||||
new SimpleServer());
|
ServerSocket ss =
|
||||||
CompletableFuture.runAsync(
|
new ServerSocket(port)
|
||||||
new SimpleClient(Local.host()));
|
) {
|
||||||
new Nap(1);
|
CompletableFuture.runAsync(
|
||||||
// Success if no exceptions happen
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user