diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Severson/SeversonAssignment2_Client.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Severson/SeversonAssignment2_Client.java index 213a4ad02d7611297f57890018d5b313ea74aff1..2b9aedd378f77bcca3e7c09f1b098ed8486ef6dd 100644 --- a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Severson/SeversonAssignment2_Client.java +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Severson/SeversonAssignment2_Client.java @@ -30,66 +30,32 @@ public class SeversonAssignment2_Client { try { - System.out.println("Awaiting Client Confirmation To Create Socket..."); + String message = "Waiting for Client Confirmation"; + int confirm = JOptionPane.showConfirmDialog(null, message); - while (true) { + if (confirm == JOptionPane.YES_OPTION) { - //Show confirmation dialogue box to ensure client wants to establish connection - String message = "Are You Sure You Want To Create a Socket"; - int confirmResult = JOptionPane.showConfirmDialog(null, message); - //If client confirms (selects Yes), the connection is established - if (confirmResult == JOptionPane.YES_OPTION) { + while (true) { - Socket socket = new Socket(LOCALHOST, 2317); // locohost? - - //Establish streams to send server a message - OutputStream os = socket.getOutputStream(); - OutputStreamWriter osw = new OutputStreamWriter(os); - BufferedWriter bw = new BufferedWriter(osw); + System.out.println("TcpClient creating socket..."); - //Send message to server - String sendMessage = "Client Accepted Connection"; - bw.write(sendMessage); - bw.flush(); + Socket socket = new Socket(LOCALHOST, 2317); // locohost? - //print mesage that was sent to server - System.out.println("=================================================="); - System.out.println("Message sent to the server : " + sendMessage); - - //Set up streams to read mesages from server InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); - //Read server message and print String serverMessage = br.readLine(); System.out.println("=================================================="); System.out.println("Now we're talking!"); System.out.println("The message the server sent was " + serverMessage); - //If the client does not confirm (selects anything other than Yes), - //establish connection, send server message that clkient refused, - //and immediately close the socket - } else { - - Socket socket = new Socket(LOCALHOST, 2317); + } - //Set up stream to send message to server - OutputStream os = socket.getOutputStream(); - OutputStreamWriter osw = new OutputStreamWriter(os); - BufferedWriter bw = new BufferedWriter(osw); + } else { - //Send message to server - String sendMessage = "Client Refused Connection"; - bw.write(sendMessage); - bw.flush(); + System.out.println("Client Refused Connection"); - //Close socket, inform client that message was send and return - socket.close(); - System.out.println("=================================================="); - System.out.println("Message sent to the server : " + sendMessage); - return; - } } } catch (IOException e) { @@ -98,7 +64,7 @@ public class SeversonAssignment2_Client { System.out.println(e); } - + System.out.println("client exit"); } diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Severson/SeversonAssingment2_Server.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Severson/SeversonAssingment2_Server.java index 35c1bfea13f580f2ba7f6425aaf6d584dbab774e..e2ff60c5996397a5b3a717d788bc5e8bf9b0de9b 100644 --- a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Severson/SeversonAssingment2_Server.java +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Severson/SeversonAssingment2_Server.java @@ -27,52 +27,38 @@ public class SeversonAssingment2_Server { public static void main(String[] args) { try { - - //Instantiate socket and wait for connection - System.out.println("TcpServer has started..."); // it helps debugging to put this on console first + ServerSocket serverSocket = new ServerSocket(2317); - //Loop until client connection is made while (true) { - - //Socket accepting connection with client - Socket clientConnection = serverSocket.accept(); // block until connected to a client - - InputStream is = clientConnection.getInputStream(); - InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); - - String serverMessage = br.readLine(); - System.out.println("The message the client sent was " + serverMessage); - - //write output text to the client and server + + Socket clientConnection = serverSocket.accept(); + System.out.println("TcpServer has started..."); + OutputStream os = clientConnection.getOutputStream(); PrintStream ps = new PrintStream(os); - //Send to client ps.println("This was written by the server"); - //Establish port and IP addresses of the server connection and print InetAddress localAddress = clientConnection.getLocalAddress(); InetAddress remoteAddress = clientConnection.getInetAddress(); int localPort = clientConnection.getLocalPort(); int remotePort = clientConnection.getPort(); - System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "+ remoteAddress.toString() + ", " + remotePort + " ))"); + System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + remoteAddress.toString() + ", " + remotePort + " ))"); - //close the connection and inform the client of the close - ps.println("The client connection is closed ---- BYE!"); ps.flush(); - clientConnection.close(); - + clientConnection.close(); + } + } catch (IOException e) { - + System.out.println("problem with networking"); - + } - } + } } -