Note of

Document Sample
Note of Powered By Docstoc
					                               Chapter 2 Note:

                    Application Layer Protocol

Goal: Define key application layer concepts, including application-layer protocols, clients
and servers, process, sockets, and transport layer interface. Examine application-layer
protocols in detail, including HTTP, FTP, SMTP, POP3, and DNS.

2.1 Principles of Application-Layer Protocols

   Application-Layer Protocols:
    An application’s software is distributed among two or more end systems (that is,
     host computers), for example, web servers and browsers, ftp client and ftp server.
     Email client and email server.
    A process can be thought of as a program that is running within an end system.
    Processes on two different end systems communicate with each other by
     exchanging messages across the computer network. A sending process creates
     and sends messages into the network; a receiving process receives these messages
     and possibly responds by sending messages back. Networking applications have
     application-layer protocols that define the format and order of the messages
     exchanged between processes, as well as define the actions taken on the
     transmission or receipt of a message. Figure 2.1 illustrates that processes
     communicate with each other by using the application layer of the five-layer
     protocol stack.

   Client and Server Side of an Application
    The host that initiates the session is labeled the client.

   Process Communicating Across a Network
    A process sends messages into, and receives messages from, the network through
      its socket.
    Figure 2.3 illustrates socket communication between two processes that
      communicate over the Internet. (Figure 2.3 assumes that the underlying transport
      protocol is TCP, although the UDP protocol could be used as well in the Internet.)
    The application developer has control of everything on the application-layer side
      of the socket but has little control of the transport-layer side of the socket. The
      only control that the application developer has on the transport-layer side is (1)
      the choice of transport protocol and (2) perhaps the ability to fix a few transport-
      layer parameters such as maximum buffer and maximum segment sizes. Once the
      application developer chooses a transport protocol (if a choice is available), the
      application is built using the transport layer services provided by that protocol.
      In Internet applications, the destination host is specified by its IP address. The IP
       address of any end system connected to the public Internet must be globally
       unique, the assignment of IP addresses must be carefully managed.
      A sending application must also provide information that will allow the receiving
       end system to direct the message to the appropriate process on that system. A
       destination port number serves this purpose in the Internet.
      HTTP Server port number 80. SMTP Server on 25. FTP Server on 21 and 20

      A connection has six tuples: (source ip address, source port number, source
       transport protocol, destination ip address, destination port number,
       destination transport protocol).

2.1.2 What Services Does an Application Need?

      When you develop an application, you must choose one of the available transport
       protocols TCP and UDP. We can broadly classify an aplication’s service
       requirements along three dimensions: data loss, bandwidth, and timing.
      Reliable Data Transfer: Some applications, such as electronic mail, instant
       messaging, file transfer, remote host access, Web document transfers, and
       financial applications require fully reliable data transfer, that is, no data loss. In
       particular, a loss of file data, or data in a financial transaction, can have
       devastating consequences (in the latter case, for either the bank or the customer!).
       Other loss-tolerant applications, most notably multimedia applications such as
       real-time audio/video or stored audio/video, can tolerate some amount of data
      Bandwidth: Some applications must be able to transmit data at a certain rate in
       order to be effective. Many current multimedia applications are bandwidth
       sensitive. Electronic mail, file transfer, remote access, and Web transfers are not
       bandwidth-sensitive application.
      Timing: Interactive real-time applications, such as Internet telephony, virtual
       environments, teleconferencing, and multiplayer games require tight timing
       constraints on data delivery in order to be effective. For example, many of these
       applications require that end-to-end delays be on the order of a few hundred
       milliseconds or less. Examine Figure 2.4.

