SocketChannel.write() buffering problem???

Q

Qu0ll

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?

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
E

Esmond Pitt

Qu0ll said:
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.

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.
 
Q

Qu0ll

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.

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.

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top