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 95e3f6d45cfa376358c1fe1a2632262c45624d2f..59e9878ed8b9ef6b35f0855743b09f76cfb42faf 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 @@ -182,6 +182,15 @@ public class Chat { if (tokens.length == 4) { for (int i = 0; i < 4; i++) { address[i] = Short.parseShort(tokens[i]); + // mimics range limits of Byte, but for unsigned values + if (address[i] < 0 || address[i] > 255) { + // "Value out of range. Value:"..." Radix: 10" + String message = bundle.getString("exception.numberFormat.startOfValueOutOfRangeMessage") + + " " + MessageFormat.format(bundle + .getString("exception.numberFormat.endOfValueOutOfRangeMessage.0"), + address[i]); + throw new NumberFormatException(message); + } } haveGoodAddress = true; } else { @@ -190,22 +199,18 @@ public class Chat { addressString)); haveGoodAddress = false; } - } catch (NumberFormatException ignored) { + } catch (NumberFormatException nfException) { // "The IP address should be exactly as reported to the host user." System.out.println(bundle.getString("connection.error.badNumberInAddress")); - /* String message = nfException.getMessage(); - // "Value out of range. Value" - if (message.startsWith(bundle.getString("exception.numberFormat.startOfValueOutOfRangeMessage"))) { + // "Value out of range." + if (message.contains(bundle.getString("exception.numberFormat.startOfValueOutOfRangeMessage"))) { String[] messageTokens = message.split("\""); long value = Long.parseLong(messageTokens[1]); // this may break if message format changes - if ((127 < value) && (value < 256)) { - // "Note that Java does not have unsigned integers, so subtract 256 from values greater than..." - System.out.println(MessageFormat.format(bundle.getString( - "connection.error.userAttemptedUnsignedByte.0.1"), value, value - 256)); - } + // "Invalid address field: ... Address fields must be between 0 and 255, inclusive." + System.out.println(MessageFormat.format(bundle.getString( + "connection.error.valueOutOfRange.0"), value)); } - */ haveGoodAddress = false; } } diff --git a/src/main/resources/socketchat_en.properties b/src/main/resources/socketchat_en.properties index 43e70873883da3ce0f6cc5f11b52de419ff6b27e..739fc5f64c54357a0d4a67ff87775ebd6e5224c5 100644 --- a/src/main/resources/socketchat_en.properties +++ b/src/main/resources/socketchat_en.properties @@ -15,9 +15,9 @@ connection.error.malformedAddress = The IP address should be f connection.error.portInUse.0 = Port {0} is already in use. connection.error.portNumberTooHigh = Expected value less than 65536, got {0} connection.error.portNumberTooLow = Expected positive value, got {0} -connection.error.userAttemptedUnsignedByte.0.1 = Note that Java does not have unsigned integers, so subtract 256 from values greater than 127. For example, {0} should be {1}. connection.error.tooManyAttempts = Exceeded maximum number of connection attempts. Terminating. communicate.error.unrecognizedKeyword = Unrecognized keyword +connection.error.valueOutOfRange.0 = Invalid address field: {0}. Address fields must be between 0 and 255, inclusive. connection.info.hostAddress.0.1.2.3 = Host address: {0}.{1}.{2}.{3} connection.info.ready = Connection established. Host goes first. @@ -32,4 +32,5 @@ connection.prompt.selectPortNumber = Select port number connection.response.yes = Y connection.response.no = N -exception.numberFormat.startOfValueOutOfRangeMessage= Value out of range. Value +exception.numberFormat.startOfValueOutOfRangeMessage= Value out of range. +exception.numberFormat.endOfValueOutOfRangeMessage.0= Value:"{0}" Radix: 10