0

I used java to make simple chat program with AsynchronousChannelGroup, But some received data does not reach to completed. or reach very slow (1s ~ 2s). (I think it's random to client. Some client reach well to server but some client reach very slow and finally disconnect)

I try to get data callback way using code below

class socket
channel.read(readBuffer, this, new CompletionHandler<Integer, AsyncSocket>() {
    @Override
    public void completed(Integer size, AsyncSocket socket) {
        ByteBuffer read = socket.readBuffer;
        socket.callback.onRead(socket, read, read.limit() - read.position());
        read.compact();
        socket.channel.read(read, socket, this);
    }
    @Override
    public void failed(Throwable e, AsyncSocket socket) {
        // for close and output err
        socket.closeSoft(new Exception(e));
    }
});

class socket
public synchronized void write(ByteBuffer buffer) {
    try {
        while (buffer.hasRemaining()) {
            channel.write(buffer).get();
        }
    } catch (Exception e) {
        this.close(e);
    }
}

class callback //server
public void onRead(AsyncSocket socket, ByteBuffer readBuffer, int size) {
    // write every client using write() if all data transformed;
}

The callback omitted for some client when I try to access, and with a long time, err "The semaphore timeout" is showed in closeSoft method.

I think occurring condition is random and I cannot get any idea of this exception. Please help me. Thanks!

fnclovers
  • 119
  • 9
  • You should not wait for write completion in write method: `channel.write(buffer).get()` – hoaz Dec 29 '17 at 16:27
  • Thanks for answering! I get an idea about some client use a lot of resources. – fnclovers Dec 29 '17 at 16:51
  • but completed does not occurs either checking with write method by start-end log, but when I accept localhost, it operates well. I think some internet circumstance block transform. thanks for answering again :) – fnclovers Dec 29 '17 at 16:56

0 Answers0