Avatar of weznme
weznme
 asked on

Flushing a JDK1.4.x java.nio.SocketChannel

Hi folks!

I'm using a non-blocking java.nio.SocketChannel for high-performance multi-threaded client to server communication. Everythings works as intended, but when I shutdown my client application (which ultimately exits the sender thread), not all data is sent to the server.

This behaviour is due to the inability to check if the java.nio.SocketChannel has sent all bytes; there is no method like flush() to force the data to be sent over the wire or writeFinished() which could be polled until everything has been written.

Here is some pseudo code without synchronization and thread issues to clarify the problem:

The SocketChannel is created like this:

SocketChannel socketChannel = SocketChannel.open();
socketChannel.socket().setTcpNoDelay(true);
socketChannel.connect(new InetSocketAddress(serverHost, serverPort));
socketChannel.configureBlocking(false);

A java.nio.ByteBuffer is used to store events/messages asynchronously:
ByteBuffer buffer = new ByteBuffer;
ByteBuffer.allocateDirect(512*1024); // 512kb

When the buffer is full, I call buffer.flip() and send it repeatedly via socketChannel.write(buffer) until buffer.remaining() == 0.

When the client application exits, I made sure that buffer.remaining() == 0 but due to the very nature of NIO I can't make sure the data is actually sent over the wire. It only means that the native NIO buffer now contains the contents of ByteBuffer (or some parts of it).

Right now, I can only work around this problem by inserting a highly esotheric Thread.sleep(500) just before the client application exits and hope that the contents of the native nio.SocketChannel's bytebuffer (take a look at IOUtils) have been sent completely over the wire.

Question: How can i make sure that all data has been sent to the server before client application has been exited?

Kind regards,
weznme
Java

Avatar of undefined
Last Comment
weznme

8/22/2022 - Mon
CEHJ

Do you call shutdownOutput on the channel?
CEHJ

I mean on the Socket
ASKER CERTIFIED SOLUTION
Mick Barry

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
weznme

ASKER
CEHJ: java.nio.SocketChannel doesn't implement shutdownOutput().
objects: The SocketChannel is unidirectional, it doesn't read any data. I'm not able to change the client/server protocol.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
weznme

ASKER
CEHJ: As I'm using SocketChannel.open() to get the SocketChannel, I'm unable to access the associated socket itself directly.
Mick Barry

w/out some indication from the server theres not much u can do
weznme

ASKER
Well thank's for trying... maybe that's the wrong type of question to ask here.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.