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;
//import static MV3500Cohort2020JulySeptember.homework2.White.WhiteClient.LOCALHOST;
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.Socket;
/**
* Before, we always used telnet (netcat) to connect to the server. Here we are
* 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
* same string telnet printed, sent by the server. The output at the server will
* show different socket pairs for each time the loop iterates.
*
* @author adfis
*/
public class FisherClient {
// 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";
// if we sub localhost with someone's IP this
//should work the same
/**
* Program invocation, execution starts here
* @param args command-line arguments
*/
public static void main(String[] args) {
// Local variables/fields
Socket socket;
InputStream is;
InputStreamReader isr;
BufferedReader br;
OutputStream os;
PrintStream ps;
String serverMessage;
int clientLoopCount = 1;
try {
System.out.println("FisherClient creating socket...");
while (clientLoopCount <= 10) {
// 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);
os = socket.getOutputStream();
ps = new PrintStream(os);
// 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("The message the server sent was: '" + serverMessage + "'");
System.out.println("FisherClient responds with: To get to the other side");
System.out.println("Loop count: " + clientLoopCount);
clientLoopCount++;
// socket gets closed, either automatically/silently by this code (or possibly by the server)
} // end while(true)
} catch (IOException 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) {
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
{
// program exit: tell somebody about that happening. Likely cause: server drops connection.
System.out.println();
System.out.println("FisherClient exit");
}
}
}
package MV3500Cohort2021JulySeptember.homework2.Fisher;
//import static MV3500Cohort2020JulySeptember.homework2.White.WhiteClient.LOCALHOST;
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.Socket;
/**
* Before, we always used telnet (netcat) to connect to the server. Here we are
* 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
* same string telnet printed, sent by the server. The output at the server will
* show different socket pairs for each time the loop iterates.
*
* @author adfis
*/
public class FisherClient {
// 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";
// Sub with someone's IP
// Got it to work with McNeely in class
/**
* Program invocation, execution starts here
* @param args command-line arguments
*/
public static void main(String[] args) {
// Local variables/fields
Socket socket;
InputStream is;
InputStreamReader isr;
BufferedReader br;
OutputStream os;
PrintStream ps;
String serverMessage;
int clientLoopCount = 1;
try {
System.out.println("FisherClient creating socket...");
// 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
// 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);
os = socket.getOutputStream();
ps = new PrintStream(os);
// 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("The message the server sent was: '" + serverMessage + "'");
System.out.println("FisherClient responds with: To get to the other side");
System.out.println("Loop count: " + clientLoopCount);
clientLoopCount++;
// socket gets closed, either automatically/silently by this code (or possibly by the server)
} // end while(true)
} catch (IOException 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) {
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
{
// program exit: tell somebody about that happening. Likely cause: server drops connection.
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