Skip to content
Snippets Groups Projects
Commit 5e0dc0ef authored by garrettloeffelman's avatar garrettloeffelman
Browse files

LoeffelmanAssignment2Client/Server Messaging Complete with documentation

parent 6a897a47
No related branches found
No related tags found
No related merge requests found
...@@ -15,7 +15,7 @@ import java.net.Socket; ...@@ -15,7 +15,7 @@ import java.net.Socket;
import java.util.Scanner; import java.util.Scanner;
/** /**
* * @author DonMcGregor
* @author garrettloeffelman * @author garrettloeffelman
*/ */
public class LoeffelmanAssignment2Client { public class LoeffelmanAssignment2Client {
...@@ -27,8 +27,10 @@ public class LoeffelmanAssignment2Client { ...@@ -27,8 +27,10 @@ public class LoeffelmanAssignment2Client {
try { try {
System.out.println("creating socket"); System.out.println("creating socket");
//This program establishes allows a client and server //This program allows a client and server
//to send messages back and forth. //to send messages back and forth.
//Tell the client user that the client is listening for a message
System.out.println("Listening:"); System.out.println("Listening:");
while (true) { while (true) {
// We request an IP to connect to ("localhost") and // We request an IP to connect to ("localhost") and
...@@ -36,25 +38,31 @@ public class LoeffelmanAssignment2Client { ...@@ -36,25 +38,31 @@ public class LoeffelmanAssignment2Client {
// a connection to that IP in the form of the Socket // a connection to that IP in the form of the Socket
// object; the server uses a ServerSocket to wait for // object; the server uses a ServerSocket to wait for
// connections. // connections.
Socket socket = new Socket(LOCALHOST, 2317); // locohost? Socket socket = new Socket(LOCALHOST, 2317); // localhost?
// Read the single line written by the server. We'd // Read the single line written by the server. We'd
// do things a bit differently if many lines to be read // do things a bit differently if many lines to be read
// from the server, instead of one only. // from the server, instead of one only.
InputStream is = socket.getInputStream(); InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is); InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(isr);
//This takes the message sent by the server and
//displays it.
String serverMessage = br.readLine(); String serverMessage = br.readLine();
System.out.println("Server: " + serverMessage); System.out.println("Server: " + serverMessage);
//Create an output streatm to send messages
OutputStream os = socket.getOutputStream(); OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os); PrintStream ps = new PrintStream(os);
//Use a scanner to take user input from the key
//board and when prompted send it to the server.
Scanner keyboard = new Scanner(System.in); Scanner keyboard = new Scanner(System.in);
System.out.println("Send Message to Server:"); System.out.println("Send Message to Server:");
String message = keyboard.nextLine(); String message = keyboard.nextLine();
ps.println(message); ps.println(message);
ps.flush();
// socket gets closed, either automatically/silently this code (or possibly by server) // socket gets closed, either automatically/silently this code (or possibly by server)
} // end while(true) } // end while(true)
} }
......
...@@ -36,10 +36,14 @@ public class LoeffelmanAssignment2Server { ...@@ -36,10 +36,14 @@ public class LoeffelmanAssignment2Server {
// Stop the program somewhere else. // Stop the program somewhere else.
while (true) while (true)
{ {
//Establish a client connection by accepting
//a socket request.
Socket clientConnection = serverSocket.accept(); // block until connected Socket clientConnection = serverSocket.accept(); // block until connected
OutputStream os = clientConnection.getOutputStream(); OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os); PrintStream ps = new PrintStream(os);
//Take input from the keyboard and use a print
//stream to send it to the client.
Scanner keyboard = new Scanner(System.in); Scanner keyboard = new Scanner(System.in);
System.out.println("Send message to client:"); System.out.println("Send message to client:");
String message = keyboard.nextLine(); String message = keyboard.nextLine();
...@@ -63,6 +67,8 @@ public class LoeffelmanAssignment2Server { ...@@ -63,6 +67,8 @@ public class LoeffelmanAssignment2Server {
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "
+ remoteAddress.toString() + ", " + remotePort + " ))"); + remoteAddress.toString() + ", " + remotePort + " ))");
//Take an input stream that takes the message
//from the client and displays it.
InputStream is = clientConnection.getInputStream(); InputStream is = clientConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is); InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(isr);
......
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