Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Socket NIO connect not working under Solaris 10

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Socket NIO connect not working under Solaris 10

    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:

    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());
        }
    }
    You can run this using the command line:

    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 &
    Last edited by trojanfoe; July 4th, 2011 at 08:42 AM. Reason: Better test case.


Similar Threads

  1. Connect Four GUI
    By gromacs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 6th, 2011, 09:42 AM
  2. Need help to connect to PostgreSql
    By stab in forum JDBC & Databases
    Replies: 3
    Last Post: June 3rd, 2011, 09:01 AM
  3. Cant connect with database
    By ronn1e in forum JDBC & Databases
    Replies: 1
    Last Post: January 4th, 2011, 04:45 PM
  4. Cant connect with database
    By ronn1e in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 4th, 2011, 03:09 PM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM