Skip to content

Commit

Permalink
Поправил запись
Browse files Browse the repository at this point in the history
  • Loading branch information
khasanovbi committed Jan 10, 2017
1 parent 09891bd commit a6c260b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
7 changes: 2 additions & 5 deletions src/main/java/ru/khasanov/ClientSocketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
5 changes: 1 addition & 4 deletions src/main/java/ru/khasanov/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a6c260b

Please sign in to comment.