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;
import java.util.Scanner;
/**
*
* @author DonMcGregor
* @author garrettloeffelman
*/
public class LoeffelmanAssignment2Client {
......@@ -27,8 +27,10 @@ public class LoeffelmanAssignment2Client {
try {
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.
//Tell the client user that the client is listening for a message
System.out.println("Listening:");
while (true) {
// We request an IP to connect to ("localhost") and
......@@ -36,25 +38,31 @@ public class LoeffelmanAssignment2Client {
// 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?
Socket socket = new Socket(LOCALHOST, 2317); // localhost?
// 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);
//This takes the message sent by the server and
//displays it.
String serverMessage = br.readLine();
System.out.println("Server: " + serverMessage);
//Create an output streatm to send messages
OutputStream os = socket.getOutputStream();
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);
System.out.println("Send Message to Server:");
String message = keyboard.nextLine();
ps.println(message);
ps.flush();
// socket gets closed, either automatically/silently this code (or possibly by server)
} // end while(true)
}
......
......@@ -36,10 +36,14 @@ public class LoeffelmanAssignment2Server {
// Stop the program somewhere else.
while (true)
{
//Establish a client connection by accepting
//a socket request.
Socket clientConnection = serverSocket.accept(); // block until connected
OutputStream os = clientConnection.getOutputStream();
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);
System.out.println("Send message to client:");
String message = keyboard.nextLine();
......@@ -63,6 +67,8 @@ public class LoeffelmanAssignment2Server {
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "
+ remoteAddress.toString() + ", " + remotePort + " ))");
//Take an input stream that takes the message
//from the client and displays it.
InputStream is = clientConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
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