Commit 33ea524d authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] DailyAdviceServer while loop is now dependent upon the server being open.

parent 251b3414
Loading
Loading
Loading
Loading
+5 −7
Changes for src/main/java/ch15/DailyAdviceServer.java: 5 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -3,8 +3,6 @@ package ch15;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.Channels;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
@@ -26,13 +24,13 @@ public class DailyAdviceServer {

  public void go() {
    try {
      ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
      serverSocketChannel.bind(new InetSocketAddress("localhost", 4242));
      ServerSocketChannel serverChannel = ServerSocketChannel.open();
      serverChannel.bind(new InetSocketAddress("localhost", 4242));

      while (true) {
        SocketChannel sock = serverSocketChannel.accept();
      while (serverChannel.isOpen()) {
        SocketChannel clientChannel = serverChannel.accept();

        PrintWriter writer = new PrintWriter(Channels.newOutputStream(sock));
        PrintWriter writer = new PrintWriter(Channels.newOutputStream(clientChannel));
        String advice = getAdvice();
        writer.println(advice);
        writer.close();