<<

Computer Networks Assignments LECTURE 3 • Lab 2 – socket programming Socket Programming and – DUE: Wednesday September 14th Application Layer Protocols

Sandhya Dwarkadas Department of Computer Science University of Rochester

Computer Networks: Principles Why layering? and Architecture dealing with complex systems: • Network Architecture: • explicit structure allows identification, relationship of – Design and Implementation Guide complex system’s pieces Principle of Abstraction – layering of protocols – layered reference model for discussion • modularization eases maintenance, updating of system A protocol provides a communication service to – change of implementation of layer’s service transparent to rest of system the next higher-level layer – e.g., change in gate procedure doesn’t affect rest of system • Service interface – e.g., send and receive • layering considered harmful? • Peer interface – form and meaning of messages exchanged between peers Introduction 1-4

1 Internet protocol stack Socket programming • application: supporting network applications goal: learn how to build client/server – FTP, SMTP, HTTP application applications that communicate using sockets • transport: -process data transfer transport socket: door between application process and – TCP, UDP end-end-transport protocol application • network: routing of datagrams network application socket controlled by from source to destination process process app developer – IP, routing protocols link transport transport network network controlled • link: data transfer between by OS link Internet link neighboring network elements physical physical physical – Ethernet, 802.111 (WiFi), PPP • physical: bits “on the wire”

Introduction 1-5 Application Layer 2-6

Operating Systems Support: Socket Addresses Sockets

#include • A socket is an operating system abstraction #include in which a port is embedded #include • A port is a communication endpoint struct sockaddr{ u_short sa_family; /*address family: AF_xxx value*/ char sa_data[14];/* up to 14 bytes of addr */ Host, port Host, port }; /* (protocol-specific) */ struct sockaddr_in { short sin_family; /* AF_INET */ u_short sin_port; /* 16-bit port number */ Process socket Process struct in_addr sin_addr; /* 32-bit netid/hostid */ socket char sin_zero[8];/* unused */ }; /*sin_port and sin_addr are network byte ordered*/

2 Network Byte Order Socket • Two ways to map byte addresses onto words: – Little Endian byte ordering: Intel 80x86, DEC VAX • Creates an endpoint for communication

socket(int family, int type, int protocol); – Big Endian byte ordering: IBM 360/370, Motorola 68K, MIPS, SPARC, HPPA – Family (or domain): AF_UNIX, AF_INET, AF_NS, AF_IMPLINK

– type: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, ... • Network byte order (TCP/IP, XNS, SNA – protocol: typically 0 protocols): big-endian in protocol headers

• Byte ordering/alignment routines: htonl, htons, – Returns a socket descriptor, or sockfd ntohl, ntohs

Services Provided by the Transport Layer • Methods by which socket options may be • Connection-oriented (virtual circuit) versus changed connection-less (datagram) protocols – setsockopt • Sequencing – fcntl • Error control – ioctl • Flow control • Byte stream vs. messages • Full duplex vs. half duplex

3 • Connection-oriented (virtual circuit) – Establish a connection – Transfer data – Terminate connection • Connection-less (datagram) – Each message or datagram transmitted independently – must contain all information for delivery

Socket programming

Two socket types for two transport services: • Operations: – UDP: unreliable datagram – fd = socket(): creates the socket (think OS – TCP: reliable, byte stream-oriented metadata to manage communication endpoint) Application Example: – bind(fd, port): binds socket to local port 1. client reads a line of characters (data) from its (address) keyboard and sends data to server 2. server receives the data and converts characters – sendto(), recvfrom(), (), (): operations to uppercase for sending and receiving data 3. server sends modified data to client – (fd): close the connection 4. client receives modified data and displays line on its screen

Application Layer 2-15

4 Socket programming with UDP Datagram Protocol: 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 2-17

Client/server socket interaction: UDP Example app: UDP client

Python UDPClient on serverIP server (running ) client include Python’s socket from socket import * create socket: serverName = ‘hostname’ create socket, port= x: clientSocket = serverPort = 12000 serverSocket = socket(AF_INET,SOCK_DGRAM) socket(AF_INET,SOCK_DGRAM) clientSocket = socket(AF_INET, create UDP socket for Create datagram with server IP and server SOCK_DGRAM) port=x; send datagram via get user keyboard message = raw_input(’Input lowercase sentence:’) read datagram from clientSocket input clientSocket.sendto(message.encode(), serverSocket Attach server name, port to (serverName, serverPort)) message; send into socket write reply to modifiedMessage, serverAddress = serverSocket read datagram from read reply characters from clientSocket.recvfrom(2048) specifying clientSocket socket into string print modifiedMessage.decode() client address, port number close clientSocket.close() print out received string clientSocket and close socket Application Layer 2-20 Application 2-19

5 Example app: UDP server Socket programming with TCP

