Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

writing to sockets

Thread view: 
Andersen - 22 Jan 2006 22:00 GMT
In non-blocking NIO, why do I need the OP_WRITE flag to be set in an
interest set ever? I understand that OP_READ indicates that new data is
ready to be read. But for writing, should I not only need to do
Channel.write(buffer) whenever I feel for it?
Stefan Schulz - 22 Jan 2006 22:32 GMT
In nonblocking mode, if the write buffer of the socket is full, just
"blasting away" with writes will cause a large number of zero-length
writes. If inside a tight loop, you would effectively busy-wait for
sending of data, which would clear some of the buffer  (much like not
looking at a selector, and just calling read() repeatedly would
busy-wait for arrival of data).
Andersen - 22 Jan 2006 22:47 GMT
Gotcha, so I can have my data (can be huge) stored into Collections,
then when WRITE is ready, move it over to a ByteBuffer and output to the
channel. That is the best strategy?

The reason I am having the Collections is that I want to have a per
connection buffer (for simple design), if I would allocate big
ByteBuffers for each connection, that would give me memory problems when
 I have many connections (a few thousands). So Collections resize
nicely, then use ByteBuffers and feed the channel...

> In nonblocking mode, if the write buffer of the socket is full, just
> "blasting away" with writes will cause a large number of zero-length
> writes. If inside a tight loop, you would effectively busy-wait for
> sending of data, which would clear some of the buffer  (much like not
> looking at a selector, and just calling read() repeatedly would
> busy-wait for arrival of data).
Andersen - 22 Jan 2006 23:55 GMT
Page 134 of Java NIO (Ron Hitchens, O'Reilly) calls sayHello(channel)
which does write without checking for readiness. Is this bad programming
style?

> In nonblocking mode, if the write buffer of the socket is full, just
> "blasting away" with writes will cause a large number of zero-length
> writes. If inside a tight loop, you would effectively busy-wait for
> sending of data, which would clear some of the buffer  (much like not
> looking at a selector, and just calling read() repeatedly would
> busy-wait for arrival of data).
Stefan Schulz - 23 Jan 2006 00:14 GMT
Well, that depends: If the channel is in blocking mode, the call is
just fine. If however, the call is made in nonblocking mode, only part
of the data (possibly none) might be written, which is not a good thing.
Gordon Beaton - 23 Jan 2006 06:50 GMT
> In non-blocking NIO, why do I need the OP_WRITE flag to be set in an
> interest set ever? I understand that OP_READ indicates that new data
> is ready to be read. But for writing, should I not only need to do
> Channel.write(buffer) whenever I feel for it?

You can write without OP_WRITE as long as write() succeeds. When
write() returns 0, wait until OP_WRITE is indicated before attempting
to write again.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.