Skip to content
Snippets Groups Projects
FurrTcpClient.java 2.96 KiB
package MV3500Cohort2018JulySeptember.homework2.Furr;

import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
 * 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 {

	public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; // String constant, i.e. 127.0.0.1

	public static void main(String[] args) {
		boolean openConnection = true;
		try {
			while (openConnection) {
				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? who be crazy now?

				// 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 );  //printing out what was said

				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 (openConnection)  //x<10 will also work. 
				{
					System.out.println("\nType something to the server: ");
					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();
					}

				}
			} 
		} catch (IOException e) {
			System.out.println("Problem with client: "); // describe what is happening
			System.out.println(e);
		}
		// program exit: tell somebody about that
		System.out.println("client exit");
	}

}