Python UDPServer client must contact server • when contacted by client, from socket import * • server process must first be server TCP creates new serverPort = 12000 running socket for server process to serverSocket = socket(AF_INET, SOCK_DGRAM) create UDP socket • server must have created communicate with that serverSocket.bind(('', serverPort)) bind socket to local port socket (door) that welcomes particular client number 12000 print (“The server is ready to receive”) client’s contact – allows server to talk with while True: multiple clients loop forever message, clientAddress = serverSocket.recvfrom(2048) client contacts server by: – source port numbers used Read from UDP socket into modifiedMessage = message.decode().upper() • Creating TCP socket, to distinguish clients message, getting client’s serverSocket.sendto(modifiedMessage.encode(), address (client IP and port) specifying IP address, port clientAddress) application(more inviewpoint: Chap 3) send upper case string number of server process TCP provides reliable, in-order back to this client • when client creates socket: byte-stream transfer (“pipe”) client TCP establishes between client and server connection to server TCP Application Layer 2-21 Application Layer 2-22

Socket System Calls for Connection-Oriented System Connection-Oriented Protocol: TCP Calls • Connect: Establishes a connection with a server – includes bind - assigns 4 elements of the 5-tuple • Listen: Indicates that server is willing to accept connections (allows queueing) • Accept: Waits for actual connection from client process – creates a new socket descriptor – assumes a concurrent server

6 Client/server socket interaction: TCP Example app: TCP client

server (running on hostid) client Python TCPClient

create socket, from socket import * port=x, for incoming serverName = ’servername’ request: serverSocket = socket() serverPort = 12000 create TCP socket for server, remote port 12000 clientSocket = socket(AF_INET, SOCK_STREAM) for incoming TCP create socket, connection request connect to hostid, port=x clientSocket.connect((serverName,serverPort)) connection setup connectionSocket = clientSocket = socket() sentence = raw_input(‘Input lowercase sentence:’) serverSocket.accept() clientSocket.send(sentence.encode()) send request using No need to attach server read request from clientSocket modifiedSentence = clientSocket.recv(1024) name, port connectionSocket print (‘From Server:’, modifiedSentence.decode()) write reply to clientSocket.close() connectionSocket read reply from clientSocket close connectionSocket close clientSocket

Application Layer 2-25 Application Layer 2-26

Example app: TCP server Python TCPServer Client-Server Model from socket import * serverPort = 12000 create TCP welcoming socket serverSocket = socket(AF_INET,SOCK_STREAM) • Iterative versus concurrent servers serverSocket.bind((‘’,serverPort)) server begins listening for serverSocket.listen(1) • Role of client and server asymmetric incoming TCP requests print ‘The server is ready to receive’ while True: loop forever connectionSocket, addr = serverSocket.accept() server waits on accept() for incoming requests, new sentence = connectionSocket.recv(1024).decode() socket created on return capitalizedSentence = sentence.upper()

read bytes from socket (but connectionSocket.send(capitalizedSentence. not address as in UDP) encode()) close connection to this connectionSocket.close() client (but not welcoming socket) Application Layer 2-27

7 I/O Multiplexing Select System Call

• Methods by which I/O may be multiplexed • Waits on several socket descriptors – Set socket to non-blocking and poll • int select(int nfds, fd_set *readfds, fd_set • fcntl(fd, F_SETFL, FNDELAY), where FNDELAY *writefds, fd_set *exceptfds, struct timeval implies non-blocking I/O) non-blocking I/O *timeout) – Fork a process/ per socket • FD_SET(fd, &fdset) – SIGIO - asynchronous I/O with – Select - wait or poll for multiple events • FD_CLR(fd, &fdset) • FD_ISSET(fd, &fdset) • FD_ZERO(&fdset)

select() Example

8 Signals Signal Concepts (cont) • A is a small message that notifies a process that an of some type has occurred in the system. – Kernel abstraction for exceptions and interrupts. • Receiving a signal – Sent from the kernel (sometimes at the request of another process) to a process. – A destination process receives a signal when it is forced – Different signals are identified by small integer ID’s by the kernel to react in some way to the delivery of the – The only information in a signal is its ID and the fact that it arrived. signal. – Three possible ways to react: ID Name Default Action Corresponding Event • Ignore the signal (do nothing) 2 SIGINT Terminate from keyboard (ctl-c) • Terminate the process. 9 SIGKILL Terminate program (cannot override or ignore) • Catch the signal by executing a user-level function called a 11 SIGSEGV Terminate & Dump Segmentation violation signal handler. 14 SIGALRM Terminate Timer signal – Akin to a hardware exception handler being called in response to an asynchronous interrupt. 17 SIGCHLD Ignore Child stopped or terminated

• A signal isSignal pending if itConcepts has been sent but (cont) not yet received. Signal Concepts – There can be at most one pending signal of any particular type. • Kernel maintains pending and blocked bit vectors in – Important: Signals are not queued the context of each process. • If a process has a pending signal of type k, then subsequent signals of – pending – represents the set of pending signals type k that are sent to that process are discarded. • Kernel sets bit k in pending whenever a signal of type k is • A process can block the receipt of certain signals. delivered. • Kernel clears bit k in pending whenever a signal of type k is – Blocked signals can be delivered, but will not be received received until the signal is unblocked. – blocked – represents the set of blocked signals • A pending signal is received at most once. • Can be set and cleared by the application using the sigprocmask function.

9 Default Actions Receiving Signals

• Suppose kernel is returning from exception handler and is ready to • Each signal type has a predefined default pass control to process p. action, which is one of: • Kernel computes pnb = pending & ~blocked – The process terminates – The set of pending nonblocked signals for process p • If (pnb == 0) – The process terminates and dumps core. – Pass control to next instruction in the logical flow for p. – The process stops until restarted by a • Else SIGCONT signal. – Choose least nonzero bit k in pnb and force process p to receive signal k. – The process ignores the signal. – The receipt of the signal triggers some action by p – Repeat for all nonzero k in pnb. – Pass control to next instruction in logical flow for p.

Installing Signal Handlers • The signal function modifies the default action associated with the receipt of signal signum: – handler_t *signal(int signum, handler_t *handler) • Different values for handler: – SIG_IGN: ignore signals of type signum – SIG_DFL: revert to the default action on receipt of signals of type signum. – Otherwise, handler is the address of a signal handler • Called when process receives signal of type signum • Referred to as “installing” the handler. • Executing the handler is called “catching” or “handling” the signal. • When the handler executes its return statement, control passes back to instruction in the of the process that was interrupted by receipt of the signal.

10 11 HTTP Overview

• Hypertext Transfer Protocol (HTTP). – Deliver resources on the World Wide Web – HTML files, image files, query results etc – HTTP request format: scheme://host:port/path – Client/Server architecture • client: browser, server: web server – usually implemented over TCP/IP – stateless protocol – default port 80

WWW Architecture HTTP Message Format • Requests and responses are Web Browser Web Server request – similar

response – English-oriented readable text

Header1: value1 Web Browser Web Proxy Web Server request fwd req Header2: value2 S C Header3: value3 fwd resp response // blank line (CRLF by itself)

12 Initial Request Line Initial Response Line (Status line)

• Format: method path protocol • Format: protocol status_code message • Methods: GET POST HEAD • Protocol HTTP/1.0 or HTTP/1.1 • Protocol HTTP/1.0 or HTTP/1.1 • status code and message • Example – 200 OK – 201 Created GET /path/to/file/index.html HTTP/1.0 – 301 Moved Permanently – 302 Moved Temporarily – 400 Bad Request – 404 Not Found – 501 Not Implemented – 503 Service Unavailable

Header Important Headers • Content-Type: the MIME-type of the data • Provide information about the request or in the body, e.g. text/html or image/gif. response • Content-Length: the number of bytes in the • Format: body. Header-Name: value • Others • Header name is not case-sensitive • Any number of spaces or tabs may be between “:” • Client to server •Server to client and the value •From •Server •User-Agent •Last-Modified

13 Request and Response Example POST Method Steps to access http://www.somehost.com/path/file.htm HTTP/1.0 200 OK • Submit data to servers Date: Fri, 31 Dec 1999 23:59:59 GMT POST /path/script.cgi HTTP/1.0 •The client opens a TCP connection to Content-Type: text/html • Content-Type: From: [email protected] www.somehost.com:80 User-Agent: HTTPTool/1.0 •The client sends request Content-Length: 1354 application/x-www- Content-Type: application/x-www- •The server sends response form-urlencoded form-urlencoded •The browser display file.htm Content-Length: 32 • Content is URL-

Happy New Millennium!

encoded home=Cosby&favorite+flavor=flies GET /path/file.html HTTP/1.0 (more file contents) From: [email protected] ... User-Agent: Netscape/4.7 [blank line here]

Live example

Proxy Specialty Socket Programming Steps server client • Requests sent to proxies usually use socket gethostbyname complete URL, instead of just path bind • Example socket GET http://host/path/file.html HTTP/1.0 select connect

• Well-behaved proxy should strip accept “http://host” before forwarding.

Example: http://www.ecst.csuchico.edu/~beej/guide/net/html/clientserver.html

14 Server handle multiple requests Disclaimer

• Multi-process • Parts of the lecture slides are adapted from – fork, wait, wait_pid and copyrighted by James Kurose and Keith • select---Synchronous I/O Multiplexing (example) Ross. The slides are intended for the sole purpose of instruction of computer networks • Multi-thread: pthread_attr_init(&attr); at the University of Rochester. All pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); copyrighted materials belong to their pthread_create(&id, &attr, svc_routine, param); original owner(s). • Asynchronous I/O – sigaction and SIGIO

15