Chapter 2: Application Layer - PowerPoint PPT Presentation

1 / 84
About This Presentation
Title:

Chapter 2: Application Layer

Description:

User-agent: Mozilla/4.0. Connection: close. Accept-language:fr ... Post method: Web page often includes form input. Input is uploaded to server in entity body ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 85
Provided by: jimku89
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2: Application Layer


1
Chapter 2 Application Layer
  • learn about protocols by examining popular
    application-layer protocols
  • HTTP
  • SMTP / POP3 / IMAP
  • DNS
  • programming network applications
  • socket API
  • Our goals
  • conceptual, implementation aspects of network
    application protocols
  • transport-layer service models
  • client-server paradigm

2
Chapter 2 outline
  • 2.1 Principles of app layer protocols
  • clients and servers
  • app requirements
  • 2.2 Web and HTTP
  • 2.4 Electronic Mail
  • SMTP, POP3, IMAP
  • 2.5 DNS
  • Socket programming with TCP
  • Socket programming with UDP

3
Network applications some jargon
  • Process program running within a host.
  • within same host, two processes communicate using
    interprocess communication (defined by OS).
  • processes running in different hosts communicate
    with an application-layer protocol
  • user agent interface between user and network
    application
  • implements user interface application-level
    protocol
  • Web browser
  • E-mail mail reader
  • streaming audio/video media player

4
Applications and application-layer protocols
  • Application communicating, distributed processes
  • e.g., e-mail, Web, P2P file sharing, instant
    messaging
  • running in end systems (hosts)
  • exchange messages to implement application
  • Application-layer protocols
  • one piece of an app
  • define messages exchanged by apps and actions
    taken
  • use communication services provided by lower
    layer protocols (TCP, UDP)

5
App-layer protocol defines
  • Types of messages exchanged, eg, request
    response messages
  • Syntax of message types what fields in messages
    how fields are delineated
  • Semantics of the fields, ie, meaning of
    information in fields
  • Rules for when and how processes send respond
    to messages
  • Public-domain protocols
  • defined in RFCs
  • allows for interoperability
  • eg, HTTP, SMTP
  • Proprietary protocols
  • eg, Internet phone, KaZaA

6
Client-server paradigm
  • Typical network app has two pieces client and
    server
  • Client
  • initiates contact with server (speaks first)
  • typically requests service from server,
  • Web client implemented in browser e-mail in
    mail reader
  • Server
  • provides requested service to client
  • e.g., Web server sends requested Web page, mail
    server delivers e-mail

7
Processes communicating across network
  • process sends/receives messages to/from its
    socket
  • socket analogous to door
  • sending process shoves message out door
  • sending process assumes transport infrastructure
    on other side of door which brings message to
    socket at receiving process

controlled by app developer
Internet
controlled by OS
  • API (Application Program Interface) (1) choice
    of transport protocol (2) ability to fix a few
    parameters (lots more on this later)

8
Addressing processes
  • For a process to receive messages, it must have
    an identifier
  • Every host has a unique 32-bit IP address
  • Q does the IP address of the host on which the
    process runs suffice for identifying the process?
  • Answer No, many processes can be running on same
    host
  • Identifier includes both the IP address and port
    numbers associated with the process on the host.
  • Example port numbers
  • HTTP server 80
  • Mail server 25

9
What transport service does an app need?
  • Data loss
  • some apps (e.g., audio/video) can tolerate some
    loss
  • other apps (e.g., file transfer, telnet) require
    100 reliable data transfer
  • Bandwidth
  • some apps (e.g., multimedia) require minimum
    amount of bandwidth to be effective
  • other apps (elastic apps, e.g., email, file
    transfer) make use of whatever bandwidth they get
  • Timing
  • some apps (e.g., Internet telephony, interactive
    games) require low delay to be effective

