questionersam
questionersam

Reputation: 1125

Java non-blocking socket write

I'm looking for suggestions for non-blocking socket writes in Java. My code just needs to write some data out to a socket and completely forget about it. I don't care about the response or when the data is actually consumed. What's the best way to do this?

Upvotes: 0

Views: 3832

Answers (4)

Gili
Gili

Reputation: 89993

Sounds to me like you're after asynchronous I/O. Here is a short example:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

public class Test
{
    public static void main(String[] args)
    {
        final AsynchronousSocketChannel out = AsynchronousSocketChannel.open();
        out.connect(new InetSocketAddress("www.google.com", 80), null,
            new CompletionHandler<Void, Void>()
            {
                @Override
                public void completed(Void result, Void attachment)
                {
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    buffer.asCharBuffer().put("GET /index.html HTTP/1.1");
                    out.write(buffer, null, new CompletionHandler<Integer, Void>()
                    {
                        @Override
                        public void completed(Integer result, Void attachment)
                        {
                            // ignore the result
                        }
                            @Override
                        public void failed(Throwable t, Void attachment)
                        {
                            t.printStackTrace();
                        }
                    });
                }

                @Override
                public void failed(Throwable t, Void attachment)
                {
                    t.printStackTrace();
                }
            });
    }
}

You don't need any fancy frameworks. Java7 asynchronous sockets are quite easy to use.

Upvotes: 1

kjp
kjp

Reputation: 3116

You can use the Selectors and Channels from Java NIO(complicated) - or you can try a more friendlier networking library like Apache MINA which supports non-blocking I/O.

Upvotes: 0

Chris Dennett
Chris Dennett

Reputation: 22721

Best idea when you get into complexities like this is to use a networking library like KryoNet (more complex, more features) or PryoNet (less complex, less features) which both support simple asynchronous I/O over sockets. You can pick them apart to understand how they work, if you really want to. Note that a write on a Socket only blocks if the send buffer is full.

If you don't take this route, to handle this simple case without thread synchronisation you need to use Selectors and Channels, as there's no way of easily ascertaining without a busy loop if a socket is 'writeable' at any point in time with no buffer fill level notification mechanism in the basic Socket class. You could also use the socket in a thread, then it can block indefinitely without a problem, but then you may have some synchronisation issues.

Upvotes: 1

Rocky Pulley
Rocky Pulley

Reputation: 23301

Create a thread which you post these events to, it the background it will run and submit the data to the server.

Upvotes: 0

Related Questions