diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/Assignment_2_Output.PNG b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/Assignment_2_Output.PNG new file mode 100644 index 0000000000000000000000000000000000000000..a3b41584d12402956b667791cec03bfd8f9b3da6 Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/Assignment_2_Output.PNG differ diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayClient.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayClient.java new file mode 100644 index 0000000000000000000000000000000000000000..54f7d6be6479793e473fcfa4efe2c81b0b7b93b7 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayClient.java @@ -0,0 +1,80 @@ +package MV3500Cohort2020JulySeptember.homework2.Garibay; + +import java.io.*; +import java.net.*; +/** + * + * @author Chris + */ +public class GaribayClient { + + // IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 + public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; + + public static void main(String[] args) { + + // Local vars/fields + Socket socket; + InputStream is; + InputStreamReader isr; + BufferedReader br; + String serverMessage; + Integer count = 0; + OutputStream os; + PrintStream ps; + + + try { + while (true) { + System.out.println("TcpExample3Client creating socket..."); + + // We request an IP to connect to ("localhost") and + // port number at that IP (2317). This establishes + // a connection to that IP in the form of a Socket + // object; the server uses a ServerSocket to wait for + // connections. + socket = new Socket(LOCALHOST, 2317); // locohost? + + // Now hook everything up (i.e. set up the streams), Java style: + is = socket.getInputStream(); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); + + + // Read a single line written by the server. We'd + // do things a bit differently if there were many lines to be read + // from the server instead of one only. + serverMessage = br.readLine(); + System.out.println("=================================================="); + System.out.println("'How many beer bottles are on the wall?"); + System.out.println("The GaribayServer responds by saying: '" + serverMessage + "'"); + System.out.println("GaribayClient says, 'There are "+ count +" beer bottles on the wall. Always good to have more beer bottles."); + count++; + // socket gets closed, either automatically/silently by this code (or possibly by the server) + + + // Now hook everything up (i.e. set up the streams), Java style: + os = socket.getOutputStream(); + ps = new PrintStream(os); + ps.println("GaribayClient message."); // this gets sent back to client! + + ps.flush(); + + + + } // end while(true) + } catch (IOException e) { + System.err.println("Problem with TcpExample3ServerClient networking:"); // describe what is happening + System.err.println("Error: " + e); + + // Provide more helpful information to user if exception occurs due to running twice at one time + if (e instanceof java.net.BindException) { + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } finally { + + // program exit: tell somebody about that + System.out.println("\nclient exit"); + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayServer.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayServer.java new file mode 100644 index 0000000000000000000000000000000000000000..353cdebcd1e38aeb2397d457b5a97ec064a4fc7c --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayServer.java @@ -0,0 +1,93 @@ +package MV3500Cohort2020JulySeptember.homework2.Garibay; + +import java.io.*; +import java.net.*; + +/** + * + * @author Chris + */ +public class GaribayServer { + + public static void main(String[] args) { + try { + + // ServerSocket waits for a connection from a client. + // Notice that it is outside the loop; ServerSocket + // needs to be made only once. + System.out.println("TcpExample3Server has started..."); // it helps debugging to put this on console first + + ServerSocket serverSocket = new ServerSocket(2317); + OutputStream os; + PrintStream ps; + InetAddress localAddress, remoteAddress; + + InputStream is; + InputStreamReader isr; + BufferedReader br; + String clientMessage; + int localPort, remotePort; + + // Server is up and waiting (i.e. "blocked" or paused) + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while (true) { + + // block until connected to a client + try (Socket clientConnection = serverSocket.accept()) { + + // Now hook everything up (i.e. set up the streams), Java style: + os = clientConnection.getOutputStream(); + ps = new PrintStream(os); + ps.println("Count for yourself!"); // this gets sent back to client! + + // Print some information locally about the Socket connection. + // This includes the port and IP numbers on both sides (the socket pair). + localAddress = clientConnection.getLocalAddress(); + remoteAddress = clientConnection.getInetAddress(); + localPort = clientConnection.getLocalPort(); + remotePort = clientConnection.getPort(); + + // My socket pair connection looks like this, to localhost: + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) + + // Why is the first IP/port the same, while the second set has different ports? + System.out.println("TcpExample3Server socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + remoteAddress.toString() + ", " + remotePort + " ))"); + + // Notice the use of flush() and try w/ resources. Without + // the try w/ resources the Socket object may stay open for + // a while after the client has stopped needing this + // connection. try w/ resources explicitly ends the connection. + + // Now hook everything up (i.e. set up the streams), Java style: + is = clientConnection.getInputStream(); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); + + + // Read a single line written by the server. We'd + // do things a bit differently if there were many lines to be read + // from the server instead of one only. + clientMessage = br.readLine(); + System.out.println("=================================================="); + System.out.println("Now we're talking!"); + System.out.println("The message the server sent was: '" + clientMessage + "'"); +// System.out.println("This was the "+ count +" connection."); + + + ps.flush(); + // like it or not, you're outta here! + } + } + } catch (IOException e) { + System.err.println("Problem with TcpExample3Server 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.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayUML.PNG b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayUML.PNG new file mode 100644 index 0000000000000000000000000000000000000000..b706e2dbc8bfceab4903de1f2fda5487afca82c8 Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayUML.PNG differ diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/GoerickeClient.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/GoerickeClient.java new file mode 100644 index 0000000000000000000000000000000000000000..092a90ba4ddbd267e7aa0eccc98a4b70f25c3663 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/GoerickeClient.java @@ -0,0 +1,75 @@ +package MV3500Cohort2020JulySeptember.homework2.Goericke; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.Socket; + +/** + * + * @author stefa + */ +public class GoerickeClient { + + // IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 + public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; + + public static void main(String[] args) { + + // Local vars/fields + Socket socket; + InputStream is; + InputStreamReader isr; + BufferedReader br; + String serverMessage; + + try { + while (true) { + System.out.println("\nGoerickeClient creating socket..."); + + // We request an IP to connect to ("localhost") and + // port number at that IP (2317). This establishes + // a connection to that IP in the form of a Socket + // object; the server uses a ServerSocket to wait for + // connections. + socket = new Socket(LOCALHOST, 2317); // locohost? + + // thats my input stream + is = socket.getInputStream(); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); + + // Read a single line written by the server. We'd + // do things a bit differently if there were many lines to be read + // from the server instead of one only. + serverMessage = br.readLine(); + System.out.println("The message the server sent was: '" + serverMessage + "'"); + // socket gets closed, either automatically/silently by this code (or possibly by the server) + if (serverMessage.contentEquals("Welcome, how are you !!!")) { + System.out.println("What a nice and friendly server"); + } else if (serverMessage.contentEquals("Still alive there ?")){ + System.out.println("Yes, I am ... still .... where else should I be"); + } else{ + System.out.println("Seems he does not have anything to tell"); + } + + } // end while(true) + } catch (IOException e) { + System.err.println("Problem with TcpExample3ServerClient networking:"); // describe what is happening + System.err.println("Error: " + e); + + // Provide more helpful information to user if exception occurs due to running twice at one time + if (e instanceof java.net.BindException) { + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } finally { + + // program exit: tell somebody about that + System.out.println("\nclient exit"); + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/GoerickeServer.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/GoerickeServer.java new file mode 100644 index 0000000000000000000000000000000000000000..b85a12b0e2d5d8a17eeea248b8b23c5c37e9c533 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/GoerickeServer.java @@ -0,0 +1,102 @@ +package MV3500Cohort2020JulySeptember.homework2.Goericke; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * This runable sets up a TCP/IP server for use with a client coded + * within MV3500 - Homework 2. + * @author stefan goericke */ +public class GoerickeServer { + + + private static String getLogo(){ + String masterString = " ######### ###### # #\r\n"; + masterString = masterString + " # # # #\r\n"; + masterString = masterString + " # ###### # #\r\n"; + masterString = masterString + " # # # #\r\n"; + masterString = masterString + " # ###### #\r\n\n\n"; + return masterString; + } + + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + System.out.print(getLogo()); + try { + + // ServerSocket waits for a connection from a client. + // Notice that it is outside the loop; ServerSocket + // needs to be made only once. + System.out.println("GoerickeServer has started... "); // it helps debugging to put this on console first + ServerSocket serverSocket = new ServerSocket(2317); + OutputStream os; + PrintStream ps; + InetAddress localAddress, remoteAddress; + int localPort, remotePort; + + // Server is up and waiting (i.e. "blocked" or paused) + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + int counter = 0; + while (true) { + + // block until connected to a client + try (Socket clientConnectionSocket = serverSocket.accept()) { + counter++; + // my output stream + os = clientConnectionSocket.getOutputStream(); + ps = new PrintStream(os); + + + if (counter==1){ + ps.println("Welcome, how are you !!!"); // this gets sent back to client! + } + else if (counter%10 == 0){ + ps.println("Still alive there ?"); + } else { + ps.println("----------"); + } + + // Print some information locally about the Socket connection. + // This includes the port and IP numbers on both sides (the socket pair). + localAddress = clientConnectionSocket.getLocalAddress(); + remoteAddress = clientConnectionSocket.getInetAddress(); + localPort = clientConnectionSocket.getLocalPort(); + remotePort = clientConnectionSocket.getPort(); + + if (( localAddress.getHostName().equals( localAddress.getHostAddress()) || + remoteAddress.getHostName().equals(remoteAddress.getHostAddress())) && + (counter%50==0)){ + System.out.println("Talking to myself ..... again"); + } else if (counter%50==0) { + System.out.println("Connected to ...."); + System.out.print("(" + remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))"); + System.out.println(); + System.out.println("I am ...."); + System.out.print("(" + localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( "); + } + + // connection. try w/ resources explicitly ends the connection. + ps.flush(); + } + } + } catch (IOException e) { + System.err.println("Problem with TcpExample3Server 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.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/Goericke_HW2.pdf b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/Goericke_HW2.pdf new file mode 100644 index 0000000000000000000000000000000000000000..e8226f4ef9a75b3099df8cadd40129a43186485d Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/Goericke_HW2.pdf differ diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/Mahan_Client.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/Mahan_Client.java new file mode 100644 index 0000000000000000000000000000000000000000..a3bc4bc62668dd67bd48c7cd79a86b97979e4a0a --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/Mahan_Client.java @@ -0,0 +1,81 @@ +package MV3500Cohort2020JulySeptember.homework2.Mahan; + +import java.io.*; +import java.net.*; +/** + * + * @author Bill + */ +public class Mahan_Client { + + // IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 + public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; + + public static void main(String[] args) { + + // Local vars/fields + Socket socket; + InputStream is; + InputStreamReader isr; + BufferedReader br; + String serverMessage; + Integer count = 0; + + OutputStream os; + PrintStream ps; + + + try { + while (true) { + System.out.println("TcpExample3Client creating socket..."); + + // We request an IP to connect to ("localhost") and + // port number at that IP (2317). This establishes + // a connection to that IP in the form of a Socket + // object; the server uses a ServerSocket to wait for + // connections. + socket = new Socket(LOCALHOST, 2317); // locohost? + + // Now hook everything up (i.e. set up the streams), Java style: + is = socket.getInputStream(); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); + + + // Read a single line written by the server. We'd + // do things a bit differently if there were many lines to be read + // from the server instead of one only. + serverMessage = br.readLine(); + System.out.println("=================================================="); + System.out.println("'Knock, knock,' says the MahanClient."); + System.out.println("The MahanServer responds by saying: '" + serverMessage + "'"); + System.out.println("A frustrated MahanClient responds, 'Answer your door! I have knocked "+ count +" times."); + count++; + // socket gets closed, either automatically/silently by this code (or possibly by the server) + + + // Now hook everything up (i.e. set up the streams), Java style: + os = socket.getOutputStream(); + ps = new PrintStream(os); + ps.println("This message was produced by the MahanClient."); // this gets sent back to client! + + ps.flush(); + + + + } // end while(true) + } catch (IOException e) { + System.err.println("Problem with TcpExample3ServerClient networking:"); // describe what is happening + System.err.println("Error: " + e); + + // Provide more helpful information to user if exception occurs due to running twice at one time + if (e instanceof java.net.BindException) { + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } finally { + + // program exit: tell somebody about that + System.out.println("\nclient exit"); + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/Mahan_Server.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/Mahan_Server.java new file mode 100644 index 0000000000000000000000000000000000000000..7e8ef6460bda2cb538461e6dc6d4cf61b5d6482c --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/Mahan_Server.java @@ -0,0 +1,94 @@ +package MV3500Cohort2020JulySeptember.homework2.Mahan; + +import java.io.*; +import java.net.*; + +/** + * + * @author Bill + */ +public class Mahan_Server { + + public static void main(String[] args) { + try { + + // ServerSocket waits for a connection from a client. + // Notice that it is outside the loop; ServerSocket + // needs to be made only once. + System.out.println("TcpExample3Server has started..."); // it helps debugging to put this on console first + + ServerSocket serverSocket = new ServerSocket(2317); + OutputStream os; + PrintStream ps; + InetAddress localAddress, remoteAddress; + + InputStream is; + InputStreamReader isr; + BufferedReader br; + String clientMessage; + int localPort, remotePort; + + // Server is up and waiting (i.e. "blocked" or paused) + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while (true) { + + // block until connected to a client + try (Socket clientConnection = serverSocket.accept()) { + + // Now hook everything up (i.e. set up the streams), Java style: + os = clientConnection.getOutputStream(); + ps = new PrintStream(os); + ps.println("Go away!"); // this gets sent back to client! + + // Print some information locally about the Socket connection. + // This includes the port and IP numbers on both sides (the socket pair). + localAddress = clientConnection.getLocalAddress(); + remoteAddress = clientConnection.getInetAddress(); + localPort = clientConnection.getLocalPort(); + remotePort = clientConnection.getPort(); + + // My socket pair connection looks like this, to localhost: + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) + + // Why is the first IP/port the same, while the second set has different ports? + System.out.println("TcpExample3Server socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + remoteAddress.toString() + ", " + remotePort + " ))"); + + // Notice the use of flush() and try w/ resources. Without + // the try w/ resources the Socket object may stay open for + // a while after the client has stopped needing this + // connection. try w/ resources explicitly ends the connection. + + // Now hook everything up (i.e. set up the streams), Java style: + is = clientConnection.getInputStream(); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); + + + // Read a single line written by the server. We'd + // do things a bit differently if there were many lines to be read + // from the server instead of one only. + clientMessage = br.readLine(); + System.out.println("=================================================="); + System.out.println("Now we're talking!"); + System.out.println("The message the server sent was: '" + clientMessage + "'"); +// System.out.println("This was the "+ count +" connection."); + + + + ps.flush(); + // like it or not, you're outta here! + } + } + } catch (IOException e) { + System.err.println("Problem with TcpExample3Server 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.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/Output.PNG b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/Output.PNG new file mode 100644 index 0000000000000000000000000000000000000000..f4186dec73b83c4b7ee9acc2922bab7e7c281935 Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/Output.PNG differ diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/diagram.PNG b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/diagram.PNG new file mode 100644 index 0000000000000000000000000000000000000000..2bb117e1d68832892abb5319b68aca0005ed9725 Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/diagram.PNG differ diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/Doku.pdf b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/Doku.pdf new file mode 100644 index 0000000000000000000000000000000000000000..191a0c453cae997e6a39f7cc48997ada7d31ede1 Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/Doku.pdf differ diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/HW2.jpg b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/HW2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2babdaed93845fd29dbe1e82b7c6f27d9cfe4ee3 Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/HW2.jpg differ diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiChatClient.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiChatClient.java new file mode 100644 index 0000000000000000000000000000000000000000..4c133b53fb47cbaf0686254824b86f5534f2192e --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiChatClient.java @@ -0,0 +1,107 @@ +package MV3500Cohort2020JulySeptember.homework2.Weissenberger; + +import java.io.*; +import java.net.*; + +/** + * This client program establishes a socket connection to the chat server, hand + * over the connection to a thread. + * + * @author Bernd "Loki" Weissenberger + */ +public class LokiChatClient implements Runnable { + + private Socket socket = null; + private Thread thread = null; + private DataInputStream console = null; + private DataOutputStream streamOut = null; + private LokiClientThread client = null; + + /** + * constructor. + * @param serverName as named (localhost per default) + * @param serverPort as named (should be 2317) + */ + public LokiChatClient(String serverName, int serverPort) { + System.out.println("***********************************************"); + System.out.println("* Establishing connection. Please wait ... :) *"); + System.out.println("***********************************************"); + + try { + socket = new Socket(serverName, serverPort); + System.out.println("I'm connected: " + socket); + start(); + } catch (UnknownHostException uhe) { + System.out.println("Host unknown: " + uhe.getMessage()); + } catch (IOException ioe) { + System.out.println("Unexpected exception (server isn't up??): " + ioe.getMessage()); + } + } + + public void run() { + while (thread != null) { + try { + streamOut.writeUTF(console.readLine()); + streamOut.flush(); + } catch (IOException ioe) { + System.out.println("Sending error: " + ioe.getMessage()); + stop(); + } + } + } + + public void handle(String msg) { + if (msg.equals(".bye")) { + System.out.println("Good bye. Press RETURN to exit ..."); + stop(); + } else { + System.out.println(msg); + } + } + + /** + * setup the streams and (if possible) the Thread + * @throws IOException + */ + public void start() throws IOException { + console = new DataInputStream(System.in); + streamOut = new DataOutputStream(socket.getOutputStream()); + if (thread == null) { + client = new LokiClientThread(this, socket); + thread = new Thread(this); + thread.start(); + } + } + + /** + * stops the Thread + */ + public void stop() { + if (thread != null) { //thread.stop(); + thread = null; + } + try { + if (console != null) { + console.close(); + } + if (streamOut != null) { + streamOut.close(); + } + if (socket != null) { + socket.close(); + } + } catch (IOException ioe) { + System.out.println("Error closing ..."); + } + client.close(); + } + + /** + * main method with fix IP and Port + * @param args + */ + public static void main(String args[]) { + LokiChatClient client = null; + client = new LokiChatClient("127.0.0.1", 2317); + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiChatServer.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiChatServer.java new file mode 100644 index 0000000000000000000000000000000000000000..393d40bd9285d5cea5d6873d43e3d339bbf50ec5 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiChatServer.java @@ -0,0 +1,149 @@ +package MV3500Cohort2020JulySeptember.homework2.Weissenberger; + +import java.io.*; +import java.net.*; + +/** + * A server example that creates a new thread to handle multiple + * connections one after another, running in parallel. + * + * @author Bernd Weissenberger + */ +public class LokiChatServer implements Runnable { + + private LokiServerThread clients[] = new LokiServerThread[50]; + private ServerSocket server = null; + private Thread thread = null; + private int clientCount = 0; + + /** + * constructor + * @param port + */ + public LokiChatServer(int port) { + try { + System.out.println("Binding to port " + port + ", please wait ..."); + server = new ServerSocket(port); + System.out.println("Server started: " + server); + start(); + } catch (IOException ioe) { + System.out.println("Can not bind to port " + port + ": " + ioe.getMessage()); + } + } + + /** + * start a new thread + */ + @Override + public void run() { + while (thread != null) { + try { + System.out.println("Waiting for a client ..."); + addThread(server.accept()); + } catch (IOException ioe) { + System.out.println("Server accept error: " + ioe); + stop(); + } + } + } + public void start() { + if (thread == null) { + thread = new Thread(this); + thread.start(); + } + } + + /** + * kill the thread + */ + public void stop() { + if (thread != null) { + //thread.stop(); + thread = null; + } + } + + /** + * get the client by ID + * @param ID + * @return + */ + private int findClient(int ID) { + for (int i = 0; i < clientCount; i++) { + if (clients[i].getID() == ID) { + return i; + } + } + return -1; + } + + /** + * sending message to all clients + * @param ID + * @param input + */ + public synchronized void handle(int ID, String input) { + if (input.equals(".bye")) { + clients[findClient(ID)].send(".bye"); + remove(ID); + } else { + for (int i = 0; i < clientCount; i++) { + clients[i].send(ID + " says: " + input); + } + } + } + + /** + * remove a quit client from list + * @param ID + */ + public synchronized void remove(int ID) { + int pos = findClient(ID); + if (pos >= 0) { + LokiServerThread toTerminate = clients[pos]; + System.out.println("Removing client thread " + ID + " at " + pos); + if (pos < clientCount - 1) { + for (int i = pos + 1; i < clientCount; i++) { + clients[i - 1] = clients[i]; + } + } + clientCount--; + try { + toTerminate.close(); + } catch (IOException ioe) { + System.out.println("Error closing thread: " + ioe); + } + //toTerminate.stop(); + } + } + + /** + * add a thread to list + * @param socket + */ + private void addThread(Socket socket) { + if (clientCount < clients.length) { + System.out.println("Client accepted: " + socket); + clients[clientCount] = new LokiServerThread(this, socket); + try { + clients[clientCount].open(); + clients[clientCount].start(); + clientCount++; + } catch (IOException ioe) { + System.out.println("Error opening thread: " + ioe); + } + } else { + System.out.println("Client refused: maximum " + clients.length + " reached."); + } + } + + /** + * the main for this class. Just starts a new server listening at port 2317 + * @param args + */ + public static void main(String args[]) { + LokiChatServer server = null; + server = new LokiChatServer(2317); + } + +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiClientThread.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiClientThread.java new file mode 100644 index 0000000000000000000000000000000000000000..7c17ba9e045f2f5f06eb07092fbd24f4584ac414 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiClientThread.java @@ -0,0 +1,67 @@ +package MV3500Cohort2020JulySeptember.homework2.Weissenberger; + +import java.io.*; +import java.net.*; + +/** + * A program that handles all logic associated with one socket connection by + * running in a thread of its own. This is the client portion. + * + * @author Bernd Weisenberger + */ +public class LokiClientThread extends Thread { + + private Socket socket = null; + private LokiChatClient client = null; + private DataInputStream streamIn = null; + + /** + * constructor + * @param client + * @param socket + */ + public LokiClientThread(LokiChatClient client, Socket socket) { + this.client = client; + this.socket = socket; + this.open(); + this.start(); + } + + /** + * initialize the stream + */ + public void open() { + try { + streamIn = new DataInputStream(socket.getInputStream()); + } catch (IOException ioe) { + System.out.println("Error getting input stream: " + ioe); + } + } + + /** + * closes the stream + */ + public void close() { + try { + if (streamIn != null) { + streamIn.close(); + } + } catch (IOException ioe) { + System.out.println("Error closing input stream: " + ioe); + } + } + + /** + * uses the handle() from Client to react (send or quit) + */ + public void run() { + while (true) { + try { + client.handle(streamIn.readUTF()); + } catch (IOException ioe) { + System.out.println("Listening error: " + ioe.getMessage()); + client.stop(); + } + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiServerThread.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiServerThread.java new file mode 100644 index 0000000000000000000000000000000000000000..a8dfc011f097413cb779d4dd88927c7801f55da0 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/LokiServerThread.java @@ -0,0 +1,97 @@ +package MV3500Cohort2020JulySeptember.homework2.Weissenberger; + +import java.io.*; +import java.net.*; + +/** + * A program that handles all logic associated with one socket connection + * by running in a thread of its own. This is the server + * portion + * + * @author Bernd Weissenberger + */ +public class LokiServerThread extends Thread { + + // as named + private LokiChatServer server = null; + private Socket socket = null; + private int ID = -1; + private DataInputStream streamIn = null; + private DataOutputStream streamOut = null; + + /** + * constructor + * @param server instance of the server + * @param socket ...and the socket + */ + public LokiServerThread(LokiChatServer server, Socket socket) { + super(); + this.server = server; + this.socket = socket; + this.ID = socket.getPort(); + } + + /** + * send the messages + * @param msg + */ + public void send(String msg) { + try { + streamOut.writeUTF(msg); + streamOut.flush(); + } catch (IOException ioe) { + System.out.println(ID + " ERROR sending: " + ioe.getMessage()); + server.remove(ID); + } + } + + /** + * simple getter + * @return + */ + public int getID() { + return ID; + } + + /** + * read he input using the handler method + */ + @Override + public void run() { + System.out.println("Server Thread " + ID + " running."); + while (true) { + try { + server.handle(ID, streamIn.readUTF()); + } catch (IOException ioe) { + System.out.println(ID + " ERROR reading: " + ioe.getMessage()); + server.remove(ID); + //stop(); + } + } + } + + /** + * open all needed streams + * @throws IOException + */ + public void open() throws IOException { + streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream())); + streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); + } + + /** + * close socket and streams + * @throws IOException + */ + public void close() throws IOException { + if (socket != null) { + socket.close(); + } + if (streamIn != null) { + streamIn.close(); + } + if (streamOut != null) { + streamOut.close(); + } + } +}