1 / 32

Networking and Sockets

Networking and Sockets. IST 411 Lecture 2 Spring 2004. Protocols. A protocol are the rules that govern information communication HTTP (HyperText Markup Language) – protocol to interpret HTML pages SMTP (Simple Mail Transfer Protocol) – rules that govern transfer of e-mail

jmilliken
Télécharger la présentation

Networking and Sockets

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. Networking and Sockets IST 411 Lecture 2 Spring 2004

  2. Protocols • A protocol are the rules that govern information communication • HTTP (HyperText Markup Language) – protocol to interpret HTML pages • SMTP (Simple Mail Transfer Protocol) – rules that govern transfer of e-mail • FTP (File Transfer Protocol) – rules for the transfer of files across the Internet • Each is an example of an application protocol

  3. Protocols • URL (Uniform Resource Locator) – web address that specifies 3 bits of information: • Method used to transfer information, i.e., HTTP or FTP • Address of the host computer, i.e., www.hbg.psu.edu • Path describing where the file is located on the host, i.e., /hbg/course00.html

  4. Protocols • E-mail address is specified by the SMTP protocol which includes: • A local mailbox address, i.e., jxs121 • Followed by the address of the computer, i.e., psu.edu

  5. Protocols • DNS (Domain Name System) – governs how names can be translated into numeric addresses, i.e., psu.edu • DNS divides the Internet into a hierarchy of domains and subdomains • Domains are names like com, edu, mil • DNS servers – job to translate names to numeric addresses whenever they are requested to do so by clients such as the SMTP or HTTP server

  6. Client/Server Applications • HTTP, FTP, SMTP, and DNS protocols are examples of client/server protocols • Client/Server application is divided into two subtasks: • One performed by the client • One performed by the server Request service Server Client Provide service

  7. Protocols • Internet has several levels of software and hardware, each with collection of protocols • Application level is the highest level • Transmission Protocols represent the next level • TCP (Transfer Control Protocol) • UDP (User Datagram Protocol) • Govern the transfer of large blocks of information called packets between networked computers

  8. Protocols • Lowest end of the hierarchy of protocols are those governing transmission of bits or electronic pulses over wires • Most of these protocols are built into the hardware • On top of these are protocols such as ethernet protocol and token ring protocol • These lower level protocols are vastly different from each other

  9. Protocols • To connect disparate networks, use IP (Internetworking Protocol) to translate one network protocol to a common format

  10. Client/Server Applications • Java includes a powerful set of classes the supports network programming and handles even the highest level of protocols needed to write client/server applications • java.net package

  11. Client/Server Applications • java.net.URL class • Used to download WWW pages • Used to access files stored on the Web as input files to an applet or application program • Socket and ServerSocket classes • Methods that let us develop our own networking applications • Enable read and write data through InputStreams and OutputStreams

  12. Client/Server Applications • DatagramPacket and DatagramSocket classes • Provide support for lower level networking applications based on Internet packets

  13. Client/Server Applications • Lab 1 – Look up local host

  14. Client/Server Applications • Algorithm to download a data file from the Internet: • Create a URL instance • Open an InputStream to it • Read the data • Close the stream

  15. Client/Server Applications try { URL url = new URL(fileURL); data = url.openStream(); String line = data.readLine(); data.close(); } catch (MalformedURLException e) { System.out.println("ERROR: " + e.getMessage()); } catch (IOException e) { System.out.println("ERROR: " + e.getMessage()); }

  16. Client/Server Applications • Lab 2 – Real estate lookup

  17. Sockets

  18. Sockets • Client / server model based on socket connection • Socket – simple communication channel for two programs to communicate over a network • Two-way communication • Server creates a certain port and waits until a client requests a connection • Port is a particular address or entry point on the host computer represented by an integer

  19. Sockets • By convention, port numbers from 0 to 1023 are reserved for special services

  20. Sockets • Ports above 1023 may be used for any user application • The port is not a hardware concept • It is a network abstraction to allow easy development programs reading and writing data to a network • Each communication channel into and out of a TCP/IP computer is identified by: • IP address • Port number • Combination creates the abstraction called a socket

  21. Sockets • Socket has two channels • one for input • one for output

  22. Server Protocol • Four steps to create the server: • Create a ServerSocket and establish a port number (ServerSocket constructor throws an IOException and a BindException) • Listen for and accept a connection from a client • Converse with the client • Close the socket

  23. Server Protocol try { ServerSocket server = new ServerSocket(8000); Socket nextClient = server.accept( ); // Communicate with the client nextClient.close( ); } catch (IOException ioe) { System.out.println("I/O error: " + ioe.getMessage()); }

  24. Let’s Practice • The Server.

  25. Client Protocol • Three steps to create the client: • Open a socket connection to the server, giving its address (Socket constructor throws an IOException) • Converse with the server • Close the connection

  26. Client Protocol String hostname = "localhost"; // Could be a URL try { Socket timeDay = new Socket (hostname, 8000); // Communicate with the client timeDay.close(); } catch (IOException e) { System.out.println("*** ERROR: " + e.getMessage()); }

  27. Let’s Practice • The Client.

More Related