10
Transport service requirements of common apps
Time Sensitive no no no yes, 100s msec yes,
few secs yes, 100s msec yes and no
Application file transfer e-mail Web
documents real-time audio/video stored
audio/video interactive games instant messaging
Bandwidth elastic elastic elastic audio
5kbps-1Mbps video10kbps-5Mbps same as above few
kbps up elastic
Data loss no loss no loss no loss loss-tolerant
loss-tolerant loss-tolerant no loss
11
Internet transport protocols services
  • UDP service
  • unreliable data transfer between sending and
    receiving process
  • does not provide connection setup, reliability,
    flow control, congestion control, timing, or
    bandwidth guarantee
  • Q why bother? Why is there a UDP?
  • TCP service
  • connection-oriented setup required between
    client and server processes
  • reliable transport between sending and receiving
    process
  • flow control sender wont overwhelm receiver
  • congestion control throttle sender when network
    overloaded
  • does not providing timing, minimum bandwidth
    guarantees

12
Internet apps application, transport protocols
Application layer protocol SMTP RFC
2821 Telnet RFC 854 HTTP RFC 2616 FTP RFC
959 proprietary (e.g. RealNetworks) proprietary (
e.g., Dialpad)
Underlying transport protocol TCP TCP TCP TCP TCP
or UDP typically UDP
Application e-mail remote terminal access Web
file transfer streaming multimedia Internet
telephony
13
Chapter 2 outline
  • 2.1 Principles of app layer protocols
  • clients and servers
  • app requirements
  • 2.2 Web and HTTP
  • 2.4 Electronic Mail
  • SMTP, POP3, IMAP
  • 2.5 DNS
  • Socket programming with TCP
  • Socket programming with UDP

14
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

15
HTTP overview
  • HTTP hypertext transfer protocol
  • Webs 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,2616

HTTP request
PC running Explorer
HTTP response
HTTP request
Server running Apache Web server
HTTP response
Mac running Navigator
16
HTTP overview (continued)
  • 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

17
HTTP connections
  • 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

18
Nonpersistent HTTP
(contains text, references to 10 jpeg images)
  • Suppose user enters URL www.someSchool.edu/someDep
    artment/home.html
  • 1a. HTTP client initiates TCP connection to HTTP
    server (process) at www.someSchool.edu on port 80

1b. HTTP server at host www.someSchool.edu
waiting for TCP connection at port 80. accepts
connection, notifying client
2. HTTP client sends HTTP request message
(containing URL) into TCP connection socket.
Message indicates that client wants object
someDepartment/home.html
3. HTTP server receives request message, forms
response message containing requested object, and
sends message into its socket
time
19
Nonpersistent HTTP (cont.)
4. HTTP server closes TCP connection.
  • 5. HTTP client receives response message
    containing html file, displays html. Parsing
    html file, finds 10 referenced jpeg objects

time
6. Steps 1-5 repeated for each of 10 jpeg objects
20
Response time modeling
  • Definition of RRT 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 2RTTtransmit time

21
Persistent HTTP
  • 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
  • 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 connection

22
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-languagefr
(extra carriage return, line feed)
header lines
Carriage return, line feed indicates end of
message
23
HTTP request message general format
24
Uploading form input
  • 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

www.somesite.com/animalsearch?monkeysbanana
25
Method types
  • HTTP/1.0
  • GET
  • POST
  • HEAD
  • asks server to leave requested object out of
    response
  • 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

26
HTTP response message
status line (protocol status code status phrase)
HTTP/1.1 200 OK Connection close Date Thu, 06
Aug 1998 120015 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
27
HTTP response status codes
In first line in server-gtclient response
message. A few sample codes
  • 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

28
Trying out HTTP (client side) for yourself
  • 1. Telnet to your favorite Web server

Opens TCP connection to port 80 (default HTTP
server port) at www.eurecom.fr. Anything typed in
sent to port 80 at www.eurecom.fr
telnet www.eurecom.fr 80
  • 2. Type in a GET HTTP request

