Skip to content
Snippets Groups Projects
Commit 059c7b7d authored by Fisher, Alexander (Alex) (Capt)'s avatar Fisher, Alexander (Alex) (Capt)
Browse files

Homework 2 - Final

parent ad7be47b
No related branches found
No related tags found
No related merge requests found
package MV3500Cohort2021JulySeptember.homework2.Fisher; package MV3500Cohort2021JulySeptember.homework2.Fisher;
//import static MV3500Cohort2020JulySeptember.homework2.White.WhiteClient.LOCALHOST; //import static MV3500Cohort2020JulySeptember.homework2.White.WhiteClient.LOCALHOST;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.net.Socket; import java.net.Socket;
/** /**
* Before, we always used telnet (netcat) to connect to the server. Here we are * Before, we always used telnet (netcat) to connect to the server. Here we are
* now writing our own program to do the connection. * now writing our own program to do the connection.
* *
* As you will see, when we run this after we start the server we will see the * As you will see, when we run this after we start the server we will see the
* same string telnet printed, sent by the server. The output at the server will * same string telnet printed, sent by the server. The output at the server will
* show different socket pairs for each time the loop iterates. * show different socket pairs for each time the loop iterates.
* *
* @author adfis * @author adfis
*/ */
public class FisherClient { public class FisherClient {
// IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 // IPv6 String constant for localhost address, similarly IPv4 127.0.0.1
public final static String LOCALHOST = "172.20.145.10"; //"0:0:0:0:0:0:0:1"; public final static String LOCALHOST = "172.20.145.10"; //"0:0:0:0:0:0:0:1";
// if we sub localhost with someone's IP this // Sub with someone's IP
//should work the same // Got it to work with McNeely in class
/** /**
* Program invocation, execution starts here * Program invocation, execution starts here
* @param args command-line arguments * @param args command-line arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) {
// Local variables/fields // Local variables/fields
Socket socket; Socket socket;
InputStream is; InputStream is;
InputStreamReader isr; InputStreamReader isr;
BufferedReader br; BufferedReader br;
OutputStream os; OutputStream os;
PrintStream ps; PrintStream ps;
String serverMessage; String serverMessage;
int clientLoopCount = 1; int clientLoopCount = 1;
try { try {
System.out.println("FisherClient creating socket..."); System.out.println("FisherClient creating socket...");
while (clientLoopCount <= 10) { // Made a loop counter for client to stop after 10 pings with server
while (clientLoopCount <= 10) {
// We request an IP to connect to ("localhost") and
// port number at that IP (2317). This establishes // We request an IP to connect to ("localhost") and
// a connection to that IP in the form of a Socket // port number at that IP (2317). This establishes
// object; the server uses a ServerSocket to wait for // a connection to that IP in the form of a Socket
// connections. // object; the server uses a ServerSocket to wait for
socket = new Socket(LOCALHOST, 2317); // connections.
socket = new Socket(LOCALHOST, 2317);
os = socket.getOutputStream();
ps = new PrintStream(os); os = socket.getOutputStream();
ps = new PrintStream(os);
// Now hook everything up (i.e. set up the streams), Java style:
is = socket.getInputStream(); // Now hook everything up (i.e. set up the streams), Java style:
isr = new InputStreamReader(is); is = socket.getInputStream();
br = new BufferedReader(isr); 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 // Read a single line written by the server. We'd
// from the server instead of one only. // do things a bit differently if there were many lines to be read
serverMessage = br.readLine(); // from the server instead of one only.
System.out.println("=================================================="); serverMessage = br.readLine();
System.out.println("==================================================");
System.out.println("The message the server sent was: '" + serverMessage + "'");
System.out.println("FisherClient responds with: To get to the other side"); System.out.println("The message the server sent was: '" + serverMessage + "'");
System.out.println("Loop count: " + clientLoopCount); System.out.println("FisherClient responds with: To get to the other side");
clientLoopCount++; System.out.println("Loop count: " + clientLoopCount);
// socket gets closed, either automatically/silently by this code (or possibly by the server) clientLoopCount++;
// socket gets closed, either automatically/silently by this code (or possibly by the server)
} // end while(true)
} catch (IOException e) { } // end while(true)
System.err.println("Problem with FisherClient networking:"); // describe what is happening } catch (IOException e) {
System.err.println("Error: " + e); System.err.println("Problem with FisherClient 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) { // Provide more helpful information to user if exception occurs due to running twice at one time
System.err.println("*** Be sure to stop any other running instances of programs using this port!"); if (e instanceof java.net.BindException) {
} System.err.println("*** Be sure to stop any other running instances of programs using this port!");
} finally // occurs after any other activity when shutting down }
{ } finally // occurs after any other activity when shutting down
// program exit: tell somebody about that happening. Likely cause: server drops connection. {
System.out.println(); // program exit: tell somebody about that happening. Likely cause: server drops connection.
System.out.println("FisherClient exit"); System.out.println();
} System.out.println("FisherClient exit");
} }
}
}
}
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