Download presentation
Presentation is loading. Please wait.
1
Some slides are in courtesy of J. Kurose and K. Ross Review of Previous Lecture Electronic Mail: SMTP, POP3, IMAP DNS Socket programming with TCP
2
Announcement Homework 1 due Wed. midnight Should have completed at least part I of project 1 Recitation tomorrow on homework 1 and project 1
3
Outline Socket programming with TCP Socket programming with UDP I/O multiplexing Web caching
4
socket() bind() listen() accept() read() write() read() close() Socket() connect() write() read() close() TCP Client TCP Server Well-known port blocks until connection from client process request Connection establishment Data(request) Data(reply) End-of-file notification
5
int connect_ socket( char *hostname, int port) { int sock; struct sockaddr_in sin; struct hostent *host; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock == -1) return sock; host = gethostbyname( hostname); if (host == NULL) { close( sock); return -1; } memset (& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ port = htons( port); sin. sin_ addr. s_ addr = *( unsigned long *) host-> h_ addr_ list[ 0]; if (connect( sock, (struct sockaddr *) &sin, sizeof( sin)) != 0) { close (sock); return -1; } return sock; } Resolve the host struct hostent *gethostbyname( const char *hostname); /*Return nonnull pointer if OK, NULL on error */ Setup up the struct unit16_t htons(unit16_t host16bitvaule) /*Change the port number from host byte order to network byte order */ Connect connect(int socketfd, const struct sockaddr * servaddr, socket_t addrlen) /*Perform the TCP three way handshaking*/ Hostent structure struct hostent{ char * h_name/*official name of host*/ char ** h_aliases; /* pointer ot array of\ pointers to alias name*/ int h_addrtype /* host address type*/ int h_length/* length of address */ char ** h_addr_list/*prt to array of ptrs with \ IPv4 or IPv6 address*/ } Ipv4 socket address structure struct socketaddr_in{ uint8_t sin_len; /*length of the structure (16)*/ sa_falimily_t sin_family /* AF_INT*/ in_port_t sin_port /* 16 bit TCP or UDP port number*/ struct in_addr sin_addr/* 32 bit Ipv4 address */ char sin_zero(8)/* unused*/ } Make the socket Socket(int family, int type, in t protocol); return nonnegative value for OK, -1 for error
6
Server – high level view Create a socket Bind the socket Listen for connections Accept new client connections Read/write to client connections Shutdown connection
7
Assigning an address to a socket The bind() system call is used to assign an address to an existing socket. int bind( int sockfd, const struct sockaddr *myaddr, int addrlen); bind returns 0 if successful or -1 on error.
8
bind() calling bind() assigns the address specified by the sockaddr structure to the socket descriptor. You can give bind() a sockaddr_in structure: bind( mysock, (struct sockaddr*) &myaddr, sizeof(myaddr) );
9
bind() Example int mysock,err; struct sockaddr_in myaddr; mysock = socket(PF_INET,SOCK_STREAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( portnum ); myaddr.sin_addr = htonl( ipaddress); err=bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr));
10
Make listen socket (TCP) int make_ listen_ socket( int port) { struct sockaddr_ in sin; int sock; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock < 0) return -1; memset(& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ addr. s_ addr = htonl( INADDR_ ANY); sin. sin_ port = htons( port); if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0) return -1; return sock; } Make the socket Setup up the struct Bind bind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen); /* return 0 if OK, -1 on error assigns a local protocol adress to a socket*/
11
listen() int listen( int sockfd, int backlog); sockfd is the TCP socket (already bound to an address) backlog is the number of incoming connections the kernel should be able to keep track of (queue for us). listen() returns -1 on error (otherwise 0).
12
Accepting an incoming connection. Once we call listen(), the O.S. will queue incoming connections –Handles the 3-way handshake –Queues up multiple connections. When our application is ready to handle a new connection, we need to ask the O.S. for the next connection.
13
accept() int accept( int sockfd, struct sockaddr* cliaddr, socklen_t *addrlen); sockfd is the passive mode TCP socket. cliaddr is a pointer to allocated space. addrlen is a value-result argument –must be set to the size of cliaddr –on return, will be set to be the number of used bytes in cliaddr.
14
accept() return value accept() returns a new socket descriptor (small positive integer) or -1 on error. After accept returns a new socket descriptor, I/O can be done using the read() and write() system calls. read() and write() operate a little differently on sockets (vs. file operation)!
15
Accepting a client connection (TCP) int get_ client_ socket( int listen_ socket) { struct sockaddr_ in sin; int sock; int sin_ len; memset(& sin, 0, sizeof( sin)); sin_ len = sizeof( sin); sock = accept( listen_ socket, (struct sockaddr *) &sin, &sin_ len); return sock; } Setup up the struct Accept the client connection accept(int sockefd, struct sockaddr * claddr, socklen_t * addrlen) /* return nonnegative descriptor if OK, -1 on error return the next completed connection from the front of the completed connection queue. if the queue is empty, the process is put to sleep(assuming blocking socket)*/
16
Reading from/writing to a TCP socket int read( int fd, char *buf, int max); int write( int fd, char *buf, int num); By default read() will block until data is available. reading from a TCP socket may return less than max bytes (whatever is available). You must be prepared to read data 1 byte at a time! write might not be able to write all num bytes (on a nonblocking socket).
17
Terminating a TCP connection When finished using a socket, the socket should be closed: status = close(s); –status : 0 if successful, -1 if error –s : the file descriptor (socket being closed) Closing a socket –closes a connection (for SOCK_STREAM ) –frees up the port used by the socket
18
socket() bind() listen() accept() read() write() read() close() Socket() connect() write() read() close() TCP Client TCP Server Well-known port blocks until connection from client process request Connection establishment Data(request) Data(reply) End-of-file notification
19
Outline Socket programming with TCP Socket programming with UDP I/O multiplexing Web caching
20
Socket programming with UDP UDP: no “connection” between client and server no handshaking sender explicitly attaches IP address and port of destination to each packet server must extract IP address, port of sender from received packet UDP: transmitted data may be received out of order, or lost application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server
21
socket() bind() recvfrom() sendto() Socket() sendto() recvfrom() close() UDP Client UDP Server Well-known port blocks until datagram received from client process request Data(request) Data(reply)
22
sendto() int sendto(int sock, const void *buf, int count, unsigned int flags, struct sockaddr *to_addr, int to_addrlen ); sock: socket descriptor previously created buf: pointer to data to be transmitted count: number of bytes to be transmitted to_addr: filled with destination address and port number to_addrlen: sizeof(to_addr) Flags is generally set to 0 Returns the number of bytes sent on success, otherwise errno is set and -1 returned Only local errors are detected, and nothing can be said about whether the data actually reaches the other side
23
recvfrom() int recvfrom(int sock, const void *buf, int count, unsigned int flags, struct sockaddr *from_addr, int *from_addrlen); sock: socket descriptor previously created buf: pointer to memory where data is to be stored count: max number of bytes to be received (i.e., size of buf). Flags is generally set to 0 from_addr: filled with source address and port number. Returns the number of bytes received on success, otherwise errno is set and -1 returned.
24
Caveats about recvfrom() If no data is available, recvfrom() will block indefinitely until a message arrives If buf is not long enough to contain the data, the message may be truncated
25
Outline Socket programming with TCP Socket programming with UDP I/O multiplexing Web caching
26
Dealing with blocking calls Many functions block –accept(), connect(), recvfrom() For simple programs this is fine What about complex connection routines –Multiple connections –Simultaneous sends and receives –Simultaneously doing non-networking processing
27
How to handle multiple connections Create multi-process or multi-threaded code –More complex, requires mutex, semaphores, etc. –Not covered I/O multiplexing using polling –Turn off blocking feature (fcntl() system call) –Very inefficient I/O multiplexing using select ()
28
I/O Multiplexing: Polling int opts = fcntl (sock, F_GETFL); if (opts < 0) { perror ("fcntl(F_GETFL)"); abort (); } opts = (opts | O_NONBLOCK); if (fcntl (sock, F_SETFL, opts) < 0) { perror ("fcntl(F_SETFL)"); abort (); } while (1) { if (receive_packets(buffer, buffer_len, &bytes_read) != 0) { break; } if (read_user(user_buffer, user_buffer_len, &user_bytes_read) != 0) { break; } get data from socket get user input first get current socket option settings then adjust settings finally store settings back
29
I/O Multiplexing: Select (1) Select() –Wait on multiple file descriptors/sockets and timeout –Return when any file descriptor is ready to be read or written, or Indicate an error, or timeout exceeded Advantages –Simple –Application does not consume CPU cycles while waiting Disadvantages –Does not scale to large number of file descriptors/sockets
30
I/O Multiplexing: Select (2) fd_set read_set; struct timeval time_out; while (1) { FD_ZERO (read_set); FD_SET (stdin, read_set); /* stdin is typically 0 */ FD_SET (sock, read_set); time_out.tv_usec = 100000; time_out.tv_sec = 0; select_retval = select(MAX(stdin, sock) + 1, &read_set, NULL, NULL, &time_out); if (select_retval < 0) { perror ("select"); abort (); } if (select_retval > 0) { if (FD_ISSET(sock, read_set)) { if (receive_packets(buffer, buffer_len, &bytes_read) != 0) { break; } if (FD_ISSET(stdin, read_set)) { if (read_user(user_buffer, user_buffer_len, &user_bytes_read) != 0) { break; } set up parameters for select() run select() interpret result
31
select function call int status = select() –Status: # of ready objects, -1 if error –nfds: 1 +largest file descriptor to check –readfds: list of descriptors to check if read-ready –writefds: list of descriptors to check if write- ready –exceptfds: list of descriptors to check if an exception is registered –Timeout: time after which select returns
32
Outline Socket programming with TCP Socket programming with UDP I/O multiplexing Web caching
33
Web caches (proxy server) user sets browser: Web accesses via cache browser sends all HTTP requests to cache –object in cache: cache returns object –else cache requests object from origin server, then returns object to client Goal: satisfy client request without involving origin server client Proxy server client HTTP request HTTP response HTTP request HTTP response origin server origin server
34
More about Web caching Cache acts as both client and server Cache can do up-to-date check using If- modified-since HTTP header Typically cache is installed by ISP (university, company, residential ISP) Why Web caching? Reduce response time for client request. Reduce traffic on an institution’s access link. Internet dense with caches enables “poor” content providers to effectively deliver content
35
Caching example (1) Assumptions average object size = 100,000 bits avg. request rate from institution’s browser to origin serves = 15/sec delay from institutional router to any origin server and back to router = 2 sec Consequences utilization on LAN = 15% utilization on access link = 100% total delay = Internet delay + access delay + LAN delay = 2 sec + minutes + milliseconds origin servers public Internet institutional network 10 Mbps LAN 1.5 Mbps access link institutional cache
36
Caching example (2) Possible solution increase bandwidth of access link to, say, 10 Mbps Consequences utilization on LAN = 15% utilization on access link = 15% Total delay = Internet delay + access delay + LAN delay = 2 sec + msecs + milliseconds often a costly upgrade origin servers public Internet institutional network 10 Mbps LAN 10 Mbps access link institutional cache
37
Caching example (3) Install cache suppose hit rate is.4 Consequence 40% requests will be satisfied almost immediately 60% requests satisfied by origin server utilization of access link reduced to 60%, resulting in negligible delays (say 10 msec) total delay = Internet delay + access delay + LAN delay =.6*2 sec +.6*.01 secs + milliseconds < 1.3 secs origin servers public Internet institutional network 10 Mbps LAN 1.5 Mbps access link institutional cache
38
Chapter 2: Summary application service requirements: – reliability, bandwidth, delay client-server paradigm Internet transport service model –connection-oriented, reliable: TCP –unreliable, datagrams: UDP Our study of network apps now complete! specific protocols: –HTTP –FTP –SMTP, POP, IMAP –DNS socket programming –TCP, UDP –I/O Multiplexing Web caching
39
Announcement Homework 1 due Wed. midnight Should have completed at least part I of project 1 Recitation tomorrow on homework 1 and project 1
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.