Skip to content
Snippets Groups Projects
Commit 4b1297db authored by djfri's avatar djfri
Browse files

Homework 2 networking Frisco

parent 067110d1
No related branches found
No related tags found
No related merge requests found
...@@ -23,7 +23,7 @@ public class FriscoAssignment2Client { ...@@ -23,7 +23,7 @@ public class FriscoAssignment2Client {
boolean openConnection = true; boolean openConnection = true;
try { try {
while (openConnection) { while (openConnection) {
System.out.println("The Client is creating socket and it's wait for it...its going to be.... Legendary!"); System.out.println("The Client is creating a socket and it's going to be.... wait for it.... Legen-dary, Lengendary!");
// We request an IP to connect to ("localhost") and // We request an IP to connect to ("localhost") and
// port number at that IP (2317). This establishes // port number at that IP (2317). This establishes
...@@ -47,11 +47,12 @@ public class FriscoAssignment2Client { ...@@ -47,11 +47,12 @@ public class FriscoAssignment2Client {
System.out.println("Now we're talking!"); 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 );
//System.out.println("\nSTOP! Who would cross the Bridge of Death must answer me these questions three, ere the other side he see. "); System.out.println("\nSTOP! Who would cross the Bridge of Death must answer me these questions three, ere the other side he see. ");
while (openConnection) while (openConnection)
{ {
System.out.println("\nSTOP! Who would cross the Bridge of Death must answer me these questions three, ere the other side he see. "); //System.out.println("\nSTOP! Who would cross the Bridge of Death must answer me these questions three, ere the other side he see. ");
Scanner incomingMessage = new Scanner(System.in); System.out.println("Answer the questions!");
Scanner incomingMessage = new Scanner(System.in);
String s = incomingMessage.nextLine(); String s = incomingMessage.nextLine();
OutputStream os = socket.getOutputStream(); OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os); PrintStream ps = new PrintStream(os);
......
...@@ -19,9 +19,9 @@ import java.net.*; ...@@ -19,9 +19,9 @@ import java.net.*;
*/ */
public class FriscoAssignment2Server { public class FriscoAssignment2Server {
public static void main(String[] args) throws IOException { public static void main(String[] args) {
String[] serverQuestions = new String[7]; //canned responses for multiple inputs from client String[] serverQuestions = new String[7];
serverQuestions[0] = "What... is your name?"; serverQuestions[0] = "What... is your name?";
serverQuestions[1] = "What.... is your quest?"; serverQuestions[1] = "What.... is your quest?";
serverQuestions[2] = "What..... is your favorite color?"; serverQuestions[2] = "What..... is your favorite color?";
...@@ -36,17 +36,16 @@ public class FriscoAssignment2Server { ...@@ -36,17 +36,16 @@ public class FriscoAssignment2Server {
ServerSocket serverSocket = new ServerSocket(2317); ServerSocket serverSocket = new ServerSocket(2317);
System.out.println("Server Socket open and waiting"); System.out.println("Server Socket open and waiting");
boolean connectionActive; boolean Active;
// Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
while(true) while(true)
{ {
Socket clientConnection = serverSocket.accept(); // block until connected Socket clientConnection = serverSocket.accept();
connectionActive = true; //ensure after every new connection the boolean is reset to true. Active = true;
index =0; //reset the index for responses back to 0 each time a new connection happens. index =0;
while (connectionActive) { while (Active) {
OutputStream os = clientConnection.getOutputStream(); OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os); PrintStream ps = new PrintStream(os);
...@@ -55,11 +54,7 @@ public class FriscoAssignment2Server { ...@@ -55,11 +54,7 @@ public class FriscoAssignment2Server {
InputStream serverIS = clientConnection.getInputStream(); InputStream serverIS = clientConnection.getInputStream();
InputStreamReader serverISR = new InputStreamReader(serverIS); InputStreamReader serverISR = new InputStreamReader(serverIS);
BufferedReader br = new BufferedReader(serverISR); 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 localAddress = clientConnection.getLocalAddress();
InetAddress remoteAddress = clientConnection.getInetAddress(); InetAddress remoteAddress = clientConnection.getInetAddress();
...@@ -68,23 +63,15 @@ public class FriscoAssignment2Server { ...@@ -68,23 +63,15 @@ public class FriscoAssignment2Server {
System.out.println("\nSocket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " System.out.println("\nSocket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "
+ remoteAddress.toString() + ", " + remotePort + " ))"); + remoteAddress.toString() + ", " + remotePort + " ))");
while (index < 7) { while (index < 7) {
//while ((line = br.readLine()) != null)//&& index<10)
{ {
System.out.println("\n" + br.readLine() );
ps.println("SERVER: " + serverQuestions[index]);
System.out.println("\n" + br.readLine() ); //line); //prints out what is recieved from client System.out.println("Server has sent: " + serverQuestions[index]);
ps.println("\tThe SERVER has responded with: " + serverQuestions[index]); //sends the prewritten response index++;
System.out.println("Server has sent: " + serverQuestions[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; Active = false;
ps.flush(); ps.flush();
clientConnection.close(); clientConnection.close();
} }
......
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