Network Programming Tutorial #9 CPSC 261
A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere else in the world Using either the TCP or UDP transport protocol
Network API Sockets are the key data structure Collection of system calls that create/destroy/manage sockets – Create/destroy: socket, close – Connect: bind, connect, listen, accept – Manage: shutdown, setsockopt – Send/Receive: send, recv, write, read, sendto, recvfrom
Socket calls in sequence (UDP) Client side Socket Bind Sendto Recvfrom Close Server side Socket Bind Recvfrom Sendto Close
Socket calls in sequence (TCP) Client side Socket Bind Connect Send Recv Close Server side Socket Bind Listen Accept Recv Send Close
Socket programming is... baroque * The socket calls were standardized a long time ago – There were lots of different network standards – Each with their own addressing information – The socket calls need to be general enough to handle them all (and any other ones that might come along) – Easy things are much harder than they should need to be * Extravagant, complex, or bizarre
socket() Create a communication endpoint Returns a file descriptor (an integer) which must be used in every call referring to this communication endpoint Returns -1 on error – errno is set to indicate why
socket() socket(domain, type, protocol) domain: AF_UNIX, AF_INET, AF_INET6 type: SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET, SOCK_RDM protocol: 0 means the only available protocol
socket addresses struct sockaddr An address that can be in any address family struct sockaddr_in An address in the INET (v4) address family Consists of a 32 bit IP address ( s_addr ) and a 16 bit port ( s_port ) Both in network byte order All unused bits in an address must be 0
socket addresses – C evil You allocate variables of type struct sockaddr But then treat them as if they are of type struct sockaddr_in For example: struct sockaddr s; struct sockaddr_in *si = (struct sockaddr_in *) &s; si->sin_family... si->sin_addr... si->sin_port...
bind() Establish a local address for a socket The local address is the address of this endpoint, so will be the current machine’s IP address and a port Returns 0 on success Returns -1 on error – errno is set to indicate why
bind() bind(socket, addr, addrlen) socket: a file descriptor returned from socket() addr: a pointer to a struct sockaddr addrlen: the length of the addr
sendto() Sends data through an unconnected socket The destination address is specified each time Returns the number of bytes sent on success Returns -1 on error – errno is set to indicate why
sendto() sendto(socket, buf, len, flags, destaddr, addrlen) socket: the socket file descriptor buf: pointer to data to send len: length of data to send flags: almost always 0 destaddr: a struct sockaddr * indicating the destination socket addrlen: the length of destaddr
recvfrom() Receives data through an unconnected socket The source address is specified each time Returns the number of bytes received on success Returns -1 on error – errno is set to indicate why
recvfrom() recvfrom(socket, buf, len, flags, srcaddr, addrlen) socket: the socket file descriptor buf: pointer to recv buffer len: length of recv buffer flags: almost always 0 srcaddr: a struct sockaddr * for receiving the source socket addr addrlen: a pointer to the length of srcaddr, changed by the call
close() closes a socket makes it unusable in the future for sending or receiving Returns 0 on success Returns -1 on error – errno is set to indicate why
close() close(socket) socket: the socket file descriptor
Look at a couple of examples All are in the network directory of the lectures repository – receive.c – send.c – shared.c
Socket calls in sequence (TCP) Client side Socket Bind Connect Send Recv Close Server side Socket Bind Listen Accept Recv Send Close
listen() Indicates that the socket is to be used for accepting incoming connections Done on the “server” side only Returns 0 on success Returns -1 on error – errno is set to indicate why
listen() listen(socket, backlog) socket: a file descriptor returned from socket() backlog: the number of incoming connection requests to allow to queue
connect() Connects a socket to a destination socket whose address is given as an argument Done on the “client” side only Returns 0 on success Returns -1 on error – errno is set to indicate why
connect() connect(socket, addr, addrlen) socket: a file descriptor returned from socket() addr: a pointer to a struct sockaddr addrlen: the length of the addr
accept() Accepts an incoming connection – waits for an incoming connection request Done on the “server” side only Returns a new socket file descriptor on success Returns -1 on error – errno is set to indicate why
accept() accept(socket, addr, addrlen) socket: a file descriptor returned from socket() addr: a pointer to a struct sockaddr addrlen: a pointer to the length of the addr, changed by the call
send() Sends data through a connected socket No destination address is specified Returns the number of bytes sent on success Returns -1 on error – errno is set to indicate why
send() send(socket, buf, len, flags) socket: the socket file descriptor buf: pointer to data to send len: length of data to send flags: almost always 0
recv() Receives data through a connected socket No source address is indicated Returns the number of bytes received on success – 0 usually means “end-of-file”, meaning there will be no more data to receive, ever again Returns -1 on error – errno is set to indicate why
recv() recv(socket, buf, len, flags) socket: the socket file descriptor buf: pointer to recv buffer len: length of recv buffer flags: almost always 0
shutdown() Indicates that an application is done with either sending or receiving data on a socket When a connected stream socket is shutdown for sends, then the other side receives “end- of-file” Returns 0 on success Returns -1 on error – errno is set to indicate why
shutdown() shutdown(socket, how) socket: a file descriptor returned from socket() how: SHUT_RD no more receives SHUT_WR no more sends SHUT_RDWR no more communication at all
Web resources There are lots of network programming guides on the net The “standard” reference is Beej’s guide It has become way too long over the years The 1999 version is pretty good – On the web pointed to by the lab page
Bits of advice Pay very close attention to the arguments to every network call – Especially addresses Check the returned value from every network call – If something goes wrong, you need to know about it early – And bail out, or start over and try again