Skip to content
Snippets Groups Projects
Commit 2eff95fa authored by Sean Islas's avatar Sean Islas
Browse files

Submission for Assignment1

parent 5a1a794b
No related branches found
No related tags found
No related merge requests found
......@@ -45,7 +45,7 @@ public class HW1Client {
while (true)
{
clientLoopCount++; // increment at beginning of loop for reliability
System.out.println(HW1Client.class.getName() + " creating socket...");
// System.out.println(HW1Client.class.getName() + " creating socket...");
// We request an IP to connect to ("localhost") and
// port number at that IP (2317). This establishes
......@@ -63,11 +63,28 @@ public class HW1Client {
// 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.print ("Client loop " + clientLoopCount + ": ");
System.out.println("now we're talking!");
System.out.println("The message the server sent was: '" + serverMessage + "'");
// System.out.println("==================================================");
// First pose
if (clientLoopCount % 2 == 0) {
System.out.println(" O/ "); // head
System.out.println(" /| "); // arms
System.out.println(" /_\\ "); // legs
System.out.println(" | | "); // torso
System.out.println();
}
// Second pose
else {
System.out.println(" \\O "); // head
System.out.println(" |\\ "); // arms
System.out.println(" /_\\ "); // legs
System.out.println(" | | "); // torso
System.out.println();
}
// System.out.print ("Client loop " + clientLoopCount + ": ");
// System.out.println("now we're talking!");
// System.out.println("The message the server sent was: '" + serverMessage + "'");
// socket gets closed, either automatically/silently by this code (or possibly by the server)
Thread.sleep(500l); // slow things down, for example 500l (long) = 500 msec (1/2 second)
......
......@@ -36,14 +36,15 @@ public class HW1Server {
* java -classpath . TcpExamples.TcpExample3Server
* @param args command-line arguments
*/
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(HW1Server.class.getName() + " has started..."); // it helps debugging to put this on console first
// System.out.println(HW1Server.class.getName() + " has started..."); // it helps debugging to put this on console first
System.out.println("May I have this dance?");
ServerSocket serverSocket = new ServerSocket(2317);
OutputStream os;
PrintStream ps;
......@@ -51,11 +52,11 @@ public class HW1Server {
int localPort, remotePort;
int serverLoopCount = 0;
boolean keepGoing = true;
// Server is up and waiting (i.e. "blocked" or paused)
// Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
while (true) {
while (keepGoing) {
// block until connected to a client
try (Socket clientConnectionSocket = serverSocket.accept())
{
......@@ -72,18 +73,40 @@ public class HW1Server {
remoteAddress = clientConnectionSocket.getInetAddress();
localPort = clientConnectionSocket.getLocalPort();
remotePort = clientConnectionSocket.getPort();
System.out.print ("Server loop " + serverLoopCount + ": ");
if (serverLoopCount > 100) {
keepGoing = false;
}
// First pose
if (serverLoopCount % 2 == 0) {
System.out.println(" O/ "); // head
System.out.println(" /| "); // arms
System.out.println(" / \\ "); // legs
System.out.println(" | | "); // torso
System.out.println();
}
// Second pose
else {
System.out.println(" \\O "); // head
System.out.println(" |\\ "); // arms
System.out.println(" / \\ "); // legs
System.out.println(" | | "); // torso
System.out.println();
}
// System.out.print ("Server loop " + serverLoopCount + ": ");
// 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(HW1Server.class.getName() + " socket pair showing host name, address, port:");
System.out.println(" (( " +
localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " +
remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))");
// System.out.println(HW1Server.class.getName() + " socket pair showing host name, address, port:");
// System.out.println(" (( " +
// localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " +
// remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))");
if ( localAddress.getHostName().equals( localAddress.getHostAddress()) ||
remoteAddress.getHostName().equals(remoteAddress.getHostAddress()))
......
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