Skip to content
Snippets Groups Projects
Commit e6aa054e authored by thomascecil's avatar thomascecil
Browse files

HW1 submission.

parent 9698d2c3
No related branches found
No related tags found
No related merge requests found
......@@ -18,10 +18,10 @@ import java.net.*;
* @author mcgredo
* @author brutzman@nps.edu
*/
public class Assignment1TCPExample3ClientChange
public class Assignment1_TCP_Client
{
/** Default constructor */
public Assignment1TCPExample3ClientChange()
public Assignment1_TCP_Client()
{
// default constructor
}
......@@ -43,14 +43,14 @@ public class Assignment1TCPExample3ClientChange
Reader isr;
BufferedReader br;
String serverMessage;
int clientLoopCount = 0;
int numberOfLoops = 10;
int clientLoopCount = 99;
int numberOfLoops = 0;
try {
while (clientLoopCount < numberOfLoops)
while (clientLoopCount > numberOfLoops)
{
clientLoopCount++; // increment at beginning of loop for reliability
System.out.println(Assignment1TCPExample3ClientChange.class.getName() + " creating socket...");
clientLoopCount--; // increment at beginning of loop for reliability
System.out.println(Assignment1_TCP_Client.class.getName() + " creating socket...");
// We request an IP to connect to ("localhost") and
// port number at that IP (2317). This establishes
......@@ -70,9 +70,13 @@ public class Assignment1TCPExample3ClientChange
serverMessage = br.readLine();
System.out.println("======================Assignment1===========================");
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 (clientLoopCount + " bottles of beer on the wall, "
+ clientLoopCount + " bottles of beer.");
if (clientLoopCount > 0) {
System.out.println(serverMessage);
System.out.println((clientLoopCount - 1) + " bottles of beer on the wall." );
}
// socket gets closed, either automatically/silently by this code (or possibly by the server)
Thread.sleep(800l); // slow things down, for example 500l (long) = 500 msec
......@@ -81,7 +85,7 @@ public class Assignment1TCPExample3ClientChange
}
catch (IOException | InterruptedException e)
{
System.err.println("Problem with " + TcpExample3Client.class.getName() + " networking:"); // describe what is happening
System.err.println("Problem with " + Assignment1_TCP_Client.class.getName() + " 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
......@@ -98,7 +102,7 @@ public class Assignment1TCPExample3ClientChange
// program exit: tell somebody about that happening. Likely cause: server drops connection.
System.out.println();
System.out.println(TcpExample3Client.class.getName() + " exit");
System.out.println(Assignment1_TCP_Client.class.getName() + " exit");
}
}
}
......@@ -25,10 +25,10 @@ import java.net.*;
* @author mcgredo
* @author brutzman@nps.edu
*/
public class Assignmnet1TCPExample3ServerChange
public class Assignment1_TCP_Server
{
/** Default constructor */
public Assignmnet1TCPExample3ServerChange()
public Assignment1_TCP_Server()
{
// default constructor
}
......@@ -44,7 +44,7 @@ public class Assignmnet1TCPExample3ServerChange
// 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(Assignmnet1TCPExample3ServerChange.class.getName() + " has started..."); // it helps debugging to put this on console first
System.out.println(Assignment1_TCP_Server.class.getName() + " has started..."); // it helps debugging to put this on console first
ServerSocket serverSocket = new ServerSocket(2317);
OutputStream os;
......@@ -66,8 +66,8 @@ public class Assignmnet1TCPExample3ServerChange
// Now hook everything up (i.e. set up the streams), Java style:
os = clientConnectionSocket.getOutputStream();
ps = new PrintStream(os);
ps.println("This is response " + serverLoopCount + " produced by the server, "
+ Assignmnet1TCPExample3ServerChange.class.getName()); // this gets sent back to client!
ps.println("Take one down, pass it around (for the " + serverLoopCount + " time). ");
// + Assignment1_TCP_Server.class.getName()); // 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).
......@@ -83,7 +83,7 @@ public class Assignmnet1TCPExample3ServerChange
// 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.class.getName() + " socket pair showing host name, address, port:");
System.out.println(Assignment1_TCP_Server.class.getName() + " socket pair showing host name, address, port:");
System.out.println(" (( " +
localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " +
remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))");
......@@ -101,7 +101,7 @@ public class Assignmnet1TCPExample3ServerChange
}
}
} catch (IOException e) {
System.err.println("Problem with " + TcpExample3Server.class.getName() + " networking: " + e);
System.err.println("Problem with " + Assignment1_TCP_Server.class.getName() + " networking: " + e);
// Provide more helpful information to user if exception occurs due to running twice at one time
if (e instanceof java.net.BindException) {
......
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