diff --git a/src/main/java/ru/khasanov/ClientSocketHandler.java b/src/main/java/ru/khasanov/ClientSocketHandler.java index 77240cc..bd4cce0 100644 --- a/src/main/java/ru/khasanov/ClientSocketHandler.java +++ b/src/main/java/ru/khasanov/ClientSocketHandler.java @@ -34,6 +34,7 @@ public boolean read() throws IOException { while (true) { readNumber = clientSocketChannel.read(byteBuffer); if (readNumber < 0) { + logger.error("Read error " + clientSocketChannel.getRemoteAddress()); return false; } else if (readNumber == 0) { return true; @@ -60,10 +61,6 @@ public void process(RequestHandler requestHandler) { } public void write() throws IOException { - int numBytes = clientSocketChannel.write(responseBytes); - if (numBytes > 0) { - selectionKey.interestOps(SelectionKey.OP_READ); - selectionKey.selector().wakeup(); - } + clientSocketChannel.write(responseBytes); } } diff --git a/src/main/java/ru/khasanov/Server.java b/src/main/java/ru/khasanov/Server.java index ec12d64..53e2833 100644 --- a/src/main/java/ru/khasanov/Server.java +++ b/src/main/java/ru/khasanov/Server.java @@ -50,7 +50,7 @@ private void handleAccept(SelectionKey selectionKey) throws IOException { SocketChannel clientSocketChannel = ((ServerSocketChannel) selectionKey.channel()).accept(); clientSocketChannel.configureBlocking(false); clientSocketChannel.register(selectionKey.selector(), SelectionKey.OP_READ); - logger.debug("Connection Accepted: " + clientSocketChannel.getLocalAddress()); + logger.debug("Connection Accepted: " + clientSocketChannel.getRemoteAddress()); } private void handleRead(SelectionKey selectionKey) throws IOException { @@ -60,7 +60,6 @@ private void handleRead(SelectionKey selectionKey) throws IOException { selectionKey.attach(handler); } if (!handler.read()) { - logger.error("Read error " + handler); close(selectionKey); return; } @@ -96,9 +95,7 @@ public ServerSocketChannel initServerSocketChannel() throws IOException { public void start() { try { Selector selector = Selector.open(); - ServerSocketChannel serverSocketChannel = initServerSocketChannel(); - serverSocketChannel.register(selector, serverSocketChannel.validOps()); while (true) {