Skip to content
Snippets Groups Projects
Commit dfe98acc authored by brutzman's avatar brutzman
Browse files

restoring mysterious loss of projects following merge/commit

parent 927f59e7
No related branches found
No related tags found
No related merge requests found
Showing
with 945 additions and 0 deletions
assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/Assignment_2_Output.PNG

88.2 KiB

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");
}
}
}
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!");
}
}
}
}
assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayUML.PNG

135 KiB

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");
}
}
}
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!");
}
}
}
}
File added
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");
}
}
}
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!");
}
}
}
}
assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/Output.PNG

20.2 KiB

assignments/src/MV3500Cohort2020JulySeptember/homework2/Mahan/diagram.PNG

48.5 KiB

File added
assignments/src/MV3500Cohort2020JulySeptember/homework2/Weissenberger/HW2.jpg

59.8 KiB

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);
}
}
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);
}
}
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();
}
}
}
}
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();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment