Unexpected exception in the selector loop. java.lang.Error: java.net.SocketException: Socket operation on nonsocket: getsockname ...

Wesly smith weslysmith0 at gmail.com
Tue Feb 3 21:57:36 EST 2009


I have a simple HttpClient which is modified from the netty example as below.
The client just do connect(), and then close() the connection in a loop for
testing the server availability. From time to time, I saw the exception as
below.
It is very easy to re-produce. I saw both on the apache tomcat and my
home-made http server.

Any suggestions about the problem itself or a better way to test the far-end
server availability will be highly appreciated. 

</pre>
connect 1233711200921
connect closed 1233711200921
connect 1233711215921
connect closed 1233711215937
connect 1233711230937
connect closed 1233711230953
Feb 3, 2009 7:33:50 PM org.jboss.netty.channel.socket.nio.NioWorker
WARNING: Unexpected exception in the selector loop.
java.lang.Error: java.net.SocketException: Socket operation on nonsocket:
getsockname
	at sun.nio.ch.Net.localAddress(Net.java:125)
	at sun.nio.ch.SocketChannelImpl.localAddress(SocketChannelImpl.java:430)
	at sun.nio.ch.SocketAdaptor.getLocalAddress(SocketAdaptor.java:147)
	at java.net.Socket.getLocalSocketAddress(Socket.java:697)
	at
org.jboss.netty.channel.socket.nio.NioSocketChannel.getLocalAddress(NioSocketChannel.java:88)
	at
org.jboss.netty.channel.socket.nio.NioWorker$RegisterTask.run(NioWorker.java:726)
	at
org.jboss.netty.channel.socket.nio.NioWorker.processRegisterTaskQueue(NioWorker.java:218)
	at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:157)
	at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.net.SocketException: Socket operation on nonsocket:
getsockname
	at sun.nio.ch.Net.localInetAddress(Native Method)
	at sun.nio.ch.Net.localAddress(Net.java:122)
	... 10 more
connect 1233711245953
connect closed 1233711245953
</pre>

The Sample Client code. You can use the Handler and PipelineFactory as in
the netty example for re-produce the problem.
<pre>
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpVersion;

public class HttpClient {

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println(
                    "Usage: " + HttpClient.class.getSimpleName() +
                    " <URL>");
            return;
        }

        URI uri = new URI(args[0]);
        String scheme = uri.getScheme() == null? "http" : uri.getScheme();
        String host = uri.getHost() == null? "localhost" : uri.getHost();
        int port = uri.getPort() == -1? 80 : uri.getPort();

        if (!scheme.equals("http")) {
            // We can actually support HTTPS fairly easily by inserting
            // an SslHandler to the pipeline - left as an exercise.
            System.err.println("Only HTTP is supported.");
            return;
        }

        // Configure the client.
        ChannelFactory factory =
            new NioClientSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool());

        ClientBootstrap bootstrap = new ClientBootstrap(factory);
        HttpClientPipelineFactory handler = new
HttpClientPipelineFactory(new HttpResponseHandler());
        bootstrap.setPipelineFactory(handler);
        bootstrap.setOption("keepAlive", true);

        // Send the HTTP request.
        for ( int loop = 0; loop < 10000; loop++)
        {
                System.out.println("connect "+System.currentTimeMillis());
                // Start the connection attempt.
                ChannelFuture future = bootstrap.connect(new
InetSocketAddress(
                                host, port));

                // Wait until the connection attempt succeeds or fails.
                Channel channel =
future.awaitUninterruptibly().getChannel();
                if (!future.isSuccess()) {
                        future.getCause().printStackTrace();
                        factory.releaseExternalResources();
                        return;
                }

                // just close after connection
                if (channel.isConnected() == false)
                {
                        System.out.println("Host is not reachable");
                }
                else
                {
                        channel.close();
                        System.out.println("connect closed
"+System.currentTimeMillis());
                }

                // sleep enough
                Thread.sleep(15000);
        }
        // Shut down executor threads to exit.
        factory.releaseExternalResources();
    }
}
</pre>
-- 
View this message in context: http://n2.nabble.com/Unexpected-exception-in-the-selector-loop.-java.lang.Error%3A-java.net.SocketException%3A-Socket-operation-on-nonsocket%3A-getsockname-...-tp2266877p2266877.html
Sent from the Netty User Group mailing list archive at Nabble.com.




More information about the netty-users mailing list