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

made progress

parent a5194767
No related branches found
No related tags found
No related merge requests found
......@@ -6,9 +6,12 @@
package MV3500Cohort2018JulySeptember.homework2.Severson;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import javax.swing.JOptionPane;
......@@ -26,42 +29,77 @@ public class SeversonAssignment2_Client {
public static void main(String[] args) {
try {
while (true) {
System.out.println("TcpClient creating socket...");
int confirmButton = JOptionPane.OK_OPTION;
String message = "Are you sure";
JOptionPane.showConfirmDialog(null, message);
System.out.println("Awaiting Client Confirmation To Create Socket...");
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.
if (confirmButton == JOptionPane.OK_OPTION) {
//Show confirmation dialogue box to ensure client wants to establish connection
String message = "Are You Sure You Want To Create a Socket";
int confirmResult = JOptionPane.showConfirmDialog(null, message);
//If client confirms (selects Yes), the connection is established
if (confirmResult == JOptionPane.YES_OPTION) {
Socket socket = new Socket(LOCALHOST, 2317); // locohost?
//Establish streams to send server a message
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
//Send message to server
String sendMessage = "Client Accepted Connection";
bw.write(sendMessage);
bw.flush();
//print mesage that was sent to server
System.out.println("==================================================");
System.out.println("Message sent to the server : " + sendMessage);
//Set up streams to read mesages from server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
//Read server message and print
String serverMessage = br.readLine();
System.out.println("==================================================");
System.out.println("Now we're talking!");
System.out.println("The message the server sent was " + serverMessage);
//If the client does not confirm (selects anything other than Yes),
//establish connection, send server message that clkient refused,
//and immediately close the socket
} else {
Socket socket = new Socket(LOCALHOST, 2317);
//Set up stream to send message to server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
//Send message to server
String sendMessage = "Client Refused Connection";
bw.write(sendMessage);
bw.flush();
//Close socket, inform client that message was send and return
socket.close();
System.out.println("==================================================");
System.out.println("Message sent to the server : " + sendMessage);
return;
}
}
} catch (IOException e) {
} catch (IOException e) {
System.out.println("Problem with client: ");
System.out.println(e);
}
System.out.println("client exit");
}
System.out.println("client exit");
}
}
......@@ -5,7 +5,10 @@
*/
package MV3500Cohort2018JulySeptember.homework2.Severson;
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;
......@@ -24,18 +27,32 @@ public class SeversonAssingment2_Server {
public static void main(String[] args) {
try {
//Instantiate socket and wait for connection
System.out.println("TcpServer has started..."); // it helps debugging to put this on console first
ServerSocket serverSocket = new ServerSocket(2317);
//Loop until client connection is made
while (true) {
//Socket accepting connection with client
Socket clientConnection = serverSocket.accept(); // block until connected to a client
InputStream is = clientConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String serverMessage = br.readLine();
System.out.println("The message the client sent was " + serverMessage);
//write output text to the client and server
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("This was written by the server"); // this goes back to client!
//Send to client
ps.println("This was written by the server");
//Establish port and IP addresses of the server connection and print
InetAddress localAddress = clientConnection.getLocalAddress();
InetAddress remoteAddress = clientConnection.getInetAddress();
......@@ -44,8 +61,11 @@ public class SeversonAssingment2_Server {
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "+ remoteAddress.toString() + ", " + remotePort + " ))");
//close the connection and inform the client of the close
ps.println("The client connection is closed ---- BYE!");
ps.flush();
clientConnection.close();
}
} catch (IOException e) {
......
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