From 96f699a0364fe86f309b72ece4ed1a91328cc258 Mon Sep 17 00:00:00 2001 From: Christopher Bohn <bohn@unl.edu> Date: Tue, 5 Nov 2019 20:32:34 -0600 Subject: [PATCH] Gracefully handles case when port is already in use. Closes #8 --- .../edu/unl/cse/csce361/socket_chat/Chat.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java b/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java index 5759bd0..aa0f804 100644 --- a/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java +++ b/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java @@ -48,9 +48,18 @@ public class Chat { private Socket connectAsServer(Scanner userInput) throws IOException { byte[] address = InetAddress.getLocalHost().getAddress(); System.out.println("Host address: " + address[0] + "." + address[1] + "." + address[2] + "." + address[3]); - String prompt = "Select port number"; - int port = getPort(prompt, userInput); - ServerSocket serverSocket = new ServerSocket(port); + int port; + ServerSocket serverSocket; + do { + String prompt = "Select port number"; + port = getPort(prompt, userInput); + try { + serverSocket = new ServerSocket(port); + } catch (BindException ignored) { + System.out.println("Port " + port + " is already in use."); + serverSocket = null; + } + } while (serverSocket == null); System.out.println("Waiting for client."); return serverSocket.accept(); } @@ -128,7 +137,7 @@ public class Chat { haveGoodNumber = true; try { port = userInput.nextInt(); - if (port < 0) throw new InputMismatchException("Expected non-negative value, got " + port); + if (port <= 0) throw new InputMismatchException("Expected positive value, got " + port); if (port >= 2 * (Short.MAX_VALUE + 1)) { throw new InputMismatchException("Expected value less than 65536, got " + port); } -- GitLab