Skip to content
Snippets Groups Projects
Commit 40783741 authored by courtneythomerson's avatar courtneythomerson
Browse files

Thomerson Homework 2

parent 6dbc8ac5
No related branches found
No related tags found
No related merge requests found
package MV3500Cohort2018JulySeptember.homework2.Thomerson;
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;
/**
* Scenario:
* This will represent a simple logistical request interaction.
* [i] The server will act as the logistical support unit
* [ii] The clients will act as the operating/requesting unit.
*
* Client Actions:
* (a) Instantiate a Socket using the LOCALHOST and 2317
* [i] Display all communication with the Server
* [ii] User answers the questions for the request
*
* @author courtneythomerson
*/
public class ThomersonAssignment2Client {
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 {
boolean requesting = true;
while (true) {
//=============ESTABLISH THE BASIC CONNECTION===================
System.out.println("creating socket");
Socket socket = new Socket(LOCALHOST, 2317); // locohost?
//Establish the InputStream and OutputStream for the client
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
//Write to the OutputStream
PrintStream ps = new PrintStream(os);
//Read the InputStream
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
//Instantiate the user input
Scanner userInput = new Scanner(System.in);
//==============================================================
//======================CONVERSATION============================
//Get confirmation on the connection from the server
String serverMessage = br.readLine();
System.out.println("==================================================");
System.out.println("Confirm Connection:");
System.out.println("From Server:\n\t" + serverMessage);
//Server starts the request process Step #1
//"Would you like to make a reqest? Yes[1] or No[2]"
serverMessage = br.readLine();
System.out.println("From Server:\n\t" + serverMessage);
//Response: Do I want to make a request
String response = userInput.nextLine();
ps.println(response);
if (response.matches("2")) {
serverMessage = br.readLine();
System.out.println(serverMessage);
continue;
}
//Server continues request process Step #2
//"Requesting: Water(gal)[w] Fuel(gal)[f] MRE(case)[m]"
serverMessage = br.readLine();
System.out.println("From Server:\n\t" + serverMessage);
//Response: What do I want
response = userInput.nextLine();
ps.println(response);
//How much of it do I want
serverMessage = br.readLine();
System.out.println("From Server:\n\t" + serverMessage);
//Response
response = userInput.nextLine();
ps.println(response);
//Order confirmation
serverMessage = br.readLine();
System.out.println("From Server:\n\t" + serverMessage);
//Closing connection
serverMessage = br.readLine();
System.out.println("From Server:\n\t" + serverMessage);
}
}
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");
}
}
package MV3500Cohort2018JulySeptember.homework2.Thomerson;
import java.io.BufferedReader;
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;
/**
* Scenario:
* This will represent a simple logistical request interaction.
* [i] The server will act as the logistical support unit
* [ii] The clients will act as the operating/requesting unit.
*
* Server Actions:
* (a) Instantiate the ServerSocket on port 2317
* (b) Display all communication with the server
* (c) Ask the Client if they want to make a request
* (d) Based on the response from the Client either:
* [i] Close the connection and start listening for more clients
* [ii] Ask what type of request they want to make
* (1) Ask how much they want
* (2) Confirm the order
* (e) Close the connection and start listening
*
* @author courtneythomerson
*/
public class ThomersonAssignment2Server {
public static void main(String[] args) {
try
{
//Instantiate the ServerSocket on port 2317
ServerSocket serverSocket = new ServerSocket(2317);
//Display to the user that the ServerSocket has been created
System.out.println("TCP ServerSocket has been instantiated");
//Working with a client
while(true)
{
//Display to the user the server is waiting on clients
System.out.println("\nWaiting on a client..........\n");
//Establish a Socket when the Client connects to the server
// **REMEMBER: accept() is a blocking method
Socket clientConnection = serverSocket.accept(); // block until connected
//Establish the InputStream and OutputStream for the client
OutputStream os = clientConnection.getOutputStream();
InputStream is = clientConnection.getInputStream();
//Tell the Client they have successfully connected
PrintStream ps = new PrintStream(os);
ps.println("You have successfully connected to the server.");
//Display the connection information for the user
InetAddress localAddress = clientConnection.getLocalAddress();
InetAddress remoteAddress = clientConnection.getInetAddress();
int localPort = clientConnection.getLocalPort();
int remotePort = clientConnection.getPort();
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " +
remoteAddress.toString() + ", " + remotePort + " ))");
System.out.println("Would you like to make a reqest? Yes[1] or No[2]");
//Ask the client if they would like to make a request
ps.println("Would you like to make a reqest? Yes[1] or No[2]");
//Read and display an InputStream
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String messageFromClient = br.readLine();
System.out.println("From Client:\n\t" + messageFromClient);
//If the Client wants to make a request continue on
if (messageFromClient.matches("1")) {
//Display a communciation update
System.out.println("\nRequesting: Water(gal)[w] Fuel(gal)[f] MRE(case)[m]\n");
//Ask the client if they would like to make a request
ps.println("Requesting: Water(gal)[w] Fuel(gal)[f] MRE(case)[m]");
//Read and display the Client response
String classOfSupply = br.readLine();
System.out.println("From Client:\n\t" + classOfSupply);
if (classOfSupply.matches("w")){
//Ask how much they want
ps.println("How much water(gal):");
System.out.println("How much water(gal):");
String amountOfWater = br.readLine();
System.out.println("From Client:\n\t" + amountOfWater);
ps.println("Order Confirmation: " + amountOfWater + " gal");
System.out.println("Order Confirmation: " + amountOfWater + " gal");
} else if (classOfSupply.matches("f")) {
//Ask how much they want
ps.println("How much fuel(gal):");
System.out.println("How much fuel(gal):");
String amountOfFuel = br.readLine();
System.out.println("From Client:\n\t" + amountOfFuel);
ps.println("Order Confirmation: " + amountOfFuel + " gal");
System.out.println("Order Confirmation: " + amountOfFuel + " gal");
} else if (classOfSupply.matches("m")) {
//Ask how much they want
ps.println("How many MREs(case):");
System.out.println("How many MREs (case):");
String amountOfMRE = br.readLine();
System.out.println("From Client:\n\t" + amountOfMRE);
ps.println("Order Confirmation: " + amountOfMRE + " case");
System.out.println("Order Confirmation: " + amountOfMRE + " case");
}
}
System.out.println("Closing Connection");
ps.println("Closing Connection");
ps.flush();
clientConnection.close(); // like it or not, you're outta here!
}
}
catch(Exception e)
{
System.out.println("problem with networking");
}
}
}
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