I am trying to write a program which accepts java nio SocketChannel Connections but also keeps them open. So lets say the Client sends a message, then should the Server immediately send the same message back (Simple Echo Server), but the answering process does not work. If the Client sends a new message the Server will not respond until I closed the Client's socketchannel. But after I closed the Client's channel all the previously sent Messages will come in one go. (Sorry for my broken English, it is not my native language)
The writing process is used for Server and Client side.
Writing process:
try {
final ByteBuffer byteBuffer = ByteBuffer.wrap(data);
while(byteBuffer.hasRemaining()){
socketChannel.write(byteBuffer);
}
byteBuffer.flip();
} catch (IOException exception) {
throw new BloumException(exception.getMessage());
}
Reading process:
final ByteBuffer byteBuffer = ByteBuffer.allocate(DefaultConnectionCreator.this.getDefaultBufferSize());
try {
while(socketChannel.read(byteBuffer) != -1){
//byteBuffer.clear();
}
} catch (IOException exception) {
exception.printStackTrace();
throw new BloumException(exception.getMessage());
}
return byteBuffer.array();
Key selection process (Auto close Returns false):
private void handleKeys(final ServerSocketChannel serverSocketChannel, Set<SelectionKey> keys, HashMap<SocketChannel, ByteBuilder> sessions) throws Exception{
final Iterator<SelectionKey> iterator = keys.iterator();
while(iterator.hasNext()){
final SelectionKey selectionKey = iterator.next();
iterator.remove();
if(selectionKey.isValid()){
if(selectionKey.isAcceptable()){
final ServerSocketChannel serverSocketChannel2 = (ServerSocketChannel)selectionKey.channel();
final SocketChannel socketChannel = serverSocketChannel2.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selectionKey.selector(), SelectionKey.OP_READ);
sessions.put(socketChannel, new ByteBuilder());
}else if(selectionKey.isReadable()){
final SocketChannel socketChannel = (SocketChannel)selectionKey.channel();
final ByteBuffer byteBuffer = ByteBuffer.allocate(Connection.MAX_BUFFER_SIZE);
final Integer bytesRead = socketChannel.read(byteBuffer);
if(bytesRead!=-1){
byte[] data = new byte[bytesRead];
System.arraycopy(byteBuffer.array(), 0, data, 0, bytesRead);
Boolean autoClose = true;
if(ConnectionHost.this.handle(this.getConnectionCreator().createConnection(socketChannel), data)){
autoClose=true;
}else autoClose=false;
if(autoClose){
sessions.remove(socketChannel);
socketChannel.close();
}else{
if(!sessions.containsKey(socketChannel))sessions.put(socketChannel, new ByteBuilder());
}
}
}else throw new BloumException("The given key is not supported.");
}else throw new BloumException("The key is not valid anymore.");
}
}
Hello you have a lot of mistakes there
Arrays.copyOfRange(buffer.array(), 0, buffer.position())
or if you
flipped the buffer then Arrays.copyOfRange(buffer.array(), 0,
buffer limit())