2.1.3 Service Provided by TCP, UDP
    TCP: Transmission Control Protocol. UDP: User Data Protocol.

   TCP Services:
    TCP service is obtained by having both the sender and receiver create end points,
     called sockets. Each socket has a socket number consisting of the IP address of
     the host and a 16-bit number local to that host, called a port. To obtain TCP
     service, a connection must be explicitly established between a socket on the
     sending machine and a socket on the receiving machine. A socket may be used
     for multiple connections at the same time.
    Port numbers below 1024 are called well-known ports and are reserved for
     standard services.
    All TCP connections are full duplex and point-to-point. Full duplex means that
     traffic can go in both directions at the same time. Point-to-point means that each
     connection has exactly two end points. TCP does not support multicasting or
    A TCP connection is a byte stream, not a message stream. Message boundaries
     are not persevered end to end. For example, if the sending process does four 512-
     byte writes to a TCP stream, these data may be delivered to the receiving process
     as four 512-byte chunks, two 1024-byte chunks, one 2048-byte chunk, or some
     other way. There is no way for the receiver to detect the units in which data are
    When an application passes data to TCP, TCP may send it immediately or buffer
     at its discretion.
    TCP service offers reliable transport service and congestion control.

 UDP Services:
  UDP does not establish a connection between two sockets. No guarantee that the
   message will ever reach the receiving socket. UDP does not have a congestion
   control. Neither TCP nor UDP offers timing guarantee.

 We cover HTTP, FTP, SMTP, POP3, IMAP and DNS protocols.
 Here is a summary of the application protocols and the transport protocol and port
 numbers at servers.

              Protocol        Type           Port Number
              HTTP            TCP            80
              FTP             TCP            21, 20
              SMTP            TCP            25
              POP3            TCP            110
              IMAP            TCP            143
              DNS             UDP            53
    Every Web site has a server process listening to TCP port 80 for incoming
     connections from clients. After a connection has been established, the client sends
     one request and the server sends one reply. Then the connection is released. The
     protocol that defines the legal request and replies is called HTTP.

 Example: User types in
 The steps that occur between the user’s click and the page being displayed are as
        1. The browser determines the URL.
        2. The browser asks DNS (Domain Name Server) for the IP address of
        3. DNS replies with
       4. The browser makes a TCP connection to port 80 on

       5. It then sends Get /classes/cs383/383syllabus.html command.
       6. The sends the file 383syllabus.html.
       7. The TCP connection is released.
       8. The browser displays all the text in 383syllabus.html.
       9. The browser fetches and displays all images in syllabus.html.

   A HTTP request message contains text lines. The first line is called the request
    line followed by several Header lines. A blank line ends the header section. The
    blank line is then followed by an Entity body which contains data that client sends
    to the server.

    Example HTTP Request Message

    Get /classes/cs383/383syllabus.html HTTP /1.1
    Connection: Close
    User-Agent: Mozilla/4.0
    Accept-language: fr
    Last-Modified: Mon 22, Jan 24, 2002, 09:23:24 GMT
    Content-Length: 6821
    Content-type: text/html

    (data data …)

    The first word of the request line is the command. The built-in HTTP request

    Method                                     Description
    Get                                        Request to read a Web page
    HEAD                                       Request to read a page’s header
    Put                                        Request to store a Web page
    POST                                       Append to a named resource
    DELETE                                     Remove the Web page
    LINK                                       Connects two existing resources
    UNLINK                                     Breaks an existing connection between
                                               two resources.

    Get is the mostly used HTTP request method.

   Every request gets a response consisting of a status line, and possibly additional
    information, the page.

    Example HTTP Response message:
     HTTP/1.0 200 Document follows
     MIME-Version: 1.0
     Content-Type: text/html
     Content-Length: 8247

     <HEAD><TITLE>The Syllabus of CS 383</TITLE></HEAD>

    Some of the important lines:


 Server may send an HTTP response with header line “401 Authorization
 Required” asking users to log in. The client then sends the user account information
 in Authorization: header line.

 Provide a way that web server identify its users.

 Server create database item storing information about user. A number, for example
 1678453, indexes that information. The server then sends an HTTP response with a
 Set-cookie: header.

 Set-cookie: 1678453

 The client then stores the number associated with the server web name, and user
 information in a local file. The next time the user visits the same web site, the client
 sends an HTTP request with a Cookie: header line:

 Cookie: 1678453

 Conditional Get:

 Client sends an HTTP request with a header line

 If-modified-since: date info…

 After getting this response, the server sends an HTTP response with page content if
 the data and time of the page on the sever is newer.

Ftp is the Internet standard for file transfer. Unlink most other TCP applications,
it used two TCP connections between the client and server – a control connection
that is left up for the duration of the client-server session, and a data connection
that is created and deleted as necessary.
Example: ftp session

