Lecture 14 Sockets I Outline Sockets Socket Domain

  • Slides: 37
Download presentation
Lecture 14 Sockets I

Lecture 14 Sockets I

Outline • • • Sockets Socket Domain and Type Socket System Calls Stream Socket

Outline • • • Sockets Socket Domain and Type Socket System Calls Stream Socket Operation Datagram Socket Operation 1 November 22, 2020

Sockets • Method of bidirectional interprocess communication (IPC) • Can be on same host

Sockets • Method of bidirectional interprocess communication (IPC) • Can be on same host (computer) • Or different hosts connected by network 2 November 22, 2020

Sockets in Client-Server Applications • Each application creates a socket • Socket is the

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 November 22, 2020

Socket Creation • Socket created with socket() system call fd = socket( domain, type,

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 November 22, 2020

Outline • • • Sockets Socket Domain and Type Socket System Calls Stream Socket

Outline • • • Sockets Socket Domain and Type Socket System Calls Stream Socket Operation Datagram Socket Operation 5 November 22, 2020

Socket Domain fd = socket( domain, type, protocol) • Sockets exist in a communication

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_INET 6 6 November 22, 2020

Socket Domain • Linux supports 3 different domains: 1. UNIX domain (AF_UNIX): allows communication

Socket Domain • Linux supports 3 different domains: 1. UNIX domain (AF_UNIX): allows communication between applications on same host Address = pathname 7 November 22, 2020

Socket Domain • Linux supports 3 different domains: 2. IPv 4 domain (AF_INET): allows

Socket Domain • Linux supports 3 different domains: 2. IPv 4 domain (AF_INET): allows communication between application on hosts connected with Internet Protocol v 4 Address = 32 -bit IPv 4 address + 16 -port number 8 November 22, 2020

Socket Domain • Linux supports 3 different domains: 3. IPv 6 domain (AF_INET 6):

Socket Domain • Linux supports 3 different domains: 3. IPv 6 domain (AF_INET 6): allows communication between application on hosts connected with Internet Protocol v 6 Address= 128 -bit IPv 6 address + 16 -bit port number 9 November 22, 2020

Socket Type fd = socket( domain, type, protocol) • Two types of sockets 1.

Socket Type fd = socket( domain, type, protocol) • Two types of sockets 1. Stream 2. Datagram 10 November 22, 2020

Stream Sockets • Reliable, bidirectional, byte-stream communication channel • Similar to having two pipes

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 November 22, 2020

Datagram Sockets • Allows data to be exchanged in “datagram” messages • Message boundaries

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 November 22, 2020

Internet Domain • Stream sockets employ TCP (transmission control protocol) • Datagram sockets employ

Internet Domain • Stream sockets employ TCP (transmission control protocol) • Datagram sockets employ UDP (user datagram protocol) 13 November 22, 2020

Outline • • • Sockets Socket Domain and Type Socket System Calls Stream Socket

Outline • • • Sockets Socket Domain and Type Socket System Calls Stream Socket Operation Datagram Socket Operation 14 November 22, 2020

Socket System Calls • • • socket() – creates a new socket bind() –

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 November 22, 2020

socket() int socket( int domain, int type, int protocol ); • • #include <sys/socket.

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_INET 6 ) type : type of socket ( SOCK_STREAM or SOCK_DGRAM ) protocol : set to 0 16 November 22, 2020

bind() int bind( int socketfd, const struct sockaddr *addr, socklen_t addrlen ); • •

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 November 22, 2020

struct sockaddr • Generic socket address structure • Size depends on domain • Typical

struct sockaddr • Generic socket address structure • Size depends on domain • Typical contents: sa_family_t char 18 November 22, 2020 sa_family; //address family (AF_*) sa_data[14]; //socket address

Outline • • • Sockets Socket Domain and Type Socket System Calls Stream Socket

Outline • • • Sockets Socket Domain and Type Socket System Calls Stream Socket Operation Datagram Socket Operation 19 November 22, 2020

Stream Socket Operation (Telephone Analogy) • • • Install telephone Known telephone number Telephone

Stream Socket Operation (Telephone Analogy) • • • Install telephone Known telephone number Telephone is turned on Other phone dials number Answer phone Hang up phone 20 November 22, 2020 => 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()

“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

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 November 22, 2020

accept() int accept( int socketfd, struct sockaddr *addr, socklen_t *addrlen); #include <sys/socket. h> Returns

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 November 22, 2020

accept() int accept( int socketfd, struct sockaddr *addr, socklen_t *addrlen); • Remaining arguments return

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 November 22, 2020

connect() int connect( int socketfd, const struct sockaddr *addr, socklen_t addrlen ); • •

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 November 22, 2020

connect() int connect( int socketfd, const struct sockaddr *addr, socklen_t addrlen ); • If

connect() int connect( int socketfd, const struct sockaddr *addr, socklen_t addrlen ); • If connect() fails: 1. Close the socket 2. Create a new socket 3. Reattempt to connect 26 November 22, 2020

I/O on Stream Sockets • Pair of connected stream sockets provides bidirectional communication •

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 November 22, 2020

Outline • • • Sockets Socket Domain and Type Socket System Calls Stream Socket

Outline • • • Sockets Socket Domain and Type Socket System Calls Stream Socket Operation Datagram Socket Operation 28 November 22, 2020

Datagram Socket Operation (Postal System Analogy) • Set up mailbox => socket() • Known

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 November 22, 2020

Datagram Socket Operation (Postal System Analogy) • Just like postal system, when multiple letters

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 November 22, 2020

Datagram Socket Operation 31 November 22, 2020

Datagram Socket Operation 31 November 22, 2020

sendto() ssize_t sendto( int sockfd, const void *buffer, size_t length, int flags, const struct

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 November 22, 2020

sendto() ssize_t sendto( int sockfd, const void *buffer, size_t length, int flags, const struct

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 November 22, 2020

recvfrom() ssize_t recvfrom( • • int sockfd, void *buffer, size_t length, int flags, struct

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 November 22, 2020 );

recvfrom() ssize_t recvfrom( int sockfd, void *buffer, size_t length, int flags, struct sockaddr *src,

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 November 22, 2020

connect() with Datagram Sockets • When connect() called on datagram socket, kernel records address

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 November 22, 2020