Skip to content
Snippets Groups Projects
Commit 4d16165d authored by danielcain's avatar danielcain
Browse files

assignment 1 initial. unable to compile/run

parent ec17f8ef
No related branches found
No related tags found
No related merge requests found
package CainAssignment1;
import java.io.*;
import java.net.*;
/**
* changed telnet # from 2317 to 2318. This won't affect the protocol handshake
* as long as client inputs the correct telnet #.
*
* telnet localhost 2318
*
* ask for the ip address of the server
* <code>telnet ipOfServersLaptop 2318</code>
*/
public class CainAssignment1 {
public static void main(String[] args) {
try {
int popularityCount = 0; // state
ServerSocket serverSocket = new ServerSocket(2318); // server decides here what port to listen on.
// of interest: often client doesn't care what port it uses locally when connecting to that server port.
// Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
while (true) {
Socket clientConnection = serverSocket.accept(); // blocks! then proceeds once a connection is "accept"ed
/**
* changed connectionCount to popularityCount
*/
popularityCount++;
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("This client response was written by server CainAssignment1"); // to remote client
System.out.println("This server response was written by server CainAssignment1"); // to server console
ps.println("You were connection #" + popularityCount + ", by my count");
// 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(); // remember the prior question, why are 2 ports different?
// 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 )) note IPv6
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 ))
System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( "
+ remoteAddress.toString() + ", " + remotePort + " ))");
System.out.println("you'er server is blowing up! Now serving #" + popularityCount); // report progress
ps.flush();
clientConnection.close();
}
} catch (Exception e) {
/**
* added to the println for fun
*/
System.out.println("you got all sorts of problems with your networking: " + 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