Skip to content
Snippets Groups Projects
Commit b8be5f20 authored by John Furr's avatar John Furr
Browse files

Assignment 2 update.... again.

parent 7e2c0779
No related branches found
No related tags found
No related merge requests found
......@@ -35,3 +35,4 @@
/projects/TcpExample2/build
/projects/TcpExample3/Client/TcpClient/nbproject/private
/projects/TcpExample3/Server/TcpServer/nbproject/private
/deliverables/build/
\ No newline at end of file
package MV3500Cohort2018JulySeptember.homework1;
package MV3500Cohort2018JulySeptember.homework1;
import java.io.*;
import java.net.*;
......@@ -48,8 +48,8 @@ public class FurrAssignment1
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("This client response was written by server FurrAssignment1"); // to remote client
System.out.println("This server response was written by server FurrAssignment1"); // to server console
ps.println("This client response was written by server FurrAssignment1"); // to remote client changed the name
System.out.println("This server response was written by server FurrAssignment1"); // to server console changed the name
ps.println("Thanks for showing up it's been fun, but you have to go now. Goodbye!"); //added a goodbye message, it really has been fun!
// "flush()" in important in that it forces a write
......
package MV3500Cohort2018JulySeptember.homework1;
import java.io.*;
......@@ -11,12 +10,21 @@ import java.net.*;
* out the socket pair the server sees. Run the program via telnet
* several times and compare the socket pairs.
*
* telnet localhost 2317
* telnet localhost 2317-2326
*
* If you're sophisticated you can contact the instructor's computer
* while running this program.
*
* telnet <ipOfServersLaptop> 2317
* telnet <ipOfServersLaptop> 2318
* telnet <ipOfServersLaptop> 2319
* telnet <ipOfServersLaptop> 2320
* telnet <ipOfServersLaptop> 2321
* telnet <ipOfServersLaptop> 2322
* telnet <ipOfServersLaptop> 2323
* telnet <ipOfServersLaptop> 2324
* telnet <ipOfServersLaptop> 2325
* telnet <ipOfServersLaptop> 2326
*
* And have him display the socket pairs he got.
* @author mcgredo
......@@ -24,63 +32,97 @@ import java.net.*;
public class FurrAssignment2
{
public static void main(String[] args)
public static void main(String[] args) throws IOException
{
try
{
// ServerSocket waits for a connection from a client.
// Notice that it is outside the loop; ServerSocket
// needs to be made only once.
ServerSocket[] serverSocket = new ServerSocket[10]; // created an array for 10 ports to be open
int connectionCount = 0; // state
for(int i=2317; i<10; i++){
ServerSocket serverSocket = new ServerSocket(i);
System.out.println("server established for port #" +i);
int j = 0; //number of ports to open
for(int i=2317; j<10; i++){
serverSocket[j] = new ServerSocket(i); //open the ports
System.out.println("server established for port # " +i +" in Array slot " +j);
j++;
}
j=0;
// Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
Socket clientConnections[] = new Socket[10]; //not used, but wanted to create connections.
//clientConnections[j] = serverSocket[j].accept();
//Socket clientConnection;
boolean wait = true; //not used, tried to find a way to see what port had a pending connection
while(true)
{
Socket clientConnection = serverSocket.accept(); // blocks! then proceeds once a connection is "accept"ed
connectionCount++; // got another one!
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("This client response was written by server TcpExample2"); // to remote client
System.out.println("This server response was written by server TcpExample2"); // to server console
// Print some information locally about the Socket
// connection. This includes the port and IP numbers
// on both sides (the socket pair.)
InetAddress localAddress = clientConnection.getLocalAddress();
InetAddress remoteAddress = clientConnection.getInetAddress();
int localPort = clientConnection.getLocalPort();
int remotePort = clientConnection.getPort();
// My socket pair connection looks like this, to localhost:
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 ))
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 ))
//
// Why is the first IP/port the same, while the second set has
// different ports?
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " +
remoteAddress.toString() + ", " + remotePort + " ))");
System.out.println("got another connection, #" + connectionCount); // report progress
//Loop to look for the port that is being connected to, can't find the variable or bool to see what is pending connection
// while(wait)
// {
// for(int i =0; i<10; i++){
// int z = 2317;
// if(serverSocket[i].isClosed()){
// //try(
// Socket clientConnection = serverSocket[i].accept();
// //)
// z++;
// wait=false;
// return;
// }
// }
// }
// for(int i=0; i<10; i++){
// if(serverSocket[i].isClosed())
// j=i;
// }
try (
Socket clientConnection = serverSocket[j].accept(); // blocks! then proceeds once a connection is "accept"ed //will only follow on for port 2317 since that is in slot 0 in the array
)
// Notice the use of flush() and close(). Without
// the close() to Socket object may stay open for
// a while after the client has stopped needing this
// connection. Close() explicitly ends the connection.
ps.flush();
clientConnection.close();
{
connectionCount++; // got another one!
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("This client response was written by server FurrAssignment2"); // to remote client //Changed name
System.out.println("This server response was written by server FurrAssignment2"); // to server console // Changed name
// Print some information locally about the Socket
// connection. This includes the port and IP numbers
// on both sides (the socket pair.)
InetAddress localAddress = clientConnection.getLocalAddress();
InetAddress remoteAddress = clientConnection.getInetAddress();
int localPort = clientConnection.getLocalPort();
int remotePort = clientConnection.getPort();
// My socket pair connection looks like this, to localhost:
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 ))
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 ))
//
// Why is the first IP/port the same, while the second set has
// different ports?
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " +
remoteAddress.toString() + ", " + remotePort + " ))");
System.out.println("got another connection, #" + connectionCount); // report progress
j++;
// Notice the use of flush() and close(). Without
// the close() to Socket object may stay open for
// a while after the client has stopped needing this
// connection. Close() explicitly ends the connection.
ps.flush();
} // got another one!
}
}
catch(Exception e)
......@@ -90,4 +132,4 @@ public class FurrAssignment2
}
}
}
\ No newline at end of file
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