I have a Java client that connects to a C++ server using TCP Sockets. I have implemented the client using Java NIO where a separate 'network thread' is responsible for using a single `Selector` object and performing all of the Socket operations (including opening, connecting and closing the Socket Channels). The worker threads 'post' instructions to the network thread, which carries them out on their behalf and calls back into their methods when the various `OP_xxx` events occur (the reads and writes also occur in the context of the network thread therefore).
This works under Linux, AIX and HP/UX but under Solaris the connect operation never completes. I register interest in just `OP_CONNECT` during the connect but the `SelectionKey` never fires.
Here is some test code which demonstrates the issue:
You can run this using the command line:import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class NioTest3 { public static void main(String[] args) { int i, tcount = 1, open = 0; String[] addr = args[0].split(":"); int port = Integer.parseInt(addr[1]); if (args.length == 2) tcount = Integer.parseInt(args[1]); InetSocketAddress inetaddr = new InetSocketAddress(addr[0], port); try { Set<SocketChannel> writeGuard = new HashSet<SocketChannel>(); Selector selector = Selector.open(); SocketChannel channel; for (i = 0; i < tcount; i++) { channel = SocketChannel.open(); channel.configureBlocking(false); channel.register(selector, SelectionKey.OP_CONNECT); channel.connect(inetaddr); } open = tcount; while (open > 0) { selector.select(); Iterator<SelectionKey> it = selector.selectedKeys().iterator(); while (it.hasNext()) { SelectionKey key = it.next(); channel = (SocketChannel)key.channel(); if (key.isConnectable()) { System.out.println("isConnectable"); if (channel.finishConnect()) { System.out.println(formatAddr(channel) + " connected"); key.interestOps(SelectionKey.OP_WRITE); } } else if (key.isWritable()) { if (!writeGuard.contains(channel)) { System.out.println(formatAddr(channel) + " isWritable"); String message = formatAddr(channel) + " the quick brown fox jumps over the lazy dog"; ByteBuffer buffer = ByteBuffer.wrap(message.getBytes()); channel.write(buffer); writeGuard.add(channel); } key.interestOps(SelectionKey.OP_READ); } else if (key.isReadable()) { System.out.println(formatAddr(channel) + " isReadable"); ByteBuffer buffer = ByteBuffer.allocate(1024); channel.read(buffer); buffer.flip(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); String message = new String(bytes); System.out.println(formatAddr(channel) + " read: '" + message + "'"); channel.close(); open--; } } } } catch (IOException e) { e.printStackTrace(); } } static String formatAddr(SocketChannel channel) { return Integer.toString(channel.socket().getLocalPort()); } }
java NioTest3 ipaddr:port num_connections
Where port should be 7 if you are running against a real echo service; i.e.:
java NioTest3 127.0.0.1:7 5
If you cannot get a real echo service running then the source to one is here. Compile the echo server under Solaris with:
$ cc -o echoserver echoserver.c -lsocket -lnsl
and run it like this:
$ ./echoserver 8007 > out 2>&1 &