By typing this in (hit carriage return twice),
you send this minimal (but complete) GET request
to HTTP server
GET /ross/index.html HTTP/1.0
3. Look at response message sent by HTTP server!
29
User-server interaction authorization
  • Authorization control access to server content
  • authorization credentials typically name,
    password
  • stateless client must present authorization in
    each request
  • authorization header line in each request
  • if no authorization header, server refuses
    access, sends
  • WWW-authenticate
  • header line in response

server
client
usual http request msg
401 authorization req. WWW-authenticate
30
Cookies keeping state
  • 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 users host and managed by
    users browser
  • 4) back-end database at Web site
  • Example
  • Susan access Internet always from same PC
  • She visits a specific e-commerce site for first
    time
  • When initial HTTP requests arrives at site, site
    creates a unique ID and creates an entry in
    backend database for ID

31
Cookies keeping state (cont.)
server creates ID 1678 for user
entry in backend database
access
access
one week later
32
Conditional GET client-side caching
server
client
  • Goal dont send object if client has up-to-date
    cached version
  • client specify date of cached copy in HTTP
    request
  • If-modified-since ltdategt
  • server response contains no object if cached
    copy is up-to-date
  • HTTP/1.0 304 Not Modified

HTTP request msg If-modified-since ltdategt
object not modified
HTTP request msg If-modified-since ltdategt
object modified
HTTP response HTTP/1.0 200 OK ltdatagt
33
Chapter 2 outline
  • 2.1 Principles of app layer protocols
  • clients and servers
  • app requirements
  • 2.2 Web and HTTP
  • 2.4 Electronic Mail
  • SMTP, POP3, IMAP
  • 2.5 DNS
  • Socket programming with TCP
  • Socket programming with UDP

34
Electronic Mail
  • 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
  • outgoing, incoming messages stored on server

35
Electronic Mail mail servers
  • Mail Servers
  • mailbox contains incoming messages for user
  • message queue of outgoing (to be sent) mail
    messages
  • SMTP protocol between mail servers to send email
    messages
  • client sending mail server
  • server receiving mail server

36
Electronic Mail SMTP RFC 2821
  • uses TCP to reliably transfer email message from
    client to server, port 25
  • direct transfer sending server to receiving
    server
  • three phases of transfer
  • handshaking (greeting)
  • transfer of messages
  • closure
  • command/response interaction
  • commands ASCII text
  • response status code and phrase
  • messages must be in 7-bit ASCII

37
Scenario Alice sends message to Bob
  • 4) SMTP client sends Alices message over the TCP
    connection
  • 5) Bobs mail server places the message in Bobs
    mailbox
  • 6) Bob invokes his user agent to read message
  • 1) Alice uses UA to compose message and to
    bob_at_someschool.edu
  • 2) Alices UA sends message to her mail server
    message placed in message queue
  • 3) Client side of SMTP opens TCP connection with
    Bobs mail server

1
2
6
3
4
5
38
Sample SMTP interaction
S 220 hamburger.edu C HELO crepes.fr
S 250 Hello crepes.fr, pleased to meet
you C MAIL FROM ltalice_at_crepes.frgt
S 250 alice_at_crepes.fr... Sender ok C RCPT
TO ltbob_at_hamburger.edugt S 250
bob_at_hamburger.edu ... Recipient ok C DATA
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 C QUIT
S 221 hamburger.edu closing connection
39
Try SMTP interaction for yourself
  • telnet servername 25
  • (cs mail server mailhub.cs.iastate.edu)
  • see 220 reply from server
  • enter HELO, MAIL FROM, RCPT TO, DATA, QUIT
    commands
  • above lets you send email without using email
    client (reader)

