Top Banner
1 CSE 422 - Phillips Application Layer CSE 422 Notes, Set 2 These slides contain materials provided with the text: Computer Networking: A Top Down Approach,5 th edition, by Jim Kurose and Keith Ross, Addison-Wesley, April 2009. Additional figures are repeated, with permission, from Computer Networks, 2 nd through 4 th Editions, by A. S. Tanenbaum, Prentice Hall. The remainder of the materials were developed by Philip McKinley and modified by Dennis Phillips at Michigan State University CSE 422 - Phillips Application Layer Assignment: Read sections in Chapter 2 of Kurose-Ross. We will cover in this order: 2.1, 2.7, 2.5, 2.4, 2.2
26

CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

Jul 27, 2018

Download

Documents

trinhdang
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

1

CSE 422 - Phillips Application Layer

CSE 422 Notes, Set 2

These slides contain materials provided with the text: Computer Networking: A Top Down Approach,5th edition, by Jim Kurose and Keith Ross, Addison-Wesley, April 2009.

Additional figures are repeated, with permission, from Computer Networks, 2nd

through 4th Editions, by A. S. Tanenbaum, Prentice Hall.

The remainder of the materials were developed by Philip McKinley and modified by Dennis Phillips at Michigan State University

CSE 422 - Phillips Application Layer

Assignment:

Read sections in Chapter 2 of Kurose-Ross.

We will cover in this order:2.1, 2.7, 2.5, 2.4, 2.2

Page 2: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

2

CSE 422 - Phillips Application Layer

Outline

r Introduce Application Layerr Socket Programmingr Domain Name Service (DNS)r Standard Application-level Protocols

❖ email (SMTP)❖ HTTP

CSE 422 - Phillips Application Layer

Some network applications

e-mail

web

instant messaging

remote login

P2P file sharing

multi-user network games

streaming stored video clips

social networks

voice over IP

real-time video conferencing

grid computing

Page 3: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

3

CSE 422 - Phillips Application Layer

Creating a network app

We write programs that❖ run on (different) hosts

❖ communicate over network

❖ e.g., browser software communicates with web server software

We do not need to (and are not able to) write software for network-core devices❖ Network-core devices do not run

user code

❖ Limiting applications on end systems facilitates rapid app development, distribution

applicationtransportnetworkdata linkphysical

applicationtransportnetworkdata linkphysical

applicationtransportnetworkdata linkphysical

CSE 422 - Phillips Application Layer

Client-server architectureMost common paradigm

server:

❖ always-on host

❖ permanent IP address

❖ server farms for scaling

clients:❖ communicate with server

❖ may be intermittently connected

❖ may have dynamic IP addresses

❖ Usually do not communicate directly with each other

client/server

Page 4: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

4

CSE 422 - Phillips Application Layer

Application-layer protocol defines: Types of messages

exchanged, ❖ e.g., request, response

Message syntax:❖ what fields in messages &

how fields are delineated

Message semantics ❖ meaning of information in

fields

Rules for when and how processes send & respond to messages

Public-domain protocols:

defined in RFCs

allows for interoperability

e.g., HTTP, SMTP, BitTorrent

CSE 422 - Phillips Application Layer

Outline

Introduce Application LayerSocket ProgrammingDomain Name Service (DNS)Standard Application-level Protocols

❖ email (SMTP)❖ HTTP

Page 5: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

5

CSE 422 - Phillips Application Layer

Application Programming Interfaces

The TCP/IP protocol suite provides only the protocols that can be used by processes to communicate across a network.

Though standarized, how these protocols are implemented is system specific

Transport protocols and IP are typically implemented in the operating system.

Hence, an API is needed to provide user processes with access to these protocols

CSE 422 - Phillips Application Layer

“Low-level” Interfaces

Sockets -developed at Berkeley by Bill Joy and others (1981). ❖ Incorporated into BSD Unix and all derivatives,

also supported in Windows, Mac OS X, etc.

❖ Key concept: make network access look much like file access.

Technology-specific APIs ❖ E.g., ATM LANs, special LANs for compute

clusters, and so on.

Page 6: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

6

CSE 422 - Phillips Application Layer

“High-Level” APIs

Remote Procedure Call (RPC) ❖ “message passing” appears as “procedure

calling”

❖ client accesses remote server through procedure call interface.

❖ typically implemented as a library atop sockets

Object-oriented network programming ❖ Java.net class library

❖ OO middleware platforms such as CORBA

