Skip to content
Snippets Groups Projects
Commit 11710f5e authored by stefa's avatar stefa
Browse files

Upload Homework 2 - Goericke

parent b0c02102
No related branches found
No related tags found
No related merge requests found
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
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