40
SMTP final words
  • SMTP uses persistent connections
  • SMTP requires message (header body) to be in
    7-bit ASCII
  • SMTP server uses CRLF.CRLF to determine end of
    message
  • Comparison with HTTP
  • HTTP pull
  • SMTP push
  • both have ASCII command/response interaction,
    status codes
  • HTTP each object encapsulated in its own
    response msg
  • SMTP multiple objects sent in multipart msg

41
Mail message format
  • SMTP protocol for exchanging email msgs
  • RFC 822 standard for text message format
  • header lines, e.g.,
  • To
  • From
  • Subject
  • different from SMTP commands!
  • body
  • the message, ASCII characters only

header
blank line
body
42
Message format multimedia extensions
  • MIME Multipurpose Internet Mail Extensions, RFC
    2045, 2056
  • additional lines in msg header declare MIME
    content type

MIME version
method used to encode data
multimedia data type, subtype, parameter
declaration
encoded data
43
MIME typesContent-Type type/subtype parameters
  • Text
  • example subtypes plain, html
  • Image
  • example subtypes jpeg, gif
  • Audio
  • exampe subtypes basic (8-bit mu-law encoded),
    32kadpcm (32 kbps coding)
  • Video
  • example subtypes mpeg, quicktime
  • Application
  • other data that must be processed by an
    application before viewable
  • example subtypes msword, postscript

44
Multipart Type
From alice_at_crepes.fr To bob_at_hamburger.edu
Subject Picture of yummy crepe. MIME-Version
1.0 Content-Type multipart/mixed
boundaryStartOfNextPart --StartOfNextPart Dear
Bob, Please find a picture of a
crepe. --StartOfNextPart Content-Transfer-Encoding
base64 Content-Type image/jpeg base64 encoded
data ..... .........................
......base64 encoded data --StartOfNextPart Do
you want the reciple?
45
Mail access protocols
SMTP
access protocol
receivers mail server
  • SMTP delivery/storage to receivers server
  • Mail access protocol retrieval from server
  • POP3 Post Office Protocol-Version 3 RFC 1939
  • authorization (agent lt--gtserver) and download
  • IMAP Internet Mail Access Protocol RFC 1730
  • more features (more complex)
  • manipulation of stored msgs on server
  • HTTP Hotmail , Yahoo! Mail, etc.

46
POP3 protocol
S OK POP3 server ready C user bob S OK
C pass hungry S OK user successfully logged
on
  • authorization phase
  • client commands
  • user declare username
  • pass password
  • server responses
  • OK
  • -ERR
  • transaction phase, client
  • list list message numbers
  • retr retrieve message by number
  • dele delete
  • quit

C list S 1 498 S 2 912
S . C retr 1 S ltmessage 1
contentsgt S . C dele 1 C retr
2 S ltmessage 1 contentsgt S .
C dele 2 C quit S OK POP3 server
signing off
47
POP3 (more) and IMAP
  • More about POP3
  • Previous example uses download and delete mode.
  • Bob cannot re-read e-mail if he changes client
  • Download-and-keep copies of messages on
    different clients
  • IMAP (RFC 2060)
  • Keep all messages in one place the server
  • Allows user to organize messages in folders
  • IMAP keeps user state across sessions
  • names of folders and mappings between message IDs
    and folder name

48
Chapter 2 outline
  • 2.1 Principles of app layer protocols
  • clients and servers
  • app requirements
  • 2.2 Web and HTTP
  • 2.4 Electronic Mail
  • SMTP, POP3, IMAP
  • 2.5 DNS
  • Socket programming with TCP
  • Socket programming with UDP

49
DNS Domain Name System
  • People many identifiers
  • SSN, name, passport
  • Internet hosts
  • IP address (e.g. 127.7.106.83) - used by routers
  • name, e.g., popeye.cs.iastate.edu - used by
    humans
  • Q map between IP addresses and name ?
  • Domain Name System
  • distributed database implemented in hierarchy of
    many name servers
  • application-layer protocol host, name servers to
    communicate to resolve names (address/name
    translation)
  • core Internet function, implemented as
    application-layer protocol (use UDP and port 53)
  • complexity at networks edge