❖ Java Remote Method Invocation (RMI)

CSE 422 - Phillips Application Layer

Introduction to Sockets

A socket is a kernel data structure, accessed via a file descriptor, that defines a bi-directional endpoint of communication

Sockets are used as a basic building block for interprocess communication

The kernel data structure ❖ contains state information regarding the socket

❖ has associated lists of send and receive buffers

❖ accesses transport functions directly

Page 7: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

7

CSE 422 - Phillips Application Layer

Socket programming

goal: learn how to build client/server applications that communicate using sockets

socket: door between application process and end-end-transport protocol

Application Layer 2-14

Internet

controlled

by OS

controlled byapp developer

transport

application

physical

link

network

process

transport

application

physical

link

network

processsocket

CSE 422 - Phillips Application Layer

socket() system call

A socket is created using the socket() call, which takes as parameters: ❖ protocol family

❖ type of communication

❖ specific protocol (often implicit)

socket() returns a file descriptor

A socket is created without a name❖ a socket “name” is a triple:

• class (a.k.a. family), IP address, port number

❖ a name can be affixed to the socket after it is created by invoking the bind() system call

Page 8: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

8

CSE 422 - Phillips Application Layer

Protocol Families

Internet Domain (AF_INET)❖ interprocess communication among different

hosts

❖ socket name includes internet address and port

❖ some port numbers are reserved for system use (see /etc/services)

Unix Domain (AF_UNIX, aka AF_LOCAL)❖ UNIX system internal protocols

❖ interprocess communication within same host

❖ socket name is a file pathname

CSE 422 - Phillips Application Layer

INET Socket types datagram socket (SOCK_DGRAM)

❖ unreliable datagram communication

❖ record boundary (fixed maximum) is preserved

❖ implemented using the UDP protocol

stream socket (SOCK_STREAM) ❖ reliable virtual circuit communication

❖ “record” boundary is not preserved

❖ implemented using the TCP protocol

raw socket (SOCK_RAW) ❖ direct interface to the IP protocol

❖ super-user (administrator) only, for new protocol development

Page 9: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

9

CSE 422 - Phillips Application Layer

INET socket names

Name Components❖ Protocol family (AF_INET)

❖ IP address

❖ Port number (can be chosen by OS)

Socket must have a name in order to receive data

Naming is done via bind() system call or implicit bind, discussed later.

CSE 422 - Phillips Application Layer

Socket programming basics

Initial receiver (which we will call the server) must be running before the initial sender (the client) can (successfully) send anything to it.

Server must have a socket through which it receives and sends segments

Similarly, the client needs a socket

The server socket is locally identified with (bound to) a port number❖ Analogous to the mailbox number

Client needs to know the server’s IP address and port number.

Page 10: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

10

CSE 422 - Phillips Application Layer

Socket programming with UDPUDP: no “connection” between client and server

sending application specifies the IP address and port of the destination

the OS places these in the header of each outgoing datagram

OS also places IP address and port of the sending socket in the header of each datagram

Server can extract IP address, port of sender from received datagram

Note: the official terminology for a UDP packet is “datagram.” Some texts refer to “UDP segments,” but the term segment usually applies to parts of TCP data streams

CSE 422 - Phillips Application Layer

A few notes about port numbersA port number is just a (16-bit) number; it

has nothing to do with instances of physical hardware or even data structures

Port numbers 0 to 1023 are “well-known” and reserved for standardized applications; they are not available to regular users❖ e.g., port 80?

When invoking bind(), an application program can specify the port number to be bound to the socket. If already in use?

A parameter of 0 means: let OS choose an available port number.

Page 11: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

11

CSE 422 - Phillips Application Layer

Well Knows Ports

IANA is the registry for Network Services❖ https://www.iana.org/assignments/service-

names-port-numbers/service-names-port-numbers.xhtml

FTP – TCP 20/21SSH – TCP 22SMTP – TCP 25DNS – UDP/TCP 53DHCP – UDP 67/68HTTP – TCP 80

POP – TCP 110NTP – TCP 123IMAP – TCP 143LDAP – TCP 389HTTPS – TCP 443

CSE 422 - Phillips Application Layer

Example: UDP receiver

Page 12: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

12

CSE 422 - Phillips Application Layer

UDP receiver (cont.)

CSE 422 - Phillips Application Layer

UDP receiver (cont.)

Page 13: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

13

CSE 422 - Phillips Application Layer

