Skip to content
Snippets Groups Projects
Commit d15d9c83 authored by Furr, John (MAJ)'s avatar Furr, John (MAJ)
Browse files

Update FurrTcpClient.java with documentation

parent c10629ea
No related branches found
No related tags found
No related merge requests found
......@@ -5,11 +5,11 @@ import java.net.*;
import java.util.Scanner;
/**
* Before, we always used telnet 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 we ran it.
*
* @author mcgredo cd /cygdrive/e/Documents/NPS/MV3500_Internetwork_Communications/NetworkedGraphicsMV3500/deliverables/build/classes/MV3500Cohort2018JulySeptember/homework2/Furr
* This client will connect to a server, and once connected ask for user input to send to a server.
* Once it gets a message with "bye" in it, it terminates the connection.
*
* Folder for my computer to use terminal control is below.
* cd /cygdrive/e/Documents/NPS/MV3500_Internetwork_Communications/NetworkedGraphicsMV3500/deliverables/build/classes/MV3500Cohort2018JulySeptember/homework2/Furr
*/
public class FurrTcpClient {
......@@ -19,48 +19,48 @@ public class FurrTcpClient {
boolean openConnection = true;
try {
while (openConnection) {
System.out.println("The Client is creating socket and it's awesome...");
System.out.println("The Client is creating socket and it's awesome..."); //why wouldn't a connection be awesome?
// 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 the Socket
// object; the server uses a ServerSocket to wait for
// connections.
Socket socket = new Socket(LOCALHOST, 2317); // locohost?
Socket socket = new Socket(LOCALHOST, 2317); // locohost? who be crazy now?
// Read the single line written by the server. We'd
// do things a bit differently if many lines to be read
// from the server, instead of one only.
// the next three lines set up the input from the server to be
// sent to the client console/terminal
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// This is the actual reading of what was sent to the client
String serverMessage = br.readLine();
System.out.println("==================================================");
System.out.println("Now we're talking!");
System.out.println("\nThe message the server sent was " + serverMessage );
System.out.println("\nThe message the server sent was " + serverMessage ); //printing out what was said
int x = 0;
int x = 0; //for testing I have a iterator to close connection, if I get netbeans
//up and running will try with the open connection condition.
while (x < 10)//openConnection)
{
System.out.println("\nType something to the server: ");
Scanner in = new Scanner(System.in);
String s = in.nextLine();
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("Your client says: " + s);
x++;
serverMessage = br.readLine();
System.out.println(serverMessage);
if(serverMessage.contains("bye"))
Scanner in = new Scanner(System.in); // gets input from console/output window
String s = in.nextLine(); //takes input and stores it to string s
OutputStream os = socket.getOutputStream(); //gets the sender ready
PrintStream ps = new PrintStream(os); //gets the printer output ready
ps.println("Your client says: " + s); //sends the input s to the server
x++;
serverMessage = br.readLine(); // if the server sent something, this reads it again
System.out.println(serverMessage); //prints server message
if(serverMessage.contains("bye")) //conditional to close connection
{
openConnection = false;
socket.close();
}
}
// socket gets closed, either automatically/silently this code (or possibly by server)
} // end while(true)
}
} catch (IOException e) {
System.out.println("Problem with client: "); // describe what is happening
System.out.println(e);
......
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