50
DNS name servers
  • no server has all name-to-IP address mappings
  • local name servers
  • each ISP, company has local (default) name server
  • host DNS query first goes to local name server
  • authoritative name server
  • for a host stores that hosts IP address, name
  • can perform name/address translation for that
    hosts name
  • Why not centralize DNS?
  • single point of failure
  • traffic volume
  • distant centralized database
  • maintenance
  • doesnt scale!

51
DNS Root name servers
  • contacted by local name server that can not
    resolve name
  • root name server
  • contacts authoritative name server if name
    mapping not known
  • gets mapping
  • returns mapping to local name server

13 root name servers worldwide
52
Simple DNS example
root name server
  • host surf.eurecom.fr wants IP address of
    gaia.cs.umass.edu
  • 1. contacts its local DNS server, dns.eurecom.fr
  • 2. dns.eurecom.fr contacts root name server, if
    necessary
  • 3. root name server contacts authoritative name
    server, dns.umass.edu, if necessary

2
4
3
5
authorititive name server dns.umass.edu
1
6
requesting host surf.eurecom.fr
gaia.cs.umass.edu
53
DNS example
root name server
  • Root name server
  • may not know authoritative name server
  • may know intermediate name server who to contact
    to find authoritative name server

6
2
3
7
5
4
1
8
authoritative name server dns.cs.umass.edu
requesting host surf.eurecom.fr
gaia.cs.umass.edu
54
DNS iterative queries
root name server
  • recursive query
  • puts burden of name resolution on contacted name
    server
  • heavy load?
  • iterative query
  • contacted server replies with name of server to
    contact
  • I dont know this name, but ask this server

iterative query
2
3
4
7
5
6
1
8
authoritative name server dns.cs.umass.edu
requesting host surf.eurecom.fr
gaia.cs.umass.edu
55
DNS caching
  • once (any) name server learns mapping, it caches
    mapping
  • cache entries timeout (disappear) after some time

56
DNS records
  • DNS distributed db storing resource records (RR)
  • TypeA
  • name is hostname
  • value is IP address
  • e.g. (relay1.bar.foo.com, 145.37.93.126, A)
  • TypeCNAME
  • name is alias name for some cannonical (the
    real) name
  • value is cannonical name
  • e.g. (foo.com, relay1.bar.foo.com, CNAME)
  • TypeNS
  • name is domain
  • value is hostname of authoritative name server
    for this domain
  • e.g. (foo.com, dns.foo.com, NS)
  • TypeMX
  • value is canonical name of a mail server that has
    an alias hostname name
  • e.g. (foo.com, mail.bar.foo.com, MX)

57
DNS protocol, messages
  • DNS protocol query and reply messages, both
    with same message format
  • msg header
  • identification 16 bit for query, reply to
    query uses same
  • flags
  • query (0) or reply (1)
  • reply is authoritative
  • recursion desired
  • recursion available

58
DNS protocol, messages
Name, type fields for a query
RRs in reponse to query
records for authoritative servers
additional helpful info that may be used
59
Chapter 2 outline
  • 2.1 Principles of app layer protocols
  • clients and servers
  • app requirements
  • 2.2 Web and HTTP
  • 2.4 Electronic Mail
  • SMTP, POP3, IMAP
  • 2.5 DNS
  • Socket programming with TCP
  • Socket programming with UDP

60
Socket programming
Goal learn how to build client/server
application that communicate using sockets
  • Socket API
  • explicitly created, used, released by apps
  • client/server paradigm
  • two types of transport service via socket API
  • UDP socket unreliable datagram
  • TCP socket reliable, byte stream-oriented

61
Socket-programming using TCP
  • Socket a door between application process and
    end-end-transport protocol (UCP or TCP)
  • TCP service reliable, in-order transfer of bytes
    from one process to another

