Program Interface (API)
Program Interface (API)
portability Used by both clients and servers Extension to UNIX file I/O paradigm API Application interactions with protocol software: o Passive listen or active open o Protocol to use o IP address and port number Interface to protocol is call Application Program Interface (API) o Defined by programming/operating system o Includes collection of procedures for application program
The Socket API Protocols do not typically specify API API defined by programming system Allows greatest flexibility - compatibility with different programming systems Socket API is a specific protocol API o Originated with Berkeley BSD UNIX o Now available on Windows 95 and Windows NT, Solaris, etc. Not defined as TCP/IP standard; de facto standard Sockets and socket libraries BSD UNIX includes sockets as system calls Some systems have different API o Adding sockets would require changing OS o Added library procedures - socket library - instead Adds layer of software between application and operating system o Enhances portability o May hide native API altogether
Sockets and UNIX I/O Developed as extension to UNIX I/O system Uses same file descriptor address space (small integers) Based on open-read-write-close paradigm o open - prepare a file for access o read/write - access contents of file o close - gracefully terminate use of file Open returns a file descriptor, which is used to identify the file to read/write/close The socket API Socket programming more complex than file I/O Requires more parameters o Addresses o Protocol port numbers o Type of protocol o New semantics Two techniques o Add parameters to existing I/O system calls o Create new system calls Sockets use a collection of new system calls
3
socket descriptor = socket(protofamily, type, protocol) Returns socket descriptor used in subsequent calls protofamily selects protocol family; e.g.: o PF_INET - Internet protocols o PF_APPLETALK - AppleTalk protocols type selects type of communication o SOCK_DGRAM - connectionless o SOCK_STREAM - connection-oriented protocol specifies protocol within protocol family o IPPROTO_TCP - selects TCP o IPPROTO_UDP - selects UDP
close close(descriptor) Terminates use of socket descriptor descriptor contains descriptor of socket to be closed bind bind(socket, localaddr, address) Initially, socket has no addresses attached bind selects either local, remote or both addresses o server binds local port number for incoming messages o client binds remote address and port number to contact server
Socket address formats Because sockets can be used for any protocols, address format is generic:
/* total length of address */ /* family of the address */ /* address */
struct sockaddr_in { u_char sin_len; u_char sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8] }
First two fields match generic sockaddr structure Remainder are specific to IP protocols INADDR_ANY interpreted to mean "any" IP address
7
listen listen(socket, queuesize) Server uses listen to wait for incoming connections socket identifies socket through which connections will arrive (address) New connection requests may arrive while server processes previous request Operating system can hold requests on queue queuesize sets upper limit on outstanding requests
accept accept(socket, caddress, caddresslen) Server uses accept to accept the next connection request accept call blocks until connection request arrives Returns new socket with server's end of new connection Old socket remains unchanged and continues to field incoming requests caddress returns struct sockaddr client address; format depends on address family of socket caddresslen returns length of address
connect connect(socket, saddress, saddresslen) Client uses connect to establish connection to server Blocks until connection completed (accepted) socket holds descriptor of socket to use saddress is a struct sockaddr that identifies server saddresslen gives length of saddress Usually used with connection-oriented transport protocol Can be used with connectionless protocol o Marks local socket with server address o Implicitly identifies server for subsequent messages
10
send send(socket, data, length, flags) Used to send data through a connected socket socket identifies socket data points to data to be sent length gives length of data (in bytes) flags indicate special options
11
sendto, sendmsg sendto(socket, data, length, flags, destaddress, addresslen) sendmsg(socket, msgstruct, flags) Used for unconnected sockets by explicitly specifying destination sendto adds additional parameters: o destaddress - struct sockaddr destination address o addresslen - length of destaddress sendmsg combines list of parameters into single structure:
struct msgstruct { struct sockaddr *m_addr; struct datavec *m_vec; int m_dvlength; struct access *m_rights; int m_alength; } /* /* /* /* /* ptr to destination address pointer to message vector num. of items in vector ptr to access rights list num. of items in list */ */ */ */ */
recv
12
recv(socket, buffer, length, flags) Used to receive incoming data through connected socket socket identifies the socket Data copied into buffer At most length bytes will be recved flags give special options Returns number of bytes actually recved o 0 implies connection closed o -1 implies error
13
recvfrom, recvmsg recvfrom(socket, buffer, length, flags, sndraddress, addresslen) recvmsg(socket, msgstruct, flags) Like recvfrom and recvmsg (in reverse!) Address of source copied into sndraddress Length of address in addresslen recvmsg uses msgstruct for parameters
Other procedures getpeername - address of other end of connection getsockname - current address bound to socket setsockopt - set socket options Sockets and processes
14
Like file descriptors, sockets are inherited by child processes Socket disappears when all processes have closed it Servers use socket inheritance to pass incoming connections to slave server processes
15