1 / 57

TCP/IP Protocols and Client-Server Applications

TCP/IP Protocols and Client-Server Applications. Objectives: TCP, UDP, HTTP protocols HTTP clients and servers Electronic mail Handling TCP message boundary Handling structured data Socket Interface Synchronous and Asynchronous socket calls. Service.

vevay
Télécharger la présentation

TCP/IP Protocols and Client-Server Applications

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. TCP/IP Protocols and Client-Server Applications Objectives: • TCP, UDP, HTTP protocols • HTTP clients and servers • Electronic mail • Handling TCP message boundary • Handling structured data • Socket Interface • Synchronous and Asynchronous socket calls

  2. Service • Mechanism for computers to interact (application layer) • Term refers to overall solution • Usually associated with IP port number • Differs from protocol which describes the details of how interaction works • Ex) HTTP service builds on TCP/IP • RFC used to define service standard

  3. Applications • Traditional PC applications • Everything done locally • Fast but sharing is difficult • Word, Excel • Client/server applications • Client local and responsive • Client provides interface • Server centralizes resources • Server performs some work

  4. TCP/IP As A Protocol Suite

  5. Recall:TCP sender, receiver establish “connection” before exchanging data segments initialize TCP variables: seq. #s buffers, flow control info (e.g. RcvWindow) client: connection initiator Socket clientSocket = new Socket("hostname","port number"); server: contacted by client Socket connectionSocket = welcomeSocket.accept(); Three way handshake: Step 1:client host sends TCP SYN segment to server specifies initial seq # no data Step 2:server host receives SYN, replies with SYNACK segment server allocates buffers specifies server initial seq. # Step 3: client receives SYNACK, replies with ACK segment, which may contain data TCP Connection Management

  6. TCP Connection Establishment client server CLOSED Active open; SYN Passive open SYN, seq=x SYN/SYN+ACK LISTEN SYN_SENT SYN+ACK, seq=y, ack=x+1 SYN_RCVD SYN+ACK/ACK ACK, ack=y+1 ACK Established Solid line for client Dashed line for server

  7. TCP Connection Termination client server closing FIN_WAIT1 FIN CLOSE_WAIT ACK LAST_ACK FIN_WAIT2 FIN TIME_WAIT ACK timed wait CLOSED CLOSED

  8. Transport Layer Transport Layer Network Layer Network Layer Multiplexing/Demultiplexing HTTP • Use same communication channel between hosts for several logical communication processes • How does Mux/DeMux work? • Sockets: doors between process & host • UDP socket: (dest. IP, dest. Port) • TCP socket: (src. IP, src. port, dest. IP, dest. Port) FTP Telnet

  9. Connectionless demux • UDP socket identified by two-tuple: • (dest IP address, dest port number) • When host receives UDP segment: • checks destination port number in segment • directs UDP segment to socket with that port number • IP datagrams with different source IP addresses and/or source port numbers are directed to the same socket

  10. TCP socket identified by 4-tuple: source IP address source port number dest IP address dest port number recv host uses all four values to direct segment to appropriate socket Server host may support many simultaneous TCP sockets: each socket identified by its own 4-tuple Web servers have different sockets for each connecting client non-persistent HTTP will have different socket for each request Connection-oriented demux

  11. TCP Communication • In a simple TCP client/server application a byte array of 102 may be as the buffer size for the Send and Receive method calls • This worked fine • The program is usually run in a controlled environment • The server and client know that the message size will not be more than this size • What about in real world scenarios???

  12. Problems in TCP Communication • Too Small Buffer Size: • There are situations, where we may not determine the size of the data • We must deal with such situations • For example, if more data arrives than the buffer size • Message Boundary Problem • Due to TCP’s connection-oriented nature, messages are considered to form a continuous stream of bytes • This is implemented using TCP internal buffers, which are used to store messages until they are Received/Sent by applications

  13. Message Boundary Problem

  14. Solutions • Solution # 1: • For Text messages only • Use ReadLine and WriteLine methods of the StreamReader and StreamWriter classes respectively • We are basically inserting end-of-line markers in the message • The problem of too small buffer disappears

  15. Solutions: Cont. • Solution #2: • Send the size of the message before sending the message • Applicable for any type of data where Send and Receive methods are used for sending and Receiving • The receiver will read the data in a loop until the entire size is read • A loop similar to the following is used int total = int.Parse(reader.ReadLine()); byte [] buffer = new byte[1024]; int recv = 0; int sofar = 0; while (sofar < total) { recv = s.Receive(buffer); process(buffer, recv); sofar += recv; }

  16. UDP: User Datagram Protocol[RFC 768] • “bare bones” Internet transport protocol • “best effort” service, UDP segments may be: • lost • delivered out of order to app • Why use UDP? • No connection establishment cost (critical for some applications, e.g., DNS) • No connection state • Small segment headers (only 8 bytes) • Finer application control over data transmission

  17. often used for streaming multimedia apps loss tolerant rate sensitive Other appl. Protocols using UDP DNS SNMP reliable transfer over UDP: add reliability at application layer application-specific error recovery! UDP Segment Structure 32 bits source port # dest port # Length, in bytes of UDP segment, including header checksum length Application data (message) UDP segment format

  18. Contact a Daemon Using Telnet saga10:~> telnet cslibrary.standford.edu 80 Trying 64.233.167.104... Connected to www.google.akadns.net (64.233.167.104). Escape character is '^]'. GET /test.html HTTP/1.0 HTTP/1.0 200 OK Cache-Control: private Content-Type: text/html Set-Cookie: PREF=ID=72459b575402fb39:TM=1089165164:LM=1089165164:S=U8m_gb0hxi2SV KLp; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com Server: GWS/2.1 Content-Length: 2096 Date: Wed, 07 Jul 2004 01:52:44 GMT Connection: Keep-Alive <html><head> …

  19. Example Standardized Services DNS FTP (your Assignment #1) SCP Ping Finger Telnet, SSH SMTP POP IMAP HTTP (your assignment #3)

  20. www.someschool.edu/someDept/pic.gif path name host name Web and HTTP First some jargon • Web page consists of objects • Object can be HTML file, JPEG image, Java applet, audio file,… • Web page consists of base HTML-file which includes several referenced objects • Each object is addressable by a URL • Example URL:

  21. HTTP: hypertext transfer protocol Web’s application layer protocol client/server model client: browser that requests, receives, “displays” Web objects server: Web server sends objects in response to requests HTTP 1.0: RFC 1945 HTTP 1.1: RFC 2068 HTTP overview HTTP request PC running Explorer HTTP response HTTP request Server running Apache Web server HTTP response Mac running Navigator

  22. Steps For Fetching URLs • The browser makes up an absolute URL if URL is relative • The browser examines the protocol part and the host part • Browser uses DNS to resolve host name to IP address • For HTTP protocol, the browser makes a TCP connection to IP address and port 80 • The port maybe :portno following the hostname in URL • The browser sends a GET request as in: GET /path/somefile.htm HTTP/1.0 • The server sends the file and closes the connection. • Browser renders file. If it is HTML file then any image referenced by some <img> tag, then go back to step 4

  23. Uses TCP: client initiates TCP connection (creates socket) to server, port 80 server accepts TCP connection from client HTTP messages (application-layer protocol messages) exchanged between browser (HTTP client) and Web server (HTTP server) TCP connection closed HTTP is “stateless” server maintains no information about past client requests HTTP overview (continued) Protocols that maintain “state” are complex! • past history (state) must be maintained • if server/client crashes, their views of “state” may be inconsistent, must be reconciled

  24. Nonpersistent HTTP At most one object is sent over a TCP connection. HTTP/1.0 uses nonpersistent HTTP Persistent HTTP Multiple objects can be sent over single TCP connection between client and server. HTTP/1.1 uses persistent connections in default mode HTTP connections

  25. Suppose user enters URL www.someSchool.edu/someDepartment/home.index 1a. HTTP client initiates TCP connection to HTTP server (process) at www.someSchool.edu on port 80 Nonpersistent HTTP (contains text, references to 10 jpeg images) 1b. HTTP server at host www.someSchool.edu waiting for TCP connection at port 80, “accepts” connection and notifies the client 2. HTTP client sends HTTP request message (containing URL) into TCP connection socket. Message indicates that client wants object someDepartment/home.index 3. HTTP server receives request message, forms response message containing requested object, and sends message into its socket time

  26. 5. HTTP client receives response message containing html file, displays html. Parsing html file, finds 10 referenced jpeg objects Nonpersistent HTTP (cont.) 4. HTTP server closes TCP connection. time 6.Steps 1-5 repeated for each of 10 jpeg objects

  27. initiate TCP connection RTT request file time to transmit file RTT file received time time Response time modeling Definition of RTT: time to send a small packet to travel from client to server and back. Response time: • one RTT to initiate TCP connection • one RTT for HTTP request and first few bytes of HTTP response to return • file transmission time total = 2RTT+transmit time

  28. Nonpersistent HTTP issues: requires 2 RTTs per object OS must work and allocate host resources for each TCP connection but browsers often open parallel TCP connections to fetch referenced objects Persistent HTTP server leaves connection open after sending response subsequent HTTP messages between same client/server are sent over the same connection Persistent without pipelining: client issues new request only when previous response has been received one RTT for each referenced object Persistent with pipelining: default in HTTP/1.1 client sends requests as soon as it encounters a referenced object as little as one RTT for all the referenced objects Persistent HTTP

  29. HTTP request message • Two types of HTTP messages: request, response • HTTP request message: • ASCII (human-readable format) request line (GET, POST, HEAD commands) GET /somedir/page.html HTTP/1.1 Host: www.someschool.edu User-agent: Mozilla/4.0 Connection: close Accept-language:fr (extra carriage return, line feed) header lines Carriage return, line feed indicates end of message

  30. HTTP request message: general format

  31. Post method: Web page often includes form input Input is uploaded to server in entity body URL method: Uses GET method Input is uploaded in URL field of request line: Uploading form input

  32. HTTP/1.0 GET POST HEAD asks server to leave requested object out of response usually used for debugging HTTP/1.1 GET, POST, HEAD PUT uploads file in entity body to path specified in URL field DELETE deletes file specified in the URL field Allows an application to delete an object on the server Method types

  33. HTTP response message status line (protocol status code status phrase) HTTP/1.1 200 OK Connection close Date: Thu, 06 Aug 1998 12:00:15 GMT Server: Apache/1.3.0 (Unix) Last-Modified: Mon, 22 Jun 1998 …... Content-Length: 6821 Content-Type: text/html data data data data data ... header lines data, e.g., requested HTML file

  34. 200 OK request succeeded, requested object later in this message 301 Moved Permanently requested object moved, new location specified later in this message (Location:) 400 Bad Request request message not understood by server 404 Not Found requested document not found on this server 505 HTTP Version Not Supported HTTP response status codes In first line in server  client response message. A few sample codes:

  35. Many major Web sites use cookies Four components: 1) cookie header line in the HTTP response message 2) cookie header line in HTTP request message 3) cookie file kept on user’s host and managed by user’s browser 4) back-end database at Web site User-server state: cookies

  36. client server usual http request msg usual http response + Set-cookie: 1678 Cookie file Cookie file Cookie file amazon: 1678 ebay: 8734 ebay: 8734 amazon: 1678 ebay: 8734 cookie- specific action usual http request msg cookie: 1678 usual http request msg cookie: 1678 usual http response msg usual http response msg cookie- spectific action Cookies: keeping “state” (cont.) server creates ID 1678 for user entry in backend database access access one week later:

  37. What cookies can bring: authorization shopping carts recommendations one-click shopping user session state (Web e-mail) Cookies (continued) aside Cookies and privacy: • cookies permit sites to learn a lot about you • you may supply name and e-mail to sites • search engines use redirection & cookies to learn yet more • advertising companies obtain info across sites

  38. Goal: don’t send object if cache has up-to-date cached version cache: specify date of cached copy in HTTP request If-modified-since: <date> server: response contains no object if cached copy is up-to-date: HTTP/1.0 304 Not Modified HTTP response HTTP/1.0 304 Not Modified Conditional GET server cache HTTP request msg If-modified-since: <date> object not modified HTTP request msg If-modified-since: <date> object modified HTTP response HTTP/1.0 200 OK <data>

  39. Synchronous and Asynchronous Socket Calls • In human communications, for example, we use both • Asynchronous, and • synchronous messaging • If we need information before taking the next step in our daily tasks • Performing a Google search, purchasing something online — we usually choose synchronous messaging • For similar reasons, client/server applications require both synchronous and asynchronous communications

  40. Synchronous and Asynchronous Socket Calls • Asynchronous and synchronous messaging each have their places in client/server systems, and neither can completely replace the other

  41. Synchronous and Asynchronous Socket Calls • A socket-based client/server application that will allow two-way asynchronous communication between a server and multiple client applications • May uses asynchronous methods • May use threads

  42. Asynchronous Client-Server Applications • the C# and .NET frameworks provide a rich set of functionalities to do asynchronous communications without introducing the complexity of threading

  43. Asynchronous Client-Server Applications • the C# and .NET frameworks provide a rich set of functionalities to do asynchronous communications without introducing the complexity of threading

  44. Synchronous and Asynchronous Socket Calls • A socket-based client/server application that will allow two-way asynchronous communication between a server and multiple client applications • May uses asynchronous methods • May use threads

  45. Asynchronous methods of the Socket & Stream class • The Socket class can be limited to the blocking methods, namely, Accept, Connect, Send and Receive • Similarly, the FileStream and NetworkStream classes(both concrete subclasses of the abstract Stream class), can be limited to the blocking methods, Read and Write Each of these methods blocks the execution of a program until its operation is completed How can we solve the problem of blocking???

  46. Asynchronous methods of the Socket & Stream class • The following are the asynchronous methods of the Socket and Stream classes tabulated based on the tasks they perform Socket Class:

  47. Synchronous and Asynchronous Socket Calls • The following fragment shows how the BeginAccept and the EndAccept methods are used: Socket server = new Socket(AddressFamily.InterNetwok, SocketType.Stream, ProtocolType.Tcp); IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9050); server.Bind(localEP); server.Listen(10); server.BeginAccept(new AsyncCallback(CallAccept), server); … private static void CallAccept(IAsyncResult result) { Socket server = (Socket) result.AsyncState; Socket client = server.EndAccept(result); … }

  48. Three major components: user agents mail servers simple mail transfer protocol: SMTP User Agent a.k.a. “mail reader” composing, editing, reading mail messages e.g., Eudora, Outlook, elm, Netscape Messenger, Thunderbird outgoing, incoming messages stored on server user agent user agent user agent user agent user agent user agent SMTP SMTP SMTP mail server mail server mail server outgoing message queue user mailbox Electronic Mail

  49. Email using SMTP/IMAP/POP • Email consists of two components • Simple Mail Transfer Protocol (SMTP) for email clients to send out emails (e.g. smtp.nd.edu, port 25) • Internet Message Access Protocol (IMAP) or Post Office Protocol (POP) • email clients to receive your emails (e.g. imap.nd.edu, port 143) • You can use telnet to “talk” to these servers directly • E.g. type ‘telnet smtp.nd.edu 25’ and then type ‘help’

  50. Email using SMTP/IMAP/POP • For SMTP, your client (say Outlook), connects to smtp.nd.edu and then delivers an email destined for friend@aol.com • Smtp.nd.edu then locates the SMTP server responsible for AOL. These servers may delegate to other SMTP servers. Eventually it reaches friend@aol.com • Friend will use IMAP or POP to retrieve this email

More Related