Connected to
220 NcFTPd Server (licensed copy) ready.
User ( anonymous
331 Guest login ok, send your complete e-mail address as password.
230-You are user #1 of 50 simultaneous users allowed.
230 Logged in anonymously.
ftp> list
Invalid command.
ftp> get
Remote file readme.txt
Local file readme.txt
200 PORT command successful.
550 No such file.
ftp> dir
200 PORT command successful.
150 Opening ASCII mode data connection for /bin/ls.
lrwxrwxrwx 1 ftpuser ftpusers         7 Jun 21 2001 bin -> usr/bin
drwxrwxr-x 250 ftpuser ftpusers      4608 May 3 2001 concluded-wg-ietf-mail-a
dr-xr-xr-x 2 ftpuser ftpusers     512 Jul 19 1996 dev
dr-xr-xr-x 2 ftpuser ftpusers     512 Aug 13 1998 etc
drwxr-xr-x 3 ftpuser ftpusers     6656 Aug 9 14:39 iesg
drwxrwxr-x 485 ftpuser ftpusers      9216 Aug 16 13:43 ietf
drwxr-xr-x 6 ftpuser ftpusers      512 Feb 2 1999 ietf-gopher
drwxrwxr-x 209 ftpuser ftpusers      4096 Aug 28 09:35 ietf-mail-archive
drwxr-xr-x 16 ftpuser ftpusers      512 May 30 2001 ietf-online-proceedings
drwxrwxr-x 2 ftpuser ftpusers 184832 Sep 5 15:12 internet-drafts
dr-xr-xr-x 2 ftpuser ftpusers     512 Aug 29 1996 lib
drwxr-xr-x 2 ftpuser ftpusers      512 Apr 17 1996 lost+found
drwxr-xr-x 2 ftpuser ftpusers      512 Jan 8 1999 pub
drwxrwxr-x 7 ftpuser ftpusers 74240 Sep 5 05:30 rfc
dr-xr-xr-x 5 ftpuser ftpusers     512 Apr 18 1996 usr
226 Listing completed.
ftp: 1016 bytes received in 0.08Seconds 13.03Kbytes/sec.
        FTP commands are 3 or four bytes of uppercase ASCII characters, some with
         optional arguments.

     Common FTP commands:
     FTP Command            Description
     ABOR                   Abort previous FTP command and any data transfer
     LIST filelist          List files or directories
     PASS password          Password on server
     QUIT                   Logoff from server
     RETR filename          Retrieve a file
     STOR filename          Store a file
     USER                   Username on server

     FTP replies are 3-digit numbers in ASCII, with an optional message
     following the number. Examples of FTP replies follow:

     331 User Name, OK, password required
     125 Data connection already open, transfer starting
     425 Can’t open data connection
     452 Error Writing File
     226 Listing completed.
     221 Goodbye.

     Note: FTP uses out-of-band communication. HTTP uses in-bound. Out-of-bound:
     control information are sent with a connection separate from data connection.
     With in-bound communication, the control information is sent within the data in
     data connection, in the form of header lines and status lines. They are two very
     different design approaches.

SMTP Protocol
    The elements of an email system: Sender’s user agent software (such as Outlook),
     Sender’s email server, receiver’s email server, receiver’s user agent (such as
    Simple Mail Transfer Protocol (SMTP) transfers messages from senders mail
     servers to the recipients’ mail servers.

 A common scenario:
 Suppose Alice wants to send Bob a simple ASCII message:

         1. Alice invokes her user agent for e-mail, provides Bob’s e-mail address (for
            example,, composes a message, and instructs
            the user agent to send the message.
        2. Alice’s user agent sends the message to her mail server, where it is placed
           in a message queue.
        3. The client side of SMTP, running on Alice’s mail server, sees the message
           in the message queue. It opens a TCP connection to an SMTP server,
           running on Bob’s mail server.
        4. After some initial SMTP handshaking, the SMTP client sends Alice’s
           message into the TCP connection.
        5. At Bob’s mail server, the server side of SMTP receives the message.
           Bob’s mail server then places the message in Bob’s mailbox.
        6. Bob invokes his user agent to read the message at his convenience.

    User composes a message, and hit the Send button. The following SMTP
     messages and data are transmitted, as soon as the TCP connection is established.

S:   220
S:   250 Hello, pleased to meet you
C:   MAIL FROM: <>
S:   250 Sender ok
C:   RCPT TO: <>
S:   250 ... Recipient ok
S:   354 Enter mail, end with “.” on a line by itself
C:   Do you like ketchup?
C:   How about pickles?
C:   .
S:   250 Message accepted for delivery
S:   221 closing connection

Explain this transcript.

Some SMTP commands:

Command            Description
HELO               Introduce yourself
MAIL FROM          Specify the sender
RCPT TO:           Specify the recipient
DATA               Specify the body of the messages
QUIT               Quit this session

    If, for some reason, the SMTP server at client side cannot connect with the SMTP
     server at the server side, then the messages goes to a message queue.
POP 3 (Post Office Protocol Version 3)
    o POP 3 server maintains a collection of text file – one for each email account.
      When a message arrives, the POP 3 server simply appends it to the bottom of
      the recipient’s file!
    o When you check your email, you e-mail client connects to the POP3 server
      using port 110. Once you have logged in, the POP3 server opens your text file
      and allows you to access it, bringing copies of your email message to your
      local machine.
    o Some common POP3 commands:

    POP3 commands                 Description
    list                          List the messages and their size
    retr                          Retrieve a message, pass it a message number
    dele                          Delete a message, pass it a message number
    top                           Show the top x lines of a message, pass it a message number
                                  and the number of lines
    user                          Enter your user id
    pass                          Enter your password
    quit                          Quit the pop3 server

    Interaction between POP3 client and POP3 server:

    C:     list
    S:     1 498
    S:     2 912
    S:     .
    C:     retr 1
    S:     (blah blah ...
    S:     .................
    S:     ..........blah)
    S:     .
    C:     dele 1
    C:     retr 2
    S:     (blah blah ...
    S:     .................
    S:     ..........blah)
    S:     .
    C:     dele 2
    C:     quit
    S:     +OK POP3 server signing off

            POP3 server does not maintain a hierarchical folder on a remote server. In
             order to do that, user the IMAP protocol. IMAP stand for Internet Mail
             Access Protocol.
MIME (Multipurpose Internet Mail Extensions)
RFC 822
      Use to encode non-ASCII data in HTTP content, email. Example of non-
       ASCII data including: exe files, document files (.doc, .pdf…), image files
       (.jpg, .gif)
      When an non-ASCII data is included in an HTTP, SMTP responses, special
       header lines inside the response tell information about the MIME document.
      MIME define new message headers and encoding rules for non-ASCII

       SMTP Example:

       Subject: Picture of yummy crepe.
       MIME-Version: 1.0
       Content-Transfer-Encoding: base64
       Content-Type: image/jpeg
       (base64 encoded data …..
       …...base64 encoded data)

       Content-Type: text/richtext

       Happy Birthday to you
      Headers added by MIME:

   Header                   Meaning
   MIME-Version:            Identifies the MIME version
   Content-Description      Human-readable string telling what is in the
   Content-ID               Unique identifier
   Content-Transfer-Enoding How the body is wrapped for transmission
   Content-Type             Nature of the message

      MIME types and subtypes are defined in RFC1521. List of the
       types/subtypes: text/plain, text/richtext, image/gif, image/jpeg, audio/basic,
       video/mpeg, applcation/postscript, ….

      Base64 coding: groups of 24 bits are broken up into four 6-bit units, with
       each unit being sent as legal ASCII character. Arbitrary binary text can be sent
       safely using this scheme.
DNS (Domain Name Server)
      The domain name space is hierarchical, similar to the Unix filesystem. It’s an
       tree structure. Unnamed Root->top level domain (com, edu, gov, int, mil, net,
       uk, us, tw,…)->second level domain (such as
      Every node has a label of up to 63 characters. The root of the tree is a special
       node with a null labe. The domain name of any node in the tree is the list of
       labels, starting at the node, working up to the root, using a period to separate
       the labels.
      When a user types in in Internet Explorer, a chain of
       queries to domain name servers is invoked. Local domain name server->root
       domain name server -> authoritative name server. (>one of the
       12 root domain server-->the authoritative name server).
      Resolving of domain name can be invoked programmatically by the API
       gethostbyname() available in most systems.
      Not every name server knows how to contact every other name server. Instead
       every name server must know how to contact the root name server. . Local
       name server stores the list of root name server. (their ip address).
      The root name server knows the name and location (ip address) of each
       authoritative name server for all the second-level domains.
      The resolving process can be implemented in one of two ways: recursive or

Programming with Microsoft Winsock API
   Server:                Client:
   Socket()               Socket()
   Bind()                 Connect()
   Listen()               Send()
   Accept()               Receive()

   Socket():The socket function causes a socket descriptor and any related resources
   to be allocated and bound to a specific transport service provider.

   Bind():The Windows   Sockets bind function associates a local address with a

   Listen():The Windows Sockets listen function places a socket a state where it is
   listening for an incoming connection.
Accept():The Windows Sockets accept function accepts an incoming connection
attempt on a socket.

Connect():The Windows Sockets connect function establishes a connection to a
specified socket.

Send(), Receive(): Send/Receive data to/from a connected socket.

Source code of a TCP Echo Server:


#define STRICT
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <commdlg.h>
#include <string.h>
#include <memory.h>
#include <process.h>

//Note: remember to link with ws2_32.lib!!

int main(int argc, char *argv[])
       SOCKET sock, sock2;
       SOCKADDR_IN sa1, sa2;
       WSADATA WSAdata1;
       int rc, STATUS, nLEN;
       LPSTR lpBUFF;
       LPSTR lpOUTPUT = "Server says hello !\n";

       printf("SSOCK01: Main called\n");

       // Global initialization
       lpBUFF = malloc(256);
       memset(lpBUFF, 0, 256);

       // WinSock initialization.
       STATUS = WSAStartup(MAKEWORD(1,1),
if(STATUS == 0)
      printf("SSOCK01: WSAStartup is OK\n");
      printf("SSOCK01: Error with WSAStartup\n");
      return 0;

// Create TCP socket.
sock = socket(AF_INET, SOCK_STREAM, 0);

if(sock != INVALID_SOCKET)
        printf("SSOCK01: Socket created\n");
        printf("SSOCK01: Creation of socket failed\n");
        return 0;

// Fill in the sock_addr.
sa1.sin_family = AF_INET;
sa1.sin_addr.s_addr = INADDR_ANY;
sa1.sin_port = htons(10000);

printf("SSOCK01: SA1 filled in\n");

// Bind to specific local address
rc = bind(sock,
        (struct sockaddr FAR *) &sa1,
if(rc == 0)
        printf("Bind is OK\n");
        printf("Bind failed\n");
        return 0;

// Passive mode
rc = listen(sock, 5);
if(rc == 0)
         printf("Listen is OK\n");
         printf("Listen failed\n");
         return 0;

// Accept client connection
nLEN = sizeof(sa2);
sock2 = accept(sock,
       (struct sockaddr FAR *) &sa2,

if(sock2 != INVALID_SOCKET)
       printf("SSOCK01: Accept OK\n");
       printf("SSOCK01: Accept failed\n");
       return 0;

// Read client data
rc = recv(sock2,

if(rc == 0 || rc == SOCKET_ERROR)
        printf("SSOCK01: Error reading from socket\n");
        return 0;
               printf("%s\n", lpBUFF);

       // Write client data
       rc = send(sock2,

       return 0;

Run time output:
SSOCK01: Main called
SSOCK01: WSAStartup is OK
SSOCK01: Socket created
SSOCK01: SA1 filled in
Bind is OK
Listen is OK
SSOCK01: Accept OK
Client is here !

Source code for a TCP Echo Client:


#define STRICT
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <commdlg.h>
#include <string.h>
#include <memory.h>
#include <process.h>

//Note: remember to link with ws2_32.lib!!

int main(int argc, char *argv[])
       SOCKET sock;
int rc, STATUS, nLEN;
LPSTR lpOUTPUT = "Client is here !\n";

printf("CSOCK01: Main called\n");

// Global initialization
lpBUFF = malloc(256);
memset(lpBUFF, 0, 256);

// WinSock init.

if(STATUS == 0)
      printf("CSOCK01: WSAStartup is OK\n");
      printf("CSSOCK01: Error with WSAStartup\n");
      return 0;

// Create TCP socket.
sock = socket(AF_INET, SOCK_STREAM, 0);

if(sock != INVALID_SOCKET)
        printf("CSOCK01: Socket created\n");
        printf("CSOCK01: Creation of socket failed\n");
        return 0;
// Determine host IP address
rc = gethostname(lpBUFF, 256);
if(rc == 0)
        printf("%s\n", lpBUFF);
        pHE = gethostbyname(lpBUFF);
        if(pHE == 0)
                printf("ERROR with gethostbyname\n");
                printf("%s\n", pHE->h_name);
                printf("HLEN = %d\n", pHE->h_length);
        printf("CSOCK01: Error gethostname\n");
        return 0;

// Fill in the sock_addr.
sa1.sin_family = AF_INET;
memcpy((char FAR *) &(sa1.sin_addr),
//gethostname(szBUFF, sizeof(szBUFF));
//sa1.sin_addr.s_addr = INADDR_ANY;
sa1.sin_port = htons(10000);

printf("CSOCK01: SA1 filled in\n");

// Initiate connect to server
rc = connect(sock,
         (struct sockaddr FAR *) &sa1,

if(rc == 0)
        printf("CSOCK01: Connect OK\n");
            printf("CSOCK01: Connect failed\n");
            return 0;

     // Write data to server
     rc = send(sock,

     rc = recv(sock,

     if(rc == 0 || rc == SOCKET_ERROR)
             printf("CSOCK01: Error reading from socket\n");
             return 0;
             printf("%s\n", lpBUFF);

     return 0;

Run time output:
CSOCK01: Main called
CSOCK01: WSAStartup is OK
CSOCK01: Socket created
HLEN = 4
CSOCK01: SA1 filled in
CSOCK01: Connect OK
Server says hello !

Shared By:
jianghongl jianghongl http://