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

more progress

parent d168d3cd
No related branches found
No related tags found
No related merge requests found
......@@ -30,66 +30,32 @@ public class SeversonAssignment2_Client {
try {
System.out.println("Awaiting Client Confirmation To Create Socket...");
String message = "Waiting for Client Confirmation";
int confirm = JOptionPane.showConfirmDialog(null, message);
while (true) {
if (confirm == JOptionPane.YES_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) {
while (true) {
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);
System.out.println("TcpClient creating socket...");
//Send message to server
String sendMessage = "Client Accepted Connection";
bw.write(sendMessage);
bw.flush();
Socket socket = new Socket(LOCALHOST, 2317); // locohost?
//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);
} else {
//Send message to server
String sendMessage = "Client Refused Connection";
bw.write(sendMessage);
bw.flush();
System.out.println("Client Refused Connection");
//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) {
......@@ -98,7 +64,7 @@ public class SeversonAssignment2_Client {
System.out.println(e);
}
System.out.println("client exit");
}
......
......@@ -27,52 +27,38 @@ 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
Socket clientConnection = serverSocket.accept();
System.out.println("TcpServer has started...");
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
//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();
int localPort = clientConnection.getLocalPort();
int remotePort = clientConnection.getPort();
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "+ remoteAddress.toString() + ", " + remotePort + " ))");
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();
clientConnection.close();
}
} catch (IOException 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