1

Currently we have a Jetty 7 server started this way

//create a new server listening on the 80
Server server = new Server();

SelectChannelConnector connector = new SelectChannelConnector();
connector.setReuseAddress(false);
connector.setPort(80);
server.setConnectors(new Connector[]{connector});

...
server.start();

And when there's no other application catching the 80 port, all is fine. I've also ensured that two Instances of Jetty can't start listening on the same port because of the setReuseAddress.

There is however a case when some other application starts listening on port 80 and Jetty server still manages to start (failing to serve connections there).

C:\Users\bacadmin>netstat -anov | find ":80 "

TCP    0.0.0.0:80             0.0.0.0:0              LISTENING       3976
TCP    0.0.0.0:80             0.0.0.0:0              LISTENING       3808
TCP    [::]:80                [::]:0                 LISTENING       3976

How is that possible and what can be done to insure that Jetty gets an exception during startup if the port isn't open.

abatishchev
  • 92,232
  • 78
  • 284
  • 421
Vic
  • 19,436
  • 10
  • 75
  • 91
  • Assuming it's not bound to another interface, I believe some apps allow the port to be re-used via `SO_REUSEPORT`. See https://github.com/eclipse/jetty.project/issues/6266 and https://stackoverflow.com/a/1694148/3196753. – tresf May 12 '21 at 16:15
  • 1
    Another great write-up is here: https://stackoverflow.com/a/14388707/3196753. I'm still bewildered by the behavior on MacOS. I will post a solution if I find one. – tresf May 12 '21 at 16:57
  • 1
    And perhaps another: https://stackoverflow.com/q/13726975/3196753 – tresf May 12 '21 at 17:02

3 Answers3

5

Jetty and any Java based service can bind a busy port with no exception.

If you use Sun JVM 7 as a host JVM be aware of that there is an issue in it related to IPv6 stack. It is still not fixed.

Good news, a workaround exists. Just use -Djava.net.preferIPv4Stack=true. This works only if you have no plans to support IPv6 in your application.

Thanks.

lsoliveira
  • 4,222
  • 3
  • 18
  • 30
Zorg
  • 388
  • 3
  • 7
1

You can try to specify IP-address and port by adding this line of code

server = new Server(new InetSocketAddress("127.0.0.1", 8080));

in this case jetty will throw an exception:

java.net.BindException: Address already in use: bind
Luzan Baral
  • 3,298
  • 2
  • 31
  • 63
mihmig
  • 11
  • 1
1

Jetty throws exception if the port is already in use. Are you catching all exceptions and suppressing it somewhere?

Regarding reserving a port: This is not really possible. If you keep running your jetty application all the time and use 80, then that kind of reserved it for you...

(added code to help in identifying the root cause)

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.nio.SelectChannelConnector;

public class Main {
    public static void main(String[] args) {

        Server server = new Server();

        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setReuseAddress(false);
        connector.setPort(80);
        server.setConnectors(new Connector[]{connector});

        try {
            server.start();
        } catch (Exception e) {
            e.printStackTrace(); 
        }

    }
}

In my environment, this definitely get java.net.BindException: Address already in use: bind

Jayan
  • 16,628
  • 12
  • 79
  • 131
  • 1
    That's the fun thing, the Jetty doesn't throw an exception. – Vic Mar 08 '12 at 08:31
  • But Jetty does throw an exception. If you're not seeing one, then something strange is going on with your setup. – Tim Mar 08 '12 at 08:33
  • 1
    @Tim, I'm debbuging it, and there is no exception. If I attempt to get two Jettys running on the same port, there is an exception though. – Vic Mar 08 '12 at 08:37
  • 1
    @Jayan - in mine too. But the other application is not a jetty one and if it's run before the jetty server is started - no exception is thrown. – Vic Mar 08 '12 at 08:52
  • @Vic: how much control you have on 'the' other application? You able to kill specific process before starting your jetty process - I will not recommend it though) – Jayan Mar 08 '12 at 09:19
  • 4
    Hmm... this thread of discussion is interesting. I found that with setReuseAddress(false) you always get the exception if it's already bound. With setReuseAddress(true), you get the exception on Linux or Mac OS but on Windows it succeeds and does god knows what. – Trejkaz Sep 10 '12 at 03:46