开发者

Tomcat, open a socket - doesn't work

开发者 https://www.devze.com 2023-04-08 18:33 出处:网络
My web-app needs to talk to a device in my local network via a soket. I am trying to open a ServerSocket on port > 45000 and listen to it for incoming connections. The class that implements this worke

My web-app needs to talk to a device in my local network via a soket. I am trying to open a ServerSocket on port > 45000 and listen to it for incoming connections. The class that implements this worked perfectly well while i was developing it and running it as a stand-alone thing, but it throws an exception when i try to run it as a part of my web-app under Tomcat.

What can i do to fix it? I do need to listen to that port, and it is not HTTP traffic that i receive through it, so i can't listen to it by opening it in the config.xml file.

Here is, basically, the code that fails:

private void start() {
    ServerSocket s = 0;
    try {
        s = new ServerSocket(45678); // this is line 38 (see stacktrace)
    } catch (IOException e) {
        Misc.log("Could not listen on port: 45678.\n" + Misc.getStackTrace(e));
//      System.exit(-1);
    }
    ...
}

Here is the stacktrace:

Could not listen on port: 45634.
java.net.BindException: Address already in use: JVM_Bind
        at java.net.PlainSocketImpl.socketBind(Native Method)
        at java.net.PlainSocketImpl.bind(Unknown Source)
        at java.net.ServerSocket.bind(Unknown Source)
        at java.net.ServerSocket.<init>(Unknown Source)
        at java.net.ServerSocket.<init>(Unknown Source)
        at x.x.x.userland.logging.WebServer.start(WebServer.java:38)
        ...

Thanks for your help. Best regards, Timofey.

UPD added the top of the stacktrace. PS. it writes it even when i run it the first time

UPD 2 It turns out that that port is used by something called Coyote...

INFO: Initializing Coyote H开发者_Python百科TTP/1.1 on http-80
28.09.2011 13:04:36 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-45634
28.09.2011 13:04:36 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 267 ms

I will try to "pursuade" Coyote not to use it. Can it be done at all?


Could not listen on port: 45634. java.net.BindException: Address already in use: JVM_Bind

That means the port is in use. Maybe by the standalone program you used to test it. Maybe it was not closed properly and will need to time-out.

Try with a different port number for now, and make sure you close the socket when done.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号