Skip to content
Snippets Groups Projects
Commit e638d8d6 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Application recovers if user enters invalid port number.

parent a243a835
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ import java.io.*; ...@@ -4,6 +4,7 @@ import java.io.*;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.InputMismatchException;
import java.util.Scanner; import java.util.Scanner;
public class Chat { public class Chat {
...@@ -64,8 +65,8 @@ public class Chat { ...@@ -64,8 +65,8 @@ public class Chat {
private Socket connectAsServer() throws IOException { private Socket connectAsServer() throws IOException {
byte[] address = InetAddress.getLocalHost().getAddress(); byte[] address = InetAddress.getLocalHost().getAddress();
System.out.println("Host address: " + address[0] + "." + address[1] + "." + address[2] + "." + address[3]); System.out.println("Host address: " + address[0] + "." + address[1] + "." + address[2] + "." + address[3]);
System.out.print("Select port number: "); String prompt = "Select port number";
int port = getPort(); short port = getPort(prompt);
ServerSocket serverSocket = new ServerSocket(port); ServerSocket serverSocket = new ServerSocket(port);
return serverSocket.accept(); return serverSocket.accept();
} }
...@@ -78,15 +79,28 @@ public class Chat { ...@@ -78,15 +79,28 @@ public class Chat {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
address[i] = Byte.parseByte(tokens[i]); address[i] = Byte.parseByte(tokens[i]);
} }
System.out.print("Enter port host is opening at " + String prompt = "Enter port host is opening at " +
address[0] + "." + address[1] + "." + address[2] + "." + address[3] + ": "); address[0] + "." + address[1] + "." + address[2] + "." + address[3];
int port = getPort(); short port = getPort(prompt);
return new Socket(InetAddress.getByAddress(address), port); return new Socket(InetAddress.getByAddress(address), port);
} }
private int getPort() { private short getPort(String prompt) {
int port = scanner.nextInt(); boolean haveGoodNumber = false;
short port = 0;
while (!haveGoodNumber) {
System.out.print(prompt + ": ");
haveGoodNumber = true;
try {
port = scanner.nextShort();
if (port < 0) throw new InputMismatchException("Expected non-negative value, got " + port);
} catch (InputMismatchException ignored) {
System.out.println("The port number must be a positive integer strictly less than 65536.");
haveGoodNumber = false;
} finally {
scanner.nextLine(); scanner.nextLine();
}
}
return port; return port;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment