Skip to content
Snippets Groups Projects
Commit 67fe5a62 authored by Peter's avatar Peter
Browse files

Severson Assignment 2

parent 1db8a3fe
No related branches found
No related tags found
No related merge requests found
......@@ -31,28 +31,34 @@ public class SeversonAssignment2_Client {
try {
//Bring up dialogue box to confirm that client wants the connection
String message = "Waiting for Client Confirmation";
int confirm = JOptionPane.showConfirmDialog(null, message);
int count = 0;
//If the client confirms, establish and count connections
if (confirm == JOptionPane.YES_OPTION) {
while (true) {
System.out.println("TcpClient creating socket...");
//Set up socket to the server
Socket socket = new Socket(LOCALHOST, 2317);
count++;
//Set up streams to read messages from server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
//Read and print server messages
String serverMessage = br.readLine();
System.out.println("==================================================");
System.out.println("Successful Connection | Number: " + count);
System.out.println("The message the server sent was " + serverMessage);
//Set up streams and send messages to server
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("-- Thank you!");
......@@ -62,6 +68,8 @@ public class SeversonAssignment2_Client {
} else {
//--------------------------------------------------------------
//If Client refuses connection, set up socket, send server a messages
//informing that the client refused, and close the socket.
Socket socket = new Socket(LOCALHOST, 2317);
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
......
......@@ -28,19 +28,24 @@ public class SeversonAssingment2_Server {
try {
//Instantiate server socket
ServerSocket serverSocket = new ServerSocket(2317);
while (true) {
//Server waits for client connection, prints once its accepted
Socket clientConnection = serverSocket.accept();
System.out.println("TcpServer has started...");
//Set up streams to send data to the client
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
//Send message to client
ps.println("-- Welcome to my Server");
//--------------------------------------------------------------
//Set up input streams to receive message from client and print the message to the window
InputStream is = clientConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
......@@ -49,6 +54,7 @@ public class SeversonAssingment2_Server {
System.out.println("The message the server sent was " + serverMessage);
//--------------------------------------------------------------
//Get port and IP address info and print to information
InetAddress localAddress = clientConnection.getLocalAddress();
InetAddress remoteAddress = clientConnection.getInetAddress();
......@@ -58,6 +64,7 @@ public class SeversonAssingment2_Server {
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "
+ remoteAddress.toString() + ", " + remotePort + " ))");
//Close connection
ps.flush();
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