controlled by application developer
controlled by application developer
controlled by operating system
controlled by operating system
internet
host or server
host or server
62
Socket programming with TCP
  • Client must contact server
  • server process must first be running
  • server must have created socket (door) that
    welcomes clients contact
  • Client contacts server by
  • creating client-local TCP socket
  • establish a connection to servers socket
  • When contacted by client, server TCP creates new
    socket for server process to communicate with
    client
  • allows server to talk with multiple clients

63
Socket Programming Using TCP Flow Diagram
client
server
socket()
socket()
bind()
listen()
TCP 3-way handshake
connect()
accept()
Data (request)
write()
read()
Data (reply)
write()
read()
close()
close()
64
The socket() System Call
  • int socket(int family, int type, int protocol)
  • address family AF_INET for Internet addresses
  • socket type
  • SOCK_STREAM supported by TCP. Connection-oriented
    and reliable. Message boundaries are not
    preserved.
  • SOCK_DGRAM supported by UDP. Connectionless and
    unreliable. Message boundaries are maintained.
  • protocol 0
  • Return value a socket descriptor that can be
    used to reference the socket in subsequent system
    calls
  • Example socksocket(AF_INET, SOCK_STREAM, 0)
    returns a TCP stream socket

65
The bind() System Call
  • int bind(int sockfd, struct sockaddr addr, int
    addrlen)
  • Bind an address to the socket
  • sockfd socket descriptor
  • struct sockaddr
  • short sa_family /address family/
  • char sa_data14 /up to 14 bytes of
    address/
  • addrlen size of addr structure
  • Return value 0 for success, -1 otherwise.

66
Internet Domain Address
  • / Internet socket address structure /
  • struct sockaddr_in
  • short sin_family /AF_INET/
  • u_short sin_port /16-bit port no. /
  • struct in_addr sin_addr/32-bit Internet
    address, network byte order/
  • char sin_zero8 /unused/
  • / Internet address /
  • struct in_addr
  • union
  • struct u_char s_b1,s_b2,s_b3,s_b4 S_un_b
  • struct u_short s_w1,s_w2 S_un_w
  • u_long S_addr
  • S_un
  • define s_addr S_un.S_addr / can be used for
    most tcp ip code /

67
The bind() System Call An Example
  • int sockfd
  • struct sockaddr_in myaddr
  • if ((sockfdsocket(AF_INET, SOCK_STREAM, 0))lt0)
  • /handle error/
  • myaddr.sin_familyAF_INET
  • myaddr.sin_porthtons(5100)
  • myaddr.sin_addr.s_addrhtonl(INADDR_ANY)
  • if (bind(sockfd, (struct sockaddr ) myaddr,
    sizeof(myaddr))lt0)
  • /handle error/
  • Note cast sockaddr_in to sockaddr in bind().

68
Listen(), Accept(), Connect()
  • int listen(int sockfd, int maxwaiting)
  • Called by server, ready to accept requests
  • Initialize a queue for incoming connection
    requests.
  • int accept(int sockfd, struct sockaddr fromaddr,
    int addrlen)
  • sockfd used for accepting incoming connection
    requests
  • Take the first pending connection request off the
    queue and create a new socket (return value) for
    communicating with client
  • Block until client connects
  • int connect(int sockfd, struct sockaddr toaddr,
    int addrlen)
  • Called by client
  • Blocking, return when accepted or error

69
Sending and Receiving Data
  • int read(int sockfd, char buffer, int n)
  • read n bytes from a socket and store them in
  • buffer.
  • int write(int sockfd, char buffer, int n)
  • write n bytes stored in buffer to a socket
  • read and write return the number of bytes
    read/written or -1 if they fail.

