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.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
EEC-484/584 Computer Networks Discussion Session for HTTP and DNS Wenbing Zhao
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Introduction to Project 1 Web Client and Server Jan 2006.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Lecture 8 UDP Sockets & I/O Multiplexing
IP Multiplexing Ying Zhang EECS 489 W07.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Chapter 2 Applications and Layered Architectures Sockets.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 8 Omar Meqdadi Department of Computer Science and Software Engineering University of.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Lecture 15 Overview.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
EECS340 Recitation 1: Very helpful to your project Hongyu Gao 1.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
@Yuan Xue CS 283Computer Networks Spring 2013 Instructor: Yuan Xue.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Chapter4 Elementary TCP Socket
Socket Programming in C
Transport layer API: Socket Programming
UDP Sockets Programming
Lecture 11 Overview.
TCP Sockets Programming
EE 122: Sockets Kevin Lai September 11, 2002.
Socket Programming.
TCP/IP Socket Programming in C
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Presentation transcript:

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

Announcement Homework 1 due Wed. midnight Should have completed at least part I of project 1 Recitation tomorrow on homework 1 and project 1

Outline Socket programming with TCP Socket programming with UDP I/O multiplexing Web caching

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

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

Server – high level view Create a socket Bind the socket Listen for connections Accept new client connections Read/write to client connections Shutdown connection

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.

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) );

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));

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*/

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).

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.

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.

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)!

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)*/

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).

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

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

Outline Socket programming with TCP Socket programming with UDP I/O multiplexing Web caching

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

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)

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

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.

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

Outline Socket programming with TCP Socket programming with UDP I/O multiplexing Web caching

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

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 ()

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

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

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 = ; 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

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

Outline Socket programming with TCP Socket programming with UDP I/O multiplexing Web caching

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

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

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

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

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

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

Announcement Homework 1 due Wed. midnight Should have completed at least part I of project 1 Recitation tomorrow on homework 1 and project 1