Skip to content
Snippets Groups Projects
Commit 18139f1f authored by Brutzman, Don's avatar Brutzman, Don
Browse files

Merge origin/master

parents 0aded205 e9e82c34
No related branches found
No related tags found
No related merge requests found
deliverables/src/MV3500Cohort2018JulySeptember/homework2/Furr/FurrHomework2UML.png

19.5 KiB

package MV3500Cohort2018JulySeptember.homework2.Furr;
import java.io.*;
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.
* 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.
*
* @author mcgredo
* 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)
{
try
{
while(true)
{
System.out.println("creating socket");
// 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?
// 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.
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String serverMessage = br.readLine();
System.out.println("==================================================");
System.out.println("Now we're talking!");
System.out.println("The message the server sent was " + serverMessage);
// 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);
}
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 (x < 10)//openConnection)
{
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");
}
}
}
......@@ -4,80 +4,109 @@ 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.
*
* 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.
*
*
* 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 class FurrTcpServer {
public static void main(String[] args)
{
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);
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.
// Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
while(true)
{
Socket clientConnection = serverSocket.accept(); // block until connected
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
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) {
ps.println("This was written by the server");
// 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?
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;
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " +
remoteAddress.toString() + ", " + remotePort + " ))");
// 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.
ps.flush();
clientConnection.close(); // like it or not, you're outta here!
}
}
catch(IOException e)
{
System.out.println("problem with networking");
}
}
}
\ No newline at end of file
// 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);
ps.println("\tThe SERVER has responded with: " + responses[index]);
System.out.println("Server has sent: " + responses[index]);
index++;
// 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);
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MV3500Cohort2018JulySeptember.homework2.Loeffelman;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author garrettloeffelman
*/
public class LoeffelmanAssignment2Client {
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) {
try {
System.out.println("creating socket");
//This program establishes allows a client and server
//to send messages back and forth.
System.out.println("Listening:");
while (true) {
// 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?
// 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.
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String serverMessage = br.readLine();
System.out.println("Server: " + serverMessage);
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
Scanner keyboard = new Scanner(System.in);
System.out.println("Send Message to Server:");
String message = keyboard.nextLine();
ps.println(message);
// 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);
}
// program exit: tell somebody about that
System.out.println("client exit");
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MV3500Cohort2018JulySeptember.homework2.Loeffelman;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author garrettloeffelman
*/
public class LoeffelmanAssignment2Server {
@SuppressWarnings("ConvertToTryWithResources")
public static void main(String[] args)
{
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);
// Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
while (true)
{
Socket clientConnection = serverSocket.accept(); // block until connected
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
Scanner keyboard = new Scanner(System.in);
System.out.println("Send message to client:");
String message = keyboard.nextLine();
ps.println(message);
// 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("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "
+ remoteAddress.toString() + ", " + remotePort + " ))");
InputStream is = clientConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String serverMessage = br.readLine();
System.out.println("Client: " + serverMessage);
// 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.
ps.flush();
clientConnection.close(); // like it or not, you're outta here!
}
}
catch (IOException e) {
System.out.println("problem with networking");
}
}
}
## Homework 2
Deliverables:
0. Think of a simple challenge/response protocol that you wish to portray.
1. Apply your own customized version of Assignment 3 showing both Server and Client programs.
2. Include comments describing your modifications.
3. Include documentation of one or more sessions, including operation.
4. Create a simple illustration of the communications exchange in a UML Sequence Diagram.
Please see the [README.md](../../README.md) in the Assignments directory for detailed instructions.
\ No newline at end of file
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