1 / 12

CSE 1341 Honors

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

balin
Download Presentation

CSE 1341 Honors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 15

  2. 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

  3. 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.

  4. 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

  5. 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

  6. 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.

  7. Example Scenario Network Server 192.168.0.101 Client 192.168.0.202

  8. 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.

  9. 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);

  10. 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());

  11. 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()); }

  12. 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

More Related