120 likes | 252 Views
CSE 1341 Honors. Professor Mark Fontenot Southern Methodist University Note Set 15. Networking. IP: 192.168.0.123. Network. IP: 97.99.88.77. Every computer connected to a network has a unique ID called an IP Address. IP: 111.222.121.99. Client/Server Model. Data Transfer
E N D
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 15
Networking IP: 192.168.0.123 Network IP: 97.99.88.77 Every computer connected to a network has a unique ID called an IP Address IP: 111.222.121.99
Client/Server Model Data Transfer Between client and server Client- Actively makes a Connection to the server Server- Waits Passively for a connection from a client Each computer has many “ports” that allow it to communicate with More than one other computer.
On The Server Pseudocode Create a server socket on port 8856 while ( no client has connected ) wait for a connection from a client “handle” the connection from the client • Different Types of Servers: • E-Mail Server (wait for a mail client like Outlook to connect) • Web Server (waits for a web client (browser) to connect and request a webpage) • FTP Server (waits for a connect and transfer files b/w two computers
On The Server • A Server (physical machine) can run many different services (logical servers) at the same time. • Each service is bound to a particular port. • This means that the service listens or waits for incoming connections on that port. • A port is identified by a unique number • Therefore, to connect to a server running on a particular port, the client needs to know the server’s ip address and port number
How to find your computer’s IP • Mac • open Terminal app (in utilities) • type ifconfig • look for line that says inet • Windows • Open command prompt (cmd) • type ipconfig • look for line that says IP Address Note: Every time you connect to a different network, you could get a different IP address.
Example Scenario Network Server 192.168.0.101 Client 192.168.0.202
Creating a Network Server in Java Let’s say this server is running on computer w/ IP 192.168.0.101 ServerSocket listener = new ServerSocket(3333); Socket connection; //Blocks and waits for some client to attempt to connect //Will accept the connection only on the port listed above connection = listener.accept(); //More code to come So… a client would attempt to connect to IP 192.168.0.101 on port 3333.
Creating a Network Client in Java Let’s say this client is running on computer w/ IP 192.168.0.202 //attempt to connect to server running at 192.168.0.101 Socket client = new Socket(“192.168.0.101”, 3333);
Communication • Two computers connected to the same socket can send and receive information to/from each other //attempt to connect to server running on Socket client = new Socket(“192.168.0.101”, 3333); //Write to socket with a printwriter PrintWriter output = new PrintWriter(client.getOutputStream()); //Read from socket with a Scanner – just like from //the keyboard Scanner input = new Scanner (client.getInputStream());
Exception Handling • Helps recover from runtime errors • Try to connect to a server but can’t get a connection • Some methods throw exceptions that must be caught in the code you write. try { //attempt to connect to server running on Socket client = new Socket(“192.168.0.101”, 3333); } catch (Exception e) { System.out.println(“Error”); System.out.println(e.getMessage()); }
Breakout IN Groups • Modify Simple Server • Have server generate a random number between 1 and 10, then wait for a connection • Upon connection, read a number from the input stream and check. • Send back to client a message of correct guess or incorrect guess • Modify Simple Client • Get a number between 1 and 10 from the user • Connect to server and then send number • Read back response and display to the user