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 / November 2007

Tip: Looking for answers? Try searching our database.

SocketChannel.write() buffering problem???

Thread view: 
Qu0ll - 11 Nov 2007 20:41 GMT
I have a ByteBuffer named "buf" which is created by the
ByteBuffer.allocate() method and is set to size 1024 bytes for each packet
to be sent over the network.  I also have a SocketChannel named "channel" to
which I want to write multiple ByteBuffer packets.  This is working but
after about 30 or so packets no data are transferred and the following code
shows that 0 bytes are being written for each subsequent packet:

    ++packetCount;
    int size = buf.remaining();
    int n = channel.write(buf);
    if (n < size) {
        System.out.println("PARTIAL WRITE (" + n + " < " + size + ") for
packet " + packetCount);
    }

Given that it works for a while, this smells of some kind of buffering
problem.  I know that output streams buffer things and that you can use the
flush() method to flush the buffer but I can't see how to do something
similar here given that I am writing directly to the channel.

Your thoughts?

Signature

And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

Esmond Pitt - 11 Nov 2007 23:11 GMT
> I have a ByteBuffer named "buf" which is created by the
> ByteBuffer.allocate() method and is set to size 1024 bytes for each
[quoted text clipped - 16 lines]
> the flush() method to flush the buffer but I can't see how to do
> something similar here given that I am writing directly to the channel.

It's not the channel, or anything else you can control from Java. It's
the TCP send buffer filling up, which means that the receiver's receive
buffer has filled up, which means that he is reading slower than you are
writing.

This is what OP_WRITE is for if you're using non-blocking I/O: register
on it and select(), and when it fires write again: if that succeeds
completely, deregister OP_WRITE. If you're using blocking I/O, just
write in a loop while buf.remaining() > 0.
Qu0ll - 12 Nov 2007 02:04 GMT
> It's not the channel, or anything else you can control from Java. It's the
> TCP send buffer filling up, which means that the receiver's receive buffer
[quoted text clipped - 4 lines]
> completely, deregister OP_WRITE. If you're using blocking I/O, just write
> in a loop while buf.remaining() > 0.

Thank you Esmond.  The problem does appear to have been that the client was
not processing the data quickly enough.  I have corrected that and all
writes are now going through fully.

Signature

And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)



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.