mrniko / netty-socketio

Socket.IO server implemented on Java. Realtime java framework
Apache License 2.0
6.82k stars 1.65k forks source link

Address already in use on Tomcat7 #244

Closed just4give closed 9 years ago

just4give commented 9 years ago

I am trying to run SocketIO server inside Tomcat. I am not sure if this the right way to do that. But when application is deployed, I am seeing "Address already in user" exception. Below is my code.

@Component public class WebSocketManager {

private SocketIOServer server;

@PostConstruct
public void init() {
    System.out.println("Initializing SocketIO server");
    Configuration config = new Configuration();
    config.setHostname("localhost");
    config.setPort(8081);
    SocketConfig socketConfig = new SocketConfig();
    socketConfig.setReuseAddress(true);
    config.setSocketConfig(socketConfig);

    server = new SocketIOServer(config);
    server.addEventListener("chatevent", ChatObject.class,
            new DataListener<ChatObject>() {

                public void onData(SocketIOClient client, ChatObject data,
                        AckRequest ackRequest) {
                    System.out.println("on data");
                    server.getBroadcastOperations().sendEvent("chatevent",
                            data);
                }
            });

    server.start();
}

@PreDestroy
public void destroy(){
    System.out.println("Shutting down SocketIO server");
    if(server!=null){
        server.stop();
    }

}

}

Error Stack

java.net.BindException: Address already in use at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Net.java:444) at sun.nio.ch.Net.bind(Net.java:436) at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214) at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:125) at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:484) at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1080) at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:430) at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:415) at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:903) at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:197) at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:350) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:380) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) at java.lang.Thread.run(Thread.java:745)

mrniko commented 9 years ago

try another port, 8081 used by tomcat shutdown service

just4give commented 9 years ago

I tried several different ports on mac such as 4000, 7777, 8888 and I got exact same error all the time. Above WebSocketManager class is loaded by spring container at startup. Wondering if this is related to running SocketIO inside Tomcat7. Can anyone please direct me to any sample code base which runs on Tomcat7 ?

mrniko commented 9 years ago

may be your ports under the firewall?

just4give commented 9 years ago

Firewall is turned off on my system. Also when I run the server from standalone java class, SocketIO server starts up successfully. Problem is while running within tomcat7. Any clue ?

Below is the standalone program which runs properly.

public class WebSocketApplication {

public static void main(String[] args) throws InterruptedException {

    Configuration config = new Configuration();
    config.setHostname("localhost");
    config.setPort(7777);
    SocketConfig socketConfig = new SocketConfig();
    socketConfig.setReuseAddress(true);
    config.setSocketConfig(socketConfig);

    final SocketIOServer server = new SocketIOServer(config);
    server.addEventListener("chatevent", ChatObject.class,
            new DataListener<ChatObject>() {

                public void onData(SocketIOClient client, ChatObject data,
                        AckRequest ackRequest) {
                    System.out.println("on data");
                    server.getBroadcastOperations().sendEvent("chatevent",
                            data);
                }
            });
    System.out.println("Starting SocketIO server");
    server.start();
    Thread.sleep(30*1000);
    System.out.println("Stopping SocketIO server");
    server.stop();
}

}

mrniko commented 9 years ago

I use netty-socketio in production with tomcat 7 and it works without any problem. No any port binding issues.

just4give commented 9 years ago

Did you make any changes in Tomcat configuration? If possible, can you please share the working code?

mrniko commented 9 years ago

no, i didn't change ports in tomcat. Tomcat uses 8080 port and socketio 7272 or 9292

xianwei commented 9 years ago

@just4give I have the same issue.Do you have the answer about this? Sorry for my English.

just4give commented 9 years ago

@xianwei I never got it working after couple of try. I have to look back.