From 37a85152eb1a6a1aabbee226a660b07c700576aa Mon Sep 17 00:00:00 2001 From: Magnus Reftel Date: Sat, 13 Oct 2018 08:39:15 +0200 Subject: [PATCH] Improve error message when binding to in-use port Signed-off-by: Magnus Reftel --- .../eclipse/jetty/server/ServerConnector.java | 7 +++- .../jetty/server/ServerConnectorTest.java | 34 +++++++++++++++---- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java index 3210ed8deb9..f8e9ee1d69c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.server; import java.io.Closeable; import java.io.IOException; +import java.net.BindException; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; @@ -336,7 +337,11 @@ public class ServerConnector extends AbstractNetworkConnector InetSocketAddress bindAddress = getHost() == null ? new InetSocketAddress(getPort()) : new InetSocketAddress(getHost(), getPort()); serverChannel.socket().setReuseAddress(getReuseAddress()); - serverChannel.socket().bind(bindAddress, getAcceptQueueSize()); + try { + serverChannel.socket().bind(bindAddress, getAcceptQueueSize()); + } catch (BindException e) { + throw new IOException("Failed to bind to " + bindAddress, e); + } } return serverChannel; diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ServerConnectorTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ServerConnectorTest.java index 3ae014fb538..19cd1de2ef8 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ServerConnectorTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ServerConnectorTest.java @@ -33,8 +33,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.lang.reflect.Field; +import java.net.BindException; import java.net.HttpURLConnection; import java.net.InetSocketAddress; +import java.net.ServerSocket; import java.net.Socket; import java.net.URI; import java.net.URISyntaxException; @@ -55,6 +57,7 @@ import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.log.StacklessLogging; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class ServerConnectorTest @@ -296,11 +299,30 @@ public class ServerConnectorTest server.stop(); assertThat(connector.getTransport(),Matchers.nullValue()); - - - - - - + } + + @Test + public void testBindToAddressWhichIsInUse() throws Exception { + try (ServerSocket socket = new ServerSocket(0)) { + final int port = socket.getLocalPort(); + + Server server = new Server(); + ServerConnector connector = new ServerConnector(server); + connector.setPort(port); + server.addConnector(connector); + + HandlerList handlers = new HandlerList(); + handlers.addHandler(new DefaultHandler()); + + server.setHandler(handlers); + + try { + server.start(); + Assertions.fail("No exception thrown"); + } catch (IOException e) { + assertThat(e.getCause(), Matchers.instanceOf(BindException.class)); + assertThat(e.getMessage(), Matchers.containsString("0.0.0.0:" + port)); + } + } } }