Lecture 14 Sockets I Outline • • • • • Sockets Socket Domain and Type Socket System Calls Stream Socket Operation Datagram Socket Operation 1 July 24, 2016 Sockets • Method of bidirectional interprocess communication (IPC) • Can be on same host (computer) • Or different hosts connected by network 2 July 24, 2016 Sockets in Client-Server Applications • Each application creates a socket • Socket is the apparatus that allows communication • Both applications require a socket • Server binds socket to well-known address • Allows client to locate server 3 July 24, 2016 Socket Creation • Socket created with socket() system call fd = socket( domain, type, protocol) • Returns a file descriptor referring to socket • In this class and in book, protocol always set to 0 4 July 24, 2016 Outline • • • • • Sockets Socket Domain and Type Socket System Calls Stream Socket Operation Datagram Socket Operation 5 July 24, 2016 Socket Domain fd = socket( domain, type, protocol) • Sockets exist in a communication domain • Domain determines: • Method of identification (i.e., format of socket’s “address”) • Range of communications (same vs. different hosts) • Linux supports 3 different domains: • AF_UNIX, AF_INET, and AF_INET6 6 July 24, 2016 Socket Domain • Linux supports 3 different domains: 1. UNIX domain (AF_UNIX): allows communication between applications on same host Address = pathname 7 July 24, 2016 Socket Domain • Linux supports 3 different domains: 2. IPv4 domain (AF_INET): allows communication between application on hosts connected with Internet Protocol v4 Address = 32-bit IPv4 address + 16-port number 8 July 24, 2016 Socket Domain • Linux supports 3 different domains: 3. IPv6 domain (AF_INET6): allows communication between application on hosts connected with Internet Protocol v6 Address= 128-bit IPv6 address + 16-bit port number 9 July 24, 2016 Socket Type fd = socket( domain, type, protocol) • Two types of sockets 1. Stream 2. Datagram 10 July 24, 2016 Stream Sockets • Reliable, bidirectional, byte-stream communication channel • Similar to having two pipes for IPC • Connection oriented socket • Operates in connected pairs • Can only be connected to one “peer” at a time 11 July 24, 2016 Datagram Sockets • Allows data to be exchanged in “datagram” messages • Message boundaries are preserved • Data transmission is NOT reliable • Messages may arrive out of order • Message may get duplicated • Messages may not arrive at all • Connectionless socket • Does NOT need to be connected to another socket 12 July 24, 2016 Internet Domain • Stream sockets employ TCP (transmission control protocol) • Datagram sockets employ UDP (user datagram protocol) 13 July 24, 2016 Outline • • • • • Sockets Socket Domain and Type Socket System Calls Stream Socket Operation Datagram Socket Operation 14 July 24, 2016 Socket System Calls • • • • • socket() – creates a new socket bind() – binds socket to address listen() – allows stream socket to accept incoming connections accept() – accepts connection from peer on listening stream connect() – establishes connection with other socket • Socket I/O – uses read(), write(), and socket-specific calls: send() recv(), etc. 15 July 24, 2016 socket() int socket( int domain, int type, int protocol ); • • • • • • • #include <sys/socket.h> Returns file descriptor of socket on success Returns -1 on error Creates a new socket domain : domain of socket ( AF_UNIX, AF_INET, or AF_INET6 ) type : type of socket ( SOCK_STREAM or SOCK_DGRAM ) protocol : set to 0 16 July 24, 2016 bind() int bind( int socketfd, const struct sockaddr *addr, socklen_t addrlen ); • • • • • • • #include <sys/socket.h> Returns 0 on success Returns -1 on error Binds a socket to an address sockfd : file descriptor of socket obtained from socket() addr : pointer to structure specifying address to bind addrlen : size of structure to be passed 17 July 24, 2016 struct sockaddr • Generic socket address structure • Size depends on domain • Typical contents: sa_family_t char 18 July 24, 2016 sa_family; //address family (AF_*) sa_data[14]; //socket address Outline • • • • • Sockets Socket Domain and Type Socket System Calls Stream Socket Operation Datagram Socket Operation 19 July 24, 2016 Stream Socket Operation (Telephone Analogy) • • • • • • Install telephone Known telephone number Telephone is turned on Other phone dials number Answer phone Hang up phone 20 July 24, 2016 => socket() to create socket => bind() socket to address => listen() for incoming sockets => connect() => listener accept() socket => close() socket “passive” socket (server) socket() bind() “active” socket (client) listen() socket() accept() connect() read() write() blocks until client connects write() close() multiple communications read() close() listen() int listen( int socketfd, int backlog ); • • • • • • • #include <sys/socket.h> Returns 0 on success Returns -1 on error “Listen” for incoming socket connection Marks socketfd as “passive” Socket used to accept connections from “active” sockets backlog : limits the number of pending connections • pending – client calls connect() before server calls accept() 22 July 24, 2016 accept() int accept( int socketfd, struct sockaddr *addr, socklen_t *addrlen); • • • • #include <sys/socket.h> Returns a file descriptor on success Returns -1 on error Accepts an incoming connection on the listening stream socket referred to by socketfd • accept() creates a NEW socket • Listening socket remains open (can be used to accept more connections) 23 July 24, 2016 accept() int accept( int socketfd, struct sockaddr *addr, socklen_t *addrlen); • Remaining arguments return address of peer socket • addr points to structure used to return socket address • addrlen points to an integer that has been initialized to the size of the buffer pointed to be addr • If we don’t care about addresses, set both to NULL 24 July 24, 2016 connect() int connect( int socketfd, const struct sockaddr *addr, socklen_t addrlen ); • • • • #include <sys/socket.h> Returns 0 on success Returns -1 on error Connects the active socket referred to by socketfd to the listening socket specified by addr and addrlen • addr and addrlen specified in same way as bind() 25 July 24, 2016 connect() int connect( int socketfd, const struct sockaddr *addr, socklen_t addrlen ); • If connect() fails: 1. 2. 3. Close the socket Create a new socket Reattempt to connect 26 July 24, 2016 I/O on Stream Sockets • Pair of connected stream sockets provides bidirectional communication • Use read() and write() system calls on socket FDs • If socket closed with close(), read end receives EOF 27 July 24, 2016 Outline • • • • • Sockets Socket Domain and Type Socket System Calls Stream Socket Operation Datagram Socket Operation 28 July 24, 2016 Datagram Socket Operation (Postal System Analogy) • Set up mailbox => socket() • Known address => server bind() socket to address • Send letter (with address) => sendto() • Receive letter => recvfrom() • Remove mailbox => close() 29 July 24, 2016 Datagram Socket Operation (Postal System Analogy) • Just like postal system, when multiple letters (datagrams) sent from on to another: • No guarantee that letters will arrive in order sent • No guarantee that letter(s) will arrive at all • Unlike post system: datagrams can arrive more than once 30 July 24, 2016 Datagram Socket Operation 31 July 24, 2016 sendto() ssize_t sendto( int sockfd, const void *buffer, size_t length, int flags, const struct sockaddr *dest, socklen_t addrlen ); • #include <sys/socket.h> • Returns number of bytes sent; -1 on error • First 3 arguments same as write() system call 32 July 24, 2016 sendto() ssize_t sendto( int sockfd, const void *buffer, size_t length, int flags, const struct sockaddr *dest, socklen_t addrlen ); • flags : bit mask controlling socket-specific I/O features • dest & addrlen – specify address of destination • Same as connect() • Can be set to NULL 33 July 24, 2016 recvfrom() ssize_t recvfrom( • • • • int sockfd, void *buffer, size_t length, int flags, struct sockaddr *src, socklen_t *addrlen #include <sys/socket.h> Returns number of bytes read; -1 on error Retrieves exactly one message First 3 arguments same as read() system call 34 July 24, 2016 ); recvfrom() ssize_t recvfrom( int sockfd, void *buffer, size_t length, int flags, struct sockaddr *src, socklen_t *addrlen ); • flags : bit mask controlling socket-specific I/O features • src & addrlen – gets filled with address of sender • Same as accept() • Can be set to NULL 35 July 24, 2016 connect() with Datagram Sockets • When connect() called on datagram socket, kernel records address of socket’s peer • Known as a “connected datagram socket” • If connect() has NOT been called yet: • “unconnected datagram socket” • Advantage of “connected datagram socket”: simplifies I/O calls when transmitting data • No need to supply dest_addr and addrlen arguments • Use write() instead 36 July 24, 2016