70
A Simple Server Using TCP Socket
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include lterrno.hgt
  • include ltstring.hgt
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • include ltnetinet/in.hgt
  • include ltarpa/inet.hgt
  • define MYPORT 3490 // the port users will
    be connecting to
  • define BACKLOG 10 // how many pending
    connections queue will hold
  • int main()
  • int sockfd, new_fd // listen on sockfd,
    new connection on new_fd
  • struct sockaddr_in my_addr // my
    address information
  • struct sockaddr_in their_addr //
    client's address information
  • int sin_size

71
A Simple Server Using TCP Socket
  • if ((sockfd socket(AF_INET,
    SOCK_STREAM, 0)) -1)
  • perror("socket")
  • exit(1)
  • my_addr.sin_family AF_INET
  • my_addr.sin_port htons(MYPORT)
  • my_addr.sin_addr.s_addr
    htonl(INADDR_ANY) // automatically fill with my
    IP
  • if (bind(sockfd, (struct sockaddr
    )my_addr, sizeof(struct sockaddr)) -1)
  • perror("bind")
  • exit(1)
  • if (listen(sockfd, BACKLOG) -1)
  • perror("listen")
  • exit(1)

72
A Simple Server Using TCP Socket
  • while(1)
  • sin_size sizeof(struct sockaddr_in)
  • if ((new_fd accept(sockfd, (struct
    sockaddr )their_addr, sin_size)) -1)
  • perror("accept")
  • continue
  • printf("server got connection from
    s\n", inet_ntoa(their_addr.sin_addr))
  • if (write(new_fd, "Hello, world!\n",
    14) lt 0)
  • perror("send")
  • exit(1)
  • close(new_fd)

73
A Simple Client Using TCP Socket
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include lterrno.hgt
  • include ltstring.hgt
  • include ltnetdb.hgt
  • include ltsys/types.hgt
  • include ltnetinet/in.hgt
  • include ltsys/socket.hgt
  • define PORT 3490 // the port client will be
    connecting to
  • define MAXDATASIZE 100 // max number of
    bytes we can get at once
  • int main(int argc, char argv)
  • int sockfd, numbytes
  • char bufMAXDATASIZE
  • struct hostent he
  • struct sockaddr_in their_addr //
    server's address information

74
A Simple Client Using TCP Socket
  • if (argc ! 2)
  • fprintf(stderr,"usage client
    hostname\n")
  • exit(1)
  • if ((sockfd socket(AF_INET,
    SOCK_STREAM, 0)) -1)
  • perror("socket")
  • exit(1)
  • if ((hegethostbyname(argv1)) NULL)
    // get the host info
  • perror("gethostbyname")
  • exit(1)
  • their_addr.sin_family AF_INET
  • their_addr.sin_port htons(PORT)
  • memcpy(their_addr.sin_addr, he-gth_addr,
    he-gth_length)

75
A Simple Client Using TCP Socket
  • if (connect(sockfd, (struct sockaddr
    )their_addr, sizeof(struct sockaddr)) -1
  • perror("connect")
  • exit(1)
  • if ((numbytesread(sockfd, buf,
    MAXDATASIZE)) lt 0)
  • perror("read")
  • exit(1)
  • printf("Received s",buf)
  • close(sockfd)

76
Chapter 2 outline
  • 2.1 Principles of app layer protocols
  • clients and servers
  • app requirements
  • 2.2 Web and HTTP
  • 2.4 Electronic Mail
  • SMTP, POP3, IMAP
  • 2.5 DNS
  • Socket programming with TCP
  • Socket programming with UDP

77
Socket programming with UDP
  • UDP no connection between client and server
  • no handshaking
  • sender explicitly specifies the address of the
    destination for each packet
  • UDP transmitted data may be received out of
    order, or lost

78
Socket Programming Using UDP Flow Diagram
server
client
socket()
socket()
bind()
Data (request)
recvfrom()
sendto()
Data (reply)
sendto()
recvfrom()
close()
close()
79
Sending via UDP Socket sendto()
  • int sendto (int sockfd, char buff, int bufflen,
    int flags, struct sockaddr toaddr, int tolen)
  • sockfd socket descriptor
  • buff a set of consecutive memory locations
    holding the message to send
  • bufflen number of bytes to send
  • flags 0
  • toaddr address of sockaddr_in structure holding
    destination address info. Must be cast to type
    sockaddr
  • tolen length of destination address
  • Return value length of data actually sent. 1 if
    error.

80
Reading from UDP Socket recvfrom()
  • int recvfrom (int sockfd, char buff, int
    bufflen, int flags, struct sockaddr fromaddr,
    int fromlen)
  • sockfd socket descriptor
  • buff a set of consecutive memory locations
    holding the message to be received
  • bufflen number of bytes to read
  • flags 0
  • fromaddr address of sockaddr_in structure
    containing address info of socket that sent the
    data. A returned value.
  • tolen size of the address structure. A returned
    value.
  • Retun value number of bytes actually received.
    1 of error.

81
A Simple Example Server Using UDP Socket
  • myaddr.sin_familyAF_INET
  • myaddr.sin_porthtons(MY_PORT_ID)
  • myaddr.sin_addr.s_addrhtonl(INADDR_ANY)
  • if (bind(sockid, (struct sockaddr ) myaddr,
    sizeof(myaddr)))lt0
  • printf(server bind fail d\n, errno)
    exit(0)
  • nreadrecvfrom(sockid,msg,11,0,(struct
    sockaddr)client_addr, addrlen))
  • If (nreadgt0) printf(server message is
    s\n,msg)
  • close(sockid)
  • include ltstdio.hgt
  • include lterrno.hgt
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • include ltarpa/inet.hgt
  • include ltnetinet/in.hgt
  • include ltnetdb.hgt
  • define MY_PORT_ID 6090
  • main (int argc, char argv )
  • int sockid, nread, addrlen
  • struct sockaddr_in myaddr, client_addr
  • char msg50
  • if ((sockidsocket(AF_INET, SOCK_DGRAM, 0))lt0)
  • printf(server socket error d\n, errno)
    exit(0)

82
A Simple Example Client Using UDP Socket
  • server_addr.sin_familyAF_INET
  • server_addr.sin_porthtons(SERVER_PORT_ID)
  • server_addr.sin_addr.s_addrinet_addr(SERV_HOST_AD
    DR)
  • sprintf(msg, hello world)
  • retcodesendto(sockid,msg,11,0,(struct
    sockaddr)server_addr, sizeof(server_addr))
  • If (retcode-1) printf(client sendto failed
    d\n,errno) exit(0)
  • close(sockid)
  • include ltstdio.hgt
  • include lterrno.hgt
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • include ltarpa/inet.hgt
  • include ltnetinet/in.hgt
  • include ltnetdb.hgt
  • define SERVER_PORT_ID 6090
  • define SERV_HOST_ADDR 129.186.3.91
  • main (int argc, char argv )
  • int sockid, retcode
  • struct sockaddr_in myaddr, server_addr
  • char msg12
  • if ((sockidsocket(AF_INET, SOCK_DGRAM, 0))lt0)
  • printf(client socket error d\n, errno)
    exit(0)

83
Chapter 2 Summary
  • Our study of network apps now complete!
  • application service requirements
  • reliability, bandwidth, delay
  • client-server paradigm
  • Internet transport service model
  • connection-oriented, reliable TCP
  • unreliable, datagrams UDP
  • specific protocols
  • HTTP
  • SMTP3, POP, IMAP
  • DNS
  • socket programming

84
Chapter 2 Summary
  • Most importantly learned about protocols
  • typical request/reply message exchange
  • client requests info or service
  • server responds with data, status code
  • message formats
  • headers fields giving info about data
  • data info being communicated
Write a Comment
User Comments (0)
About PowerShow.com