From 90d8bcb7bf7912ad99c59ba63ce195b942c98605 Mon Sep 17 00:00:00 2001
From: brutzman <brutzman@DESKTOP-2S09UKA>
Date: Wed, 17 Jul 2019 08:20:22 -0700
Subject: [PATCH] Provide more helpful information to user if exception occurs
 due to running twice at one time

---
 .../homework1/BrutzmanRefactorTcpExample1Telnet.java | 12 +++++++++---
 examples/src/TcpExamples/TcpExample1Telnet.java      |  5 ++++-
 .../TcpExamples/TcpExample2ConnectionCounting.java   |  3 +++
 examples/src/TcpExamples/TcpExample3Client.java      |  3 +++
 examples/src/TcpExamples/TcpExample3Server.java      |  6 +++++-
 5 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorTcpExample1Telnet.java b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorTcpExample1Telnet.java
index 9ba71c1cf5..ab8e1ff23b 100644
--- a/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorTcpExample1Telnet.java
+++ b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorTcpExample1Telnet.java
@@ -65,9 +65,15 @@ public class BrutzmanRefactorTcpExample1Telnet
         catch(IOException e)
         {
             System.out.println("problem with networking: " + e);
-//          if (e.getClass() == java.net.BindException) // TODO whazzup?
-            if (e.getMessage().equals("Address already in use: NET_Bind"))
-                System.out.println("Be sure to stop any other running instances of this program.");
+
+            // Program modification: provide more helpful information to user if
+            // exception occurs when running twice at one time
+            
+            // brute force exception checking, can be brittle if exection message changes
+            // if (e.getMessage().equals("Address already in use: NET_Bind")) 
+
+            if (e instanceof java.net.BindException)
+                System.out.println("Be sure to stop any other running instances of this program!");
         }
     }
 }
diff --git a/examples/src/TcpExamples/TcpExample1Telnet.java b/examples/src/TcpExamples/TcpExample1Telnet.java
index 373c0f58eb..4badbe705b 100644
--- a/examples/src/TcpExamples/TcpExample1Telnet.java
+++ b/examples/src/TcpExamples/TcpExample1Telnet.java
@@ -64,7 +64,10 @@ public class TcpExample1Telnet
         }
         catch(IOException e)
         {
-            System.out.println("problem with networking: " + e);
+            System.out.println("*** Problem with networking: " + e);
+            // Provide more helpful information to user if exception occurs due to running twice at one time
+            if (e instanceof java.net.BindException)
+                System.out.println("*** Be sure to stop any other running instances of this program!");
         }
     }
 }
diff --git a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java
index 14504d55a2..8a3a4a762a 100644
--- a/examples/src/TcpExamples/TcpExample2ConnectionCounting.java
+++ b/examples/src/TcpExamples/TcpExample2ConnectionCounting.java
@@ -85,6 +85,9 @@ public class TcpExample2ConnectionCounting
         catch(IOException e)
         {
             System.out.println("problem with networking: " + e);
+            // Provide more helpful information to user if exception occurs due to running twice at one time
+            if (e instanceof java.net.BindException)
+                System.out.println("*** Be sure to stop any other running instances of this program!");
         }
     }
 }
diff --git a/examples/src/TcpExamples/TcpExample3Client.java b/examples/src/TcpExamples/TcpExample3Client.java
index 87e44dae4c..550b8d5288 100644
--- a/examples/src/TcpExamples/TcpExample3Client.java
+++ b/examples/src/TcpExamples/TcpExample3Client.java
@@ -48,6 +48,9 @@ public class TcpExample3Client {
 		catch (IOException e) {
 			System.out.println("Problem with client: "); // describe what is happening
 			System.out.println(e);
+            // Provide more helpful information to user if exception occurs due to running twice at one time
+            if (e instanceof java.net.BindException)
+                System.out.println("*** Be sure to stop any other running instances of this program!");
 		}
 		// program exit: tell somebody about that
 		System.out.println("client exit");
diff --git a/examples/src/TcpExamples/TcpExample3Server.java b/examples/src/TcpExamples/TcpExample3Server.java
index eeccda6f4b..b948f8e026 100644
--- a/examples/src/TcpExamples/TcpExample3Server.java
+++ b/examples/src/TcpExamples/TcpExample3Server.java
@@ -73,8 +73,12 @@ public class TcpExample3Server {
 				clientConnection.close(); // like it or not, you're outta here!
 			}
 		} 
-		catch (IOException e) {
+		catch (IOException e)
+        {
 			System.out.println("problem with networking");
+            // Provide more helpful information to user if exception occurs due to running twice at one time
+            if (e instanceof java.net.BindException)
+                System.out.println("*** Be sure to stop any other running instances of this program!");
 		}
 	}
 }
-- 
GitLab