UDP receiver (cont.)

CSE 422 - Phillips Application Layer

Example UDP sender

Page 14: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

14

CSE 422 - Phillips Application Layer

UDP sender (cont.)

CSE 422 - Phillips Application Layer

UDP observations & questions

Both sender and receiver use datagram sockets

Dest IP and port are explicitly included with the outgoing datagram.

Question: how did the receiver (server) know the port number of the sender (client), if the client’s socket was never bound?

Can multiple clients send to the same server program at the same port?

Page 15: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

15

CSE 422 - Phillips Application Layer

Socket programming with TCP

Client (active side) must connect to server (passive side)

server process must first be already running

server must have created a socket that receives the client’s connection request

Client contacts server by:

creating a local TCP socket

issuing a connect() system call, specifying ❖ IP address of server socket

❖ port number of server socket

Server actions:

create and bind a name to the receiving socket

Await connection request

Upon receipt, TCP creates new socket for the server process to communicate with this client

❖ allows server to talk with multiple clients on different sockets

❖ all have same server port number

❖ more on this later…

CSE 422 - Phillips Application Layer

Stream socket – Passive side

Await (listen for) connections

Use listen() system call to prepareoperating system for connection requests

Parameters: ❖ socket descriptor

❖ backlog: defines the maximum length the queue of pending connections (usually 5)

Page 16: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

16

CSE 422 - Phillips Application Layer

Passive side (cont.)Use the accept() system call

❖ socket descriptor

❖ name (family, host addr, port) ?

❖ length of name

Extracts the first connection on the queue of pending connections, creates a new socket, and allocates/returns a new file descriptor for the socket.

Normally blocks, but this can be turned off

Returns file descriptor of a new socket.

CSE 422 - Phillips Application Layer

Stream socket – active side

Initiate a connection on a socket

Use the connect() system call ❖ socket descriptor

❖ name (family, host addr, port)

❖ length of name

Connect on a datagram socket (??)

Errors ❖ ETIMEOUT

❖ ECONNREFUSED

❖ ENETDOWN or EHOSTDOWN

❖ ENETUNREACH or EHOSTREACH

Page 17: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

17

CSE 422 - Phillips Application Layer

Transferring data

Transmitting ❖ write() as for files

❖ send() uses flags to specify such requests as out-of-band transmission (MSG OOB)

Receiving ❖ read() as for files

❖ recv() uses flags to specify such requests as examining data without reading it (MSG PEEK)

CSE 422 - Phillips Application Layer

TCP Example (passive side)

Page 18: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

18

CSE 422 - Phillips Application Layer

TCP Example (passive side)

CSE 422 - Phillips Application Layer

TCP Example (active side)

Page 19: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

19

CSE 422 - Phillips Application Layer

TCP active side (cont.)

CSE 422 - Phillips Application Layer

Server example (passive side)

Page 20: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

20

CSE 422 - Phillips Application Layer

Server passive side (cont.)

CSE 422 - Phillips Application Layer

Server example (active side)

Page 21: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

21

CSE 422 - Phillips Application Layer

Server active side (cont.)

CSE 422 - Phillips Application Layer

TCP/socket summary and a question

Active and passive sides use stream sockets

Passive side does bind(), listen() and accept()

Active side must connect() to passive side

Once connected, either side can read and write.

When the passive side accepts a connection, it creates a new socket, but with the same port number as the original socket.

Question: So, if packets are arriving from different clients, but all with the same destination port number, how does the OS know which socket which socket each belongs to?

Page 22: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

22

CSE 422 - Phillips Application Layer

Socket programming

Two socket types for two transport services:

❖ UDP: unreliable datagram

❖ TCP: reliable, byte stream-oriented

Application Layer

Application Example:1. client reads a line of characters (data) from its

keyboard and sends data to server2. server receives the data and converts characters

to uppercase3. server sends modified data to client4. client receives modified data and displays line on

its screen

CSE 422 - Phillips Application Layer

Socket programming with UDP

UDP: no “connection” between client & server no handshaking before sending data sender explicitly attaches IP destination address and

port # to each packet receiver extracts sender IP address and port# from

received packet

UDP: transmitted data may be lost or received out-of-order

Application viewpoint: UDP provides unreliable transfer of groups of bytes

(“datagrams”) between client and server

Application Layer

Page 23: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

23

CSE 422 - Phillips Application Layer

Client/server socket interaction: UDP

close

clientSocket

