From avinash.lakshman at gmail.com Thu Sep 2 11:23:27 2010 From: avinash.lakshman at gmail.com (Avinash Lakshman) Date: Thu, 2 Sep 2010 11:23:27 -0700 Subject: AsynchronousSocketChannel write() In-Reply-To: <4C6CE911.4070606@oracle.com> References: <4C6CE911.4070606@oracle.com> Message-ID: Should I assume similar semantics with the UDP too? I/O is not completed in a single operation even in UDP. Avinash On Thu, Aug 19, 2010 at 1:19 AM, Alan Bateman wrote: > Avinash Lakshman wrote: > >> Hi All >> >> I want to use the write() on the AsynchronousSocketChannel which has no >> timeout semantics. I also want to pool these connections that I use. Now if >> I have multiple threads writing into the same connection, even though the >> writes are synchronized, I get a WritePendingException because a previous >> write has not completed. What is the general paradigm for handling this? Do >> I need to keep track of pending writes etc? >> > I wonder if this is the right API for what you are doing. If you've got > multiple threads wanting to write to the same stream then the writing will > need to be coordinated to avoid "corrupting" the stream. A write to a > channel is not guaranteed to write all the remaining bytes in the buffer and > so the buffer prepared by one of the threads may actually require several > I/O operations. The only thing I can suggest is that you maintain a queue of > buffers to be written, and use the the completion notification to initiate > the next write operation (which might be from the same buffer, or the next > buffer in the queue). I think there are other folks on this mailing list > that have been through this before and they might be able to advise or > provide samples from their own usage. > > -Alan. > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.openjdk.java.net/pipermail/nio-discuss/attachments/20100902/babf2853/attachment.html From Alan.Bateman at oracle.com Thu Sep 2 13:38:32 2010 From: Alan.Bateman at oracle.com (Alan Bateman) Date: Thu, 02 Sep 2010 21:38:32 +0100 Subject: AsynchronousSocketChannel write() In-Reply-To: References: <4C6CE911.4070606@oracle.com> Message-ID: <4C800B48.8030108@oracle.com> Avinash Lakshman wrote: > Should I assume similar semantics with the UDP too? I/O is not > completed in a single operation even in UDP. UDP/datagrams are different as each send/write will send a datagram; each receive/read will read a datagram. Your MTU size will limit the size of the datagram that you can use. The send/receive buffer sizes also come into play as datagrams may be discarded (on some platforms) if you attempt to send/write a datagram that is larger than the buffer size. Same thing the receive side. So the semantics are different and you probably won't want to do another send/write if there bytes remaining from the first send/write. -Alan.