Does SocketChannel.write() block

I have a server communicating with it's clients using NIO and therefore SocketChannel's. When data are send to clients, the method SocketChannel.write(ByteBuffer) are used. The question is then, does the execution time depend of SocketChannel.write() depend on the speed of the client to receive data? Does the the method block in any way, or does it just send what is possible and then returns?

If you have the channel in blocking mode, it also depends on how fast the client is reading. If the client isn't reading at all, ultimately its receive buffer will fill up, then the sender's sending buffer will fill, and then the write will block waiting for space in the send buffer; once the client starts reading again, space will become available and the write can complete. That's not to say that every write waits for the completion of every prior read, it's a matter of buffering and windowing, and the effect is rather decoupled because of the presence of two intermediate buffers. However it certainly can occur.

Similar Messages

  • SocketChannel.write() blocking application

    Greets,
    I'm developping a huge application and got a latency/block problem using the write(ByteBuffer) method on a Socket from a socketchannel connection.
    Running java 1.5 (diablo) on Freebsd 6 servers, 4Gb ram (2.2 allocated to jvm), with dual xeon dual-core (total 4 cores)
    Here is the application schema :
    - A thread accepting connexion on the socketchannel
    - A thread selecting keys with data to process, enqueuing it after some basic checks on a command FIFO
    - A thread getting commands from the FIFO and processing 'em, generating answers on 4 answer FIFOs
    - 4 threads (1 per FIFO) to get answers and send 'em back to the socket.
    The application usually runs with 4500-5000 simultaneous clients.
    My problem is that the only write() method sometimes takes over 20ms to write a message, with a length smaller than 50 bytes.
    As I got about 25000 answers to process each second, when some of 'em decide to be slow, the 4 threads runs slowly, and all the connected clients are suffering of that latency, for the few minutes needed to empty the FIFOs.
    Every client socket get about 5 answers per second.
    On about 1 hour running, there are about 3 'peaks' of slowness, that I cannot explain yet. That's why I'm in need of advices !
    I monitored the application when such case happens. TOP indicates 40% cpu idle, JVM memory got >500Mb free, network runs @ about 1.2Mbps, where maximal transfer rate is >20Mbps. netstat -m told me no erros, and a large amount of free buffers available.
    As the only slow process is the write() method that usually runs faster than 1ms each call, but in those case I got delays over 20ms.
    freebsd tcp default sendbuffer size is 64k, receive buffer is 32k
    Commands average received size is below 1k, Answers average sending size below 8k.
    This application is running live, and as I cannot emulate 5000+ connections with a similar beahviour to test withour being sure that won't crash all.
    What points could be responsible of such slow write() calls ? Seems it's not CPU, not RAM, not network itself...
    I suppose it's the network buffers that are causing problems. But I don't really know if I have to fit 'em to a lower size, fitting my requirements, or to a larger size, to be sure there won't be full buffers blocking all ?
    I need advices. Thanks for your ideas !
    Bill

    Hmm. So you're happy to lose data?
    A few comments:
    (a) SocketChannels are thread-safe. I don't think you need the synchronization at all, unless maybe multiple writing threads are possible. I would eliminate that possibility and the sync myself.
    (b) If you're getting write delays of 30ms occasionally, the sync must also take 30ms at the same points if it is doing anything at all, i.e. if the possibility of multiple writing threads does exist. So maybe it doesn't?
    (c) I would have a good look at this:
    http://forum.java.sun.com/thread.jspa?threadID=459338
    and specifically the part on how to manage a channel that presents write blocks, using OP_WRITE when it happens and turning it off when it doesn't.
    (d) You seem to be using one output buffer for all channels. You might be better off using a small one per channel. Then that way you don't clear, you just do put/flip/write/compact, and if the write returned 0 just post OP_WRITE for next time around the select loop. Then you won't lose any data at all, except to a client who really isn't reading: you can detect that situation by keeping track of the last successful write time to a channel, and when there is pending data and the last write is too long ago have a think about what the block means in terms of the application. Maybe you should just disconnect the client?
    (e) It would be interesting to know how many times the write loop looped when you get these large delays, and also what the data size was, and also to know that for the other cases to see if there is a difference.
    (f) Generally from a fairness point of view I prefer not to have write loops, just one attempt and if it returns even a short read I post OP_WRITE as above. Otherwise you're spending too long servicing one channel.
    You can contact me offline via http://www.telekinesis.com.au if you like.

  • Exceptions not thrown on interrupted SocketChannel.write()

    I just noticed a behaviour of java.nio.channels.SocketChannel.write() that makes me wonder. If write() blocks(), and the channel is closed during this blocking by another thread, AsynchronousCloseException should be thrown.
    However, in most cases this does not happen in my little test app. If any part of the data passed to write() has already been written to TCP before the socket was closed, write() returns without exception.
    Similar behaviour is observed with intterupting. If the thread blocked in write() is interrupted by another thread, it returns immediately and has the interrupted Flag set, but in most cases no Exception is thrown.
    ClosedByInterruptException is only thrown if not any part of the data passed to write() has been passed to TCP.
    Is this a bug or a feature ?

    Yes, i'm pretty sure that it blocks. In my test, the server just accepts the connection and then goes to sleep for a looooong time. The client just connects, and sends 10Meg of fata in one write() call. If i do not interrupt the client, it blocks as long as the server sleeps. In this case, this is the client's stack while blocking:
    Thread [main] (Suspended)     
         FileDispatcher.write0(FileDescriptor, long, int) line: not available [native method]     
         SocketDispatcher.write(FileDescriptor, long, int) line: 29     
         IOUtil.writeFromNativeBuffer(FileDescriptor, ByteBuffer, long, NativeDispatcher, Object) line: 104     
         IOUtil.write(FileDescriptor, ByteBuffer, long, NativeDispatcher, Object) line: 75     
         SocketChannelImpl.write(ByteBuffer) line: 334     
         Channels.write(WritableByteChannel, ByteBuffer) line: 60     
         Channels.access$000(WritableByteChannel, ByteBuffer) line: 47     
         Channels$1.write(byte[], int, int) line: 134     
         Channels$1(OutputStream).write(byte[]) line: 58     
         SocketClient.main(String[]) line: 75     If i start another thread before calling write(), that closes the socket after 3 seconds, the following happens: The call of IOUtil.write() returns ( with a value n that i cannot see in the debugger ), This value n is tested inside SocketChannelImpl.write() via (n > 0 || (n == IOStatus.UNAVAILABLE), what return true. For that reason, AbstractInterruptibleChannel.end(boolean), does not throw an exception.
    Once the server wakes up later, it is able to read about 200K from the socket...
    I tried this on a linux system (kernel 2.6.17, glibc 2.4) with jdk 1.6.0_03. I'm now gonna try it under windows, hold on..
    Of course, if anybody is interested, i'll post the test proggy...

  • SocketChannel.write() on J2SDK1.4.2_04/Windows 2000

    Hello,
    I have a question regarding SocketChannel.write() (as I mentioned before, I am using J2SDK 1.4.2_04 on Win 2000 pro).
    I prepared a ByteBuffer with some binary data. When I invoke SocketChannel.write(), the other application receives the data only until the first null (byte = 0) character is found??? What am I doing wrong??
    Thanks.

    On the other side, I get the message
    Read: ZYKT
    (only the first 4 bytes of the message)
    I experimented adding more non-null characters and the
    result was
    Read: ZYKT08@AYes, but (again) what does the CODE look like? Maybe it's written incorrectly to stop when it gets a null byte. Or maybe it stuffs it into a C-style char array (which is null-terminated by convention) and just happens to print up to the null (as C-style char array functions are designed to do). Or, who knows? You're not showing what that client process looks like.

  • SocketChannel.write()

    I've written a non-blocking server and am trying to test it with a client that uses the traditional (blocking) I/O. The problem I'm running into is when the server tries to write data to the client, it seems to take an inordinate amount of time.
    Often times the SocketChannel.write() method will return zero and write nothing to the socket when it has a perfectly good ByteBuffer available to be written. The Javadoc for the SocketChannel.write() states this is normal:
    Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.
    So I put the following statement in the code:
    while ((retVal = socketChannel.write(outputBuffer)) == 0);However, this is proving to be quite slow, sometimes taking over half a second to complete a write of around 1000 bytes.
    While this solution delivers all of the data, it is slow. Has anybody run into this, and if so, found a better solution?
    Thanks,
    Ken

    I feel compelled to reply to myself to keep this alive, so to add:
    It seems like the delays being experience here would not happen with blocking sockets and that this is a function of NIO. What I don't understand is why socket channel takes so long to know when it can write again. Waiting over half a second for one write of a 1000 byte message is not acceptable.
    Is it because I am not using a Selector? Would that tell me the channel is ready any faster? I wouldn't see why. Or is it because the client application is reading the data too slowly?
    Any help would be appreciated.
    Ken

  • Write blocked waiting on read

    Hi all,
    I have been experiencing difficulties with simultaneous read/write with AsyncIO. For my scenario that the client/server application is being developed, a client may send requests or status information at any given time. Hence continual monitoring on the incoming stream is required.
    The server shall respond depending upon the status message requiring a write to the client (so it won't respond to all messages received). Likewise, the client in some instances will only send status messages depending on the last message it received from the server.
    I've been experiencing difficulties with writes blocking because of reading. Essentially I would like to continually poll reading whilst allowing writes to be flushed immediately.
    Using traditional blocking read/writes things hang indefinitely whilst attempting to write a message as reading has blocked waiting for input.
    Using the IBM AsyncIO package (which is purported to be faster implementation of NIO), writing blocks for some time until reading (i assume) relinquishes the socket to allow writing to occur before resuming reading again. The lag time in this situation is significant.
    Is someone able to provide an example using non-blocking R/W in which a server can sit reading (on one thread) whilst the writing is attempted on another thread that doesn't cause any lag?
    Below is a basic overview of what is happening in my software:
    public class MessageQueue {
       private LinkedList<Message> queue;
       /** Creates a new instance of MessageQueue */
       public MessageQueue() {
          queue = new LinkedList<Message>();
       public synchronized void put(Message message) {
          queue.add( message );
          notifyAll();
       public synchronized boolean isEmpty() {
          return queue.isEmpty();
       public synchronized Message get() {
          while( queue.isEmpty() ) {
             try {
                wait();
             } catch( InterruptedException ie ) {
          Message message = ( Message )queue.removeFirst();
          return message;
       public synchronized void close() {
          queue.clear();
          queue = null;
    public class InputReader implements Runnable {
      private MessageQueue messages;
      private AsyncSocketChannel async_channel;
      public InputReader(MessageQueue messages, AsyncSocketChannel async_channel) {
        this.messages = messages;
        this.async_channel = async_channel;
      public long read(ByteBuffer b) {
         long bytes_read = 0;
         helper = new AsyncSocketChannelHelper( this.async_channel );
         future = channel.read(b);
         bytes_read = future.getByteCount( );
         return bytes_read;
      public void run() {
         ByteBuffer b = ByteBuffer.allocateDirect(Message.SIZE);
         boolean running = true;
         while(running) {
            if (read(b) == 0)
              running = false;
            else
              messages.put(new Message(b));
    public class OutputWriter implements Runnable {
      private MessageQueue messages;
      private AsyncSocketChannel async_channel;
      public OutputWriter(MessageQueue messages, AsyncSocketChannel async_channel) {
        this.messages = messages;
        this.async_channel = async_channel;
      public long write(ByteBuffer b) {
          long bytes_written = 0;
          try {
             AsyncSocketChannelHelper helper = new AsyncSocketChannelHelper( this.async_channel );
             IAsyncFuture future = helper.write(b, 20000); // write or timeout
             // wait for completion of write, or for the timeout to happen
             bytes_written = future.getByteCount( );
             // THIS IS WHERE THE PROBLEM LIES. The write does not happen straight away because of the read operation. With traditional blocking IO this locks completely.
          } catch ( AsyncTimeoutException ate) {
                  System.err.println("Timed out after 20 seconds");
          return bytes_written;
       public void run() {
         boolean running = true;
         while(running) {
            Message m = this.messages.get();
            if (write(m.getByteBuffer()) == 0)
              running = false;
            else
              messages.put(new Message(b));
    public class Controller {
       public Controller(AsyncSocketChannel async_channel) {
            MessageQueue in = new MessageQueue();
            MessageQueue out = new MessageQueue();
            InputReader ir = new InputReader(out, async_channel);
            OutputWriter ow = new OutputWriter(out, async_channel);
            new Thread(ow).start();
            new Thread(ir).start();
            boolean running = true;
            Message m;
            while(running) {
               m = in.get();
               if (m.getStatus() == "REQUIRES_RESPONSE"))
                 out.put(m); // dummy example to demonstrate that once the right condition is met, a new message must be written
    }

    That makes me wonder what the problem is I am experiencing then.
    I initially had stock-standard java.net IO for socket reading and writing. The approach I took was to set up an input reader on its own thread and an output writer on its own thread.
    When it came to writing data however, things locked up. Stepping through the code in debug mode allowed me to see that the write method was not completing because the read method was waiting for input to come in.
    I tested it using traditional buffered output which made a call to flush() afterwards, but it was getting stuck on the write() call.
    I came to the conclusion that the read must be blocking the write from completing because of the response to this thread: http://forum.java.sun.com/thread.jspa?forumID=536&threadID=750707
    On further debugging, write() never locked up when I didn't allow the input reader to perform a simultaneous read() on the socket.
    Hence my belief that the java.net socket does block when one operation is being performed.
    After dealing with IBM's AsyncIO package I'd be willing to wager that their ibmaio package is 50x more complex to use than standard Java sockets (barely any documentation/examples) so 10x complexity seems positively lightweight ;-)
    So ejp, to clarify, would NIO help solve this blocking problem or do you think something else is the culprit? It is hard to see what as to test things out I made two bare bones testing programs (one client and the other a server) so I don't feel it could be anything else in the code.
    Thoughts?

  • SelectionKey.OP_WRITE versus SocketChannel.write(ByteBuffer)

    I'm writing a small Socket server using Non blocking approach based on the ractor design pattern.
    Many samples use the OP_WRITE on the selection keys to know when the channel is ready to accept writes.
    I use the SocketChannel.write(ByteBuffer) whenever I need to write instead.
    Is there a reason why SelectionKey.OP_WRITE is prefered in the samples??????
    Thanks
    Stéphane

    Rainman4500 wrote:
    I'm writing a small Socket server using Non blocking approach based on the ractor design pattern.
    Many samples use the OP_WRITE on the selection keys to know when the channel is ready to accept writes.
    I use the SocketChannel.write(ByteBuffer) whenever I need to write instead.
    Is there a reason why SelectionKey.OP_WRITE is prefered in the samples??????
    So that you don't attempt a write unless the channel can accept some data from you. In your example, suppose you actually transfer 0 bytes because the output buffer is still full. What do you do then?

  • Socketchannel  write(bytebuffer)

    I use nio socketchannel write(bytebuffer [])method.
    In my program,as following...
    buff.clear();
    sc.read(buff);
    buff.flip();
    sc.write();
    sc.close();//Ok,when this line is not been marked.
    In another program,
    msg(String s)
    buff.get(s.getBytes());
    buff.flip();
    sc.write(buff);//No close(),and problems come,in design,
    sc can not close()
    How to solve the problem?

    You didn't show any client-side code, did you? Are you using a selector? If so, it could be that your select logic is wrong, causing you to miss when the socket first becomes readable (happened to me, for instance).
    When you ctrl-c the server, a TCP FIN packet gets sent to the client - if you were blocked in a select on the client side, it'd break out - and since the data in the TCP receive queue would still be there to read, your application would be able to read it out before getting an EOF on the socket.
    --bruce                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Why does Firefox always block any link I try to open going to a web page when I don't have pop up blocker on?

    Why does Firefox always block any link I try to open going to a web page when I don't have pop up blocker on? First off they are not even pop ups....your crap blocks everything and don't save user names &/or passwords when you have it checked. Also in your tutorials about solving this issue says it has the word "OPTION" .......no, it never says option to allow you to bypass it and etc. Only thing is says is "ALLOW" . That would be nice , but the problem is it will not allow it from then on. It only allows it right then and then you have to refresh...after that to open again it blocks it again.....HOW STUPID?!!!!!
    It always say that this is preventing this page from automatically redirecting to another page. THIS IS WHAT I AM TRYING TO DO...GO TO ANOTHER PAGE........I DON'T NEED YOUr BROWSERS PERMISSION TO DO THAT. THAT IS WHAT A *%#$&*(&*%$$%% BROWSER IS FOR TO GO TO OTHER PAGES......THEY ARE NOT POP UPS!!!!!
    YOU STUPID BROWSER ACTUALLY BLOCKS EVERYTHING AND THAT SUX!!!
    HELP!!

    Are you using "Clear history when Firefox closes"?
    In case you are using "Clear history when Firefox closes":
    *do not clear the Cookies
    *do not clear the Site Preferences
    *Tools > Options > Privacy > Firefox will: "Use custom settings for history": [X] "Clear history when Firefox closes" > Settings
    *https://support.mozilla.org/kb/Clear+Recent+History
    Note that clearing "Site Preferences" clears all exceptions for cookies, images, pop-up windows, software installation, and passwords.
    Clearing cookies will remove all specified (selected) cookies including cookies with an allow exception that you want to keep.
    Let all cookies expire when you close Firefox.
    *Firefox/Tools > Options > Privacy > Cookies: Keep until: I close Firefox
    You can choose to keep third-party cookies from visited sites if necessary.
    Create a cookie 'allow' exception for cookies that you want to keep.
    *Firefox/Tools > Options > Privacy > Cookies: Exceptions

  • HT201991 How does one write a review for an app?

    I would like to write a review for an app I bought from the App Store.
    "Locate the item that you would like to write a review for."
    Where? In the App Store via the internet (e.g. on my iMac), OR via the App Store button on my iPad Mini?
    "Click "Write a Review" in the Customer Reviews section."
    How? I could find no active link associated with the Customer Reviews section once I had located the item for which I wanted to write a review.
    So the question remains "How does one write a review for an app?" … and, so far as things stand, the instructions provided in the page <http://support.apple.com/kb/ht3928> are, as far as I can tell useless. Certainly, the Apple website and associated instructions have become, for this long-time user, more akin to Microsoft Windows in their impenetrable lack of intuitive, user-friendly ccharacter.
    Oh yeh. To add to my 'disgruntledness' … How come there is no iPad Mini option in the Product drop down below?
    Where is Steve Jobs when you need him?!

    I have been having this issue for months, now--at least 3-4.  I updated my credit card info on my account, thinking maybe that would do it, but NOPE--can't review a single thing I've ever purchased in iTunes--not from my iPhone 5, PC or iPad 2.  Ridiculous, at this point...just remove the ability to review at all, if this is how it's going to be. SO frustrating.

  • How does oracle write to datafiles in a tablespace?

    hi all
    Suppose I have a tablespace consisting of two datafiles. When I need to wirte data onto it,how does ORACLE
    write ? Does it initially write to the first datafile and then write to the second datafile when the first datafile getting full or write to two datafiles in an random manner?
    Thanks for your reply.

    hi all
    I have read reply in reponse to thread "tablespaces or datafile " and testcase in that thread shows that ORACLE will write to datafiles in round-robin manner.
    But my test show totally different result:
    SQL>
    SQL> create tablespace ts_maoxl
    2 datafile '/crash/oradata/TESTDB/datafile/1.bdf' size 5m,
    3 '/crash/oradata/TESTDB/datafile/2.bdf' size 5m
    4 EXTENT MANAGEMENT LOCAL;
    Tablespace created
    SQL>
    SQL> create table maoxl(id char(2000),id3 char(2000),id2 char(2000)) tablespace ts_maoxl;
    Table created
    SQL>
    SQL> declare
    2 begin
    3 for i in 0..99 loop
    4 insert into maoxl values('x','y','z');
    5 end loop;
    6 commit;
    7 end;
    8 /
    PL/SQL procedure successfully completed
    SQL> select count(*) from maoxl;
    COUNT(*)
    100
    SQL> select t.file_id,t.extent_id,bytes from dba_extents t where tablespace_name='TS_MAOXL';
    FILE_ID EXTENT_ID BYTES
    5 0 65536
    5 1 65536
    5 2 65536
    5 3 65536
    5 4 65536
    5 5 65536
    5 6 65536
    5 7 65536
    5 8 65536
    5 9 65536
    5 10 65536
    5 11 65536
    5 12 65536
    5 13 65536
    14 rows selectedAll extents are allocated from datafile 5,none from datafile 6. My result is different from yours.
    BTW,The result of my testcase was from a 10.2.0.4 database running on HP-UNIX
    What could be the problem?
    thanks

  • If you block a phone number in iMessage on the iPad, does it also block it on the iPhone?

    If you block a phone number in iMessage on the iPad, does it also block it on the iPhone?

    Depends on whether you are using the same iCloud account on the device, or if you are syncing Contacts via iTunes, if you are syncing both devices to the same computer.
    When you block someone, no matter which application you "launch" the block from (Messages, Phone, FaceTime) you are blocking the Contact. So, any app that uses that contact will see it as a blocked contact and will block any incoming activity to that app from that contact.
    Here is a Support Article with more info:
    http://support.apple.com/en-us/HT201229
    Cheers,
    GB

  • What does SQL write to that allow changes to be undone or rolled back in case of a system failure?

    What does SQL write to that allow changes to be undone or rolled back in case of a system failure?

    What does SQL write to that allow changes to be undone or rolled back in case of a system failure?
    Hello,
    If i read your question correctly you are asking about how uncommited transaction are rolled back.For this, concept of transaction log came into picture.where first changes were first made to transaction log and then into memory .Before any change is made
    to real data file change is made to trn log files so that if rollback comes it can see in log and rollback it.
    I would suggest you to read Famous article by log which will explain you
    http://technet.microsoft.com/en-us/magazine/2009.02.logging.aspx
    Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers

  • Why does Safari say "Blocked Plug-in" when I want a video?

    Why does Safari say "Blocked Plug-in" when I want a video?

    When you have installed the latest version of Flash, relaunch Safari and test.
    If you're getting a "blocked plug-in" error, then in System Preferences… ▹ Flash Player ▹ Advanced
    click Check Now. Quit and relaunch your browser.

  • Publish to DVD says it completes, does not write the ISO file in version 13.

    When I publish to DVD and select ISO it says it completes but it does not write the ISO file in version 13.

    apsteinmetz
    The DVD disc which is labelled 4.7 GB/120 minutes is in reality 4.38 GB. And the DVD disc which is labelled 8.5 GB/240 minutes is in reality 7.95 GB.
    What version of Premiere Elements and on what computer operating system is it running? Can we assume 13/13.1 on Windows 8.1 64 bit?
    What are you doing, a burn to disc DVD-VIDEO standard or widescreen on DVD disc or burn to disc AVCHD on DVD disc?
    When you get to the burn dialog (and you have your disc in the burner tray), what does the Quality area of the burn dialog show for Space Required and Bitrate with and without a check mark next to "Fit Contents to Available Space"?
    Or, can you do burn to Folder (4.7 GB) and/or burn to ISO Image (4.7 GB)? Are your successful burn to .iso files under 1 GB in file size?
    Please quantitate "...shortening the timeline dramatically".
    Thank you. Any questions or need clarification, please do not hesitate to ask.
    Thank you.
    ATR

Maybe you are looking for