read datagram from

clientSocket

create socket:

clientSocket =socket(AF_INET,SOCK_DGRAM)

Create datagram with server IP and

port=x; send datagram via

clientSocket

create socket, port= x:

serverSocket =socket(AF_INET,SOCK_DGRAM)

read datagram from

serverSocket

write reply to

serverSocket

specifying

client address,

port number

server (running on serverIP) client

CSE 422 - Phillips Application LayerApplication Layer

Example app: UDP client

from socket import *

serverName = ‘hostname’

serverPort = 12000

clientSocket = socket(AF_INET,

SOCK_DGRAM)

message = raw_input(’Input lowercase sentence:’)

clientSocket.sendto(message.encode(),

(serverName, serverPort))

modifiedMessage, serverAddress =

clientSocket.recvfrom(2048)

print modifiedMessage.decode()

clientSocket.close()

Python UDPClientinclude Python’s socket library

create UDP socket for

server

get user keyboardinput

Attach server name, port to

message; send into socket

print out received string

and close socket

read reply characters from

socket into string

Page 24: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

24

CSE 422 - Phillips Application LayerApplication Layer

Example app: UDP server

from socket import *

serverPort = 12000

serverSocket = socket(AF_INET, SOCK_DGRAM)

serverSocket.bind(('', serverPort))

print (“The server is ready to receive”)

while True:

message, clientAddress = serverSocket.recvfrom(2048)

modifiedMessage = message.decode().upper()

serverSocket.sendto(modifiedMessage.encode(),

clientAddress)

Python UDPServer

create UDP socket

bind socket to local port

number 12000

loop forever

Read from UDP socket into message, getting client’s address (client IP and port)

send upper case string

back to this client

CSE 422 - Phillips Application Layer

Python Socket programming with TCP

client must contact server server process must first

be running server must have created

socket (door) that welcomes client’s contact

client contacts server by: Creating TCP socket,

specifying IP address, port number of server process

when client creates socket:client TCP establishes connection to server TCP

when contacted by client, server TCP creates new socket for server process to communicate with that particular client❖ allows server to talk with

multiple clients❖ source port numbers

used to distinguish clients (more in Chap 3)

Application Layer

TCP provides reliable, in-orderbyte-stream transfer (“pipe”) between client and server

application viewpoint:

Page 25: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

25

CSE 422 - Phillips Application Layer

Python Client/server socket interaction: TCP

Application Layer 2-50

wait for incoming

connection requestconnectionSocket =

serverSocket.accept()

create socket,port=x, for incoming

request:serverSocket = socket()

create socket,connect to hostid, port=x

clientSocket = socket()

server (running on hostid) client

send request using

clientSocketread request from

connectionSocket

write reply to

connectionSocket

TCP connection setup

close

connectionSocket

read reply from

clientSocket

close

clientSocket

CSE 422 - Phillips Application LayerApplication Layer 2-51

Python Example app: TCP client

from socket import *

serverName = ’servername’

serverPort = 12000

clientSocket = socket(AF_INET, SOCK_STREAM)

clientSocket.connect((serverName,serverPort))

sentence = raw_input(‘Input lowercase sentence:’)

clientSocket.send(sentence.encode())

modifiedSentence = clientSocket.recv(1024)

print (‘From Server:’, modifiedSentence.decode())

clientSocket.close()

Python TCPClient

create TCP socket for

server, remote port 12000

No need to attach server

name, port

Page 26: CSE 422 Notes, Set 2dennisp/cse422/Slides/set2-1.pdf · “message passing”appears as “procedure calling ... A socket is a kernel data structure, accessed via a file descriptor,

26

CSE 422 - Phillips Application LayerApplication Layer 2-52

Python Example app: TCP server

from socket import *

serverPort = 12000

serverSocket = socket(AF_INET,SOCK_STREAM)

serverSocket.bind((‘’,serverPort))

serverSocket.listen(1)

print ‘The server is ready to receive’

while True:

connectionSocket, addr = serverSocket.accept()

sentence = connectionSocket.recv(1024).decode()

capitalizedSentence = sentence.upper()

connectionSocket.send(capitalizedSentence.

encode())

connectionSocket.close()

Python TCPServer

create TCP welcoming

socket

server begins listening for

incoming TCP requests

loop forever

server waits on accept()for incoming requests, new socket created on return

read bytes from socket (but

not address as in UDP)

close connection to this

client (but not welcoming

socket)