Skip to content
Snippets Groups Projects
FurrTcpServer.java 4.42 KiB
package MV3500Cohort2018JulySeptember.homework2.Furr;

import java.io.*;
import java.net.*;

/**
 * Very slightly more complex than example1. A complete copy of example 2. The only thing this does differently is introduce a loop into the response, so you don't have to restart the program after one response. Also, it prints out the socket pair the server sees. Run the program via telnet several times and compare the socket pairs.
 *
 * telnet localhost 2317
 *
 * If you're sophisticated you can contact the instructor's computer while running this program.
 *
 * telnet <ipOfServersLaptop> 2317
 *
 * And have him display the socket pairs he got.
 *
 * @author mcgredo
 *
 * to change to build file: cd /cygdrive/e/Documents/NPS/MV3500_Internetwork_Communications/NetworkedGraphicsMV3500/deliverables/build/classes/MV3500Cohort2018JulySeptember/homework2/Furr
 */
public class FurrTcpServer {

	public static void main(String[] args) throws IOException {
		
		String[] responses = new String[10];  //canned responses for multiple inputs from client
		responses[0] = "Hello";
		responses[1] = "Are you there?";
		responses[2] = "Glad you stopped by to say hi";
		responses[3] = "What are you planning on doing today?";
		responses[4] = "Not that this program cares, these are pre-recored";
		responses[5] = "That's right, you can say whatever you want I'll say the samething";
		responses[6] = "Makes this chat less exciting doesn't it.";
		responses[7] = "Your hair looks nice today";
		responses[8] = "The FBI said so.";
		responses[9] = "This is really teadious, I'm done talking.  Good bye!";
		int index = 0;
		
		try {
			// ServerSocket waits for a connection from a client. 
			// Notice that it is outside the loop; ServerSocket
			// needs to be made only once.

			ServerSocket serverSocket = new ServerSocket(2317);
			System.out.println("Server Socket open and waiting");
			boolean connectionActive; //used for a inner while loop conditional below
			
			// Loop, infinitely, waiting for client connections.
			// Stop the program somewhere else.
			while(true)
			{
			Socket clientConnection = serverSocket.accept(); // block until connected
			connectionActive = true; //ensure after every new connection the boolean is reset to true.
			index =0; //reset the index for responses back to 0 each time a new connection happens.
			
			while (connectionActive) {

				OutputStream os = clientConnection.getOutputStream();
				PrintStream ps = new PrintStream(os);
				ps.println("This was written by the server");

				InputStream serverIS = clientConnection.getInputStream();
				InputStreamReader serverISR = new InputStreamReader(serverIS);
				BufferedReader br = new BufferedReader(serverISR);
				String line;

				// Print some information locally about the Socket
				// connection. This includes the port and IP numbers
				// on both sides (the socket pair.)
				InetAddress localAddress = clientConnection.getLocalAddress();
				InetAddress remoteAddress = clientConnection.getInetAddress();

				int localPort = clientConnection.getLocalPort();
				int remotePort = clientConnection.getPort();

				// 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("\nSocket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "
						+ remoteAddress.toString() + ", " + remotePort + " ))");
				while (index < 10) {
					while ((line = br.readLine()) != null)//&& index<10)
					{
						//need check to see if 

						System.out.println("\n" + line);  //prints out what is recieved from client
						ps.println("\tThe SERVER has responded with: " + responses[index]);  //sends the prewritten response
						System.out.println("Server has sent: " + responses[index]); //tells server user what was sent
						index++;  //goes to next response. 

						// Notice the use of flush() and close(). Without
						// the close() to Socket object may stay open for 
						// a while after the client has stopped needing this
						// connection. Close() explicitly ends the connection.
					}
					
				}
				connectionActive = false;
				ps.flush();
				clientConnection.close(); // like it or not, you're outta here!
			}
			}
			
		} catch (IOException e) {
			System.out.println("problem with networking" + e);
		}

	}

}