-1

When we are write data using socket channel write method. It's taking too much time for small byte of data. For example if many client is connected to my application then it take 43950 ms to write 16249 byte data. It try 442 times to write above data.

public static void writeBytesFixedLength(SocketChannel sChannel, String msg){       
    ByteBuffer writeBuffer = ByteBuffer.allocate(msg.length()*2);
    writeBuffer.put(msg.getBytes());
    writeBuffer.flip();         
    long nbytes = 0;
    long toWrite = writeBuffer.remaining();
    int sleepCount = 0;
    int loopCount = 0;
    int sleepTime = 50;
    int sumSleepCount = 0;
    int sumSleepTime = 0;
    try 
    {
        int count = 0;
        while (nbytes != toWrite) {
            count= sChannel.write(writeBuffer);
            nbytes +=count;
            loopCount += 1;
            try {

                if(count == 0){            
                    OSUsage();
                    MainImapServer.printThreadStatus();
                    sleepCount += 1;
                    sumSleepCount += 1;
                    if(sleepCount == 4){
                        sleepTime = 50;
                        sleepCount =1;
                    }
                    sumSleepTime += sleepTime*sleepCount;
                    Thread.sleep(sleepTime*sleepCount);                      

                }
            }
            catch (InterruptedException e) {}
        }            
    }
    catch (ClosedChannelException cce) {}
    catch (Exception e) {}

    if(sumSleepCount >3)
    logger.info("Send buffer is full: Total Byte: "+ toWrite+ " sleepCount: "+sumSleepCount +" sleepTime: "+sumSleepTime+ " loopCount: "+ loopCount);
}

please provide some suggestion to over come this problem

Thanks in advance

1 Answers1

0

You are here asserting both that your socket channel write() call is blocking and that it is returning zero. These assertions are mutually inconsistent. You are mistaken. Both the API specification and the underlying mechanisms make this impossible. Either it is blocking or it is returning zero.

EDIT It appears from your code that it is returning zero. That means several things:

  1. It is not blocking.
  2. Your channel is in non-blocking mode.
  3. You're writing faster than the receiver is reading.

When you get zero you should register the channel with a Selector for OP_WRITE, call select(), and retry the write when the channel becomes writable; if that write succeeds, deregister OP_WRITE. None of this sleep() nonsense.

Evidently you need to read the Oracle NIO Tutorial. Or throw it all away and use blocking mode. java.net even. It's a lot simpler.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • what mistake i am doing, Could you please explain brief. please find the updated actual code written in my application. – RamChandra Bhakar Feb 18 '16 at 12:07
  • Many times write call return zero. And it's increases the cpu cycle. is it possible to write complete data in one shot – RamChandra Bhakar Feb 18 '16 at 12:31
  • Your first mistake is probably using non-blocking mode at all, if you don't know how it works. And get rid of the sleeps while you're at it. – user207421 Feb 18 '16 at 15:33
  • I have added Thread.sleep to overcome the cpu load. it's a multithreaded application and 8000 to 10000 user accessing simultaneously. And i also added the op_write and when it finish write, i have changed the op_read. but application behavior was same. – RamChandra Bhakar Feb 19 '16 at 05:21
  • You have adde the sleep bcuae you weren't using the Selector correctly. There is nothing in your question that feomonstartes any heed to use non-blocking mode at all. I don't and can't know what 'I have changed the op_read' means until you post the associated code, – user207421 Feb 20 '16 at 09:50