Transcription of SOCKETS: INTRODUCTION - Denison University
1 sockets : INTRODUCTIONS ockets are a method of IPC that allow data to be exchanged between applications,either on the same host (computer) or on different hosts connected by a network. Thefirst widespread implementation of the sockets API appeared with in 1983,and this API has been ported to virtually every UNIX implementation, as well asmost other operating sockets API is formally specified in , which was ratified in 2000after spending about a decade as a draft standard. This standard has beensuperseded by chapter and the following chapters describe the use of sockets , as follows:zThis chapter provides a general INTRODUCTION to the sockets API.
2 The followingchapters assume an understanding of the general concepts presented here. Wedon t present any example code in this chapter. Code examples in the UNIXand Internet domains are presented in the following 57 describes UNIX domain sockets , which allow communicationbetween applications on the same host 58 introduces various computer networking concepts and describeskey features of the TCP/IP networking protocols. It provides backgroundneeded for the next 59 describes Internet domain sockets , which allow applications on dif-ferent hosts to communicate via a TCP/IP 56zChapter 60 discusses the design of servers that use 61 covers a range of advanced topics, including additional features forsocket I/O, a more detailed look at the TCP protocol, and the use of socketoptions to retrieve and modify various attributes of chapters merely aim to give the reader a good grounding in the use of programming, especially for network communication.
3 Is an enormoustopic in its own right, and forms the subject of entire books. Sources of furtherinformation are listed in Section OverviewIn a typical client-server scenario, applications communicate using sockets as follows:zEach application creates a socket. A socket is the apparatus that allows com-munication, and both applications require server binds its socket to a well-known address (name) so that clients canlocate socket is created using the socket() system call, which returns a file descriptor usedto refer to the socket in subsequent system calls:fd = socket(domain, type, protocol);We describe socket domains and types in the following paragraphs.
4 For all applica-tions described in this book, protocol is always specified as domainsSockets exist in a communication domain, which determines:zthe method of identifying a socket ( , the format of a socket address ); andzthe range of communication ( , either between applications on the same hostor between applications on different hosts connected via a network).Modern operating systems support at least the following domains:zThe UNIX (AF_UNIX) domain allows communication between applications onthe same host. ( used the name AF_LOCAL as a synonym for AF_UNIX, butthis name is not used in SUSv3.)
5 ZThe IPv4 (AF_INET) domain allows communication between applications run-ning on hosts connected via an Internet Protocol version 4 (IPv4) IPv6 (AF_INET6) domain allows communication between applications runningon hosts connected via an Internet Protocol version 6 (IPv6) IPv6 is designed as the successor to IPv4, the latter protocol is cur-rently still the most widely 56-1 summarizes the characteristics of these socket : Introduction1151In some code, we may see constants with names such as PF_UNIX instead ofAF_UNIX. In this context, AF stands for address family and PF stands for protocolfamily.
6 Initially, it was conceived that a single protocol family might supportmultiple address families. In practice, no protocol family supporting multipleaddress families has ever been defined, and all existing implementationsdefine the PF_ constants to be synonymous with the corresponding AF_ constants.(SUSv3 specifies the AF_ constants, but not the PF_ constants.) In this book, wealways use the AF_ constants. Further information about the history of theseconstants can be found in Section of [Stevens et al., 2004].Socket typesEvery sockets implementation provides at least two types of sockets : stream anddatagram.
7 These socket types are supported in both the UNIX and the Internetdomains. Table 56-2 summarizes the properties of these socket sockets (SOCK_STREAM) provide a reliable, bidirectional, byte-stream communi-cation channel. By the terms in this description, we mean the following:zReliable means that we are guaranteed that either the transmitted data willarrive intact at the receiving application, exactly as it was transmitted by thesender (assuming that neither the network link nor the receiver crashes), orthat we ll receive notification of a probable failure in means that data may be transmitted in either direction betweentwo means that, as with pipes, there is no concept of message bound-aries (refer to Section ).
8 Table 56-1: Socket domainsDomain Communicationperformed Communicationbetween applicationsAddress formatAddress structureAF_UNIX within kernel on same hostpathnamesockaddr_unAF_INETvia IPv4on hosts connected via an IPv4 network32-bit IPv4 address + 16-bit port numbersockaddr_inAF_INET6via IPv6on hosts connected via an IPv6 network128-bit IPv6 address + 16-bit port numbersockaddr_in6 Table 56-2: Socket types and their propertiesPropertySocket typeStream DatagramReliable delivery?YNMessage boundaries preserved? NYConnection-oriented?YN1152 Chapter 56A stream socket is similar to using a pair of pipes to allow bidirectional communica-tion between two applications, with the difference that (Internet domain) socketspermit communication over a sockets operate in connected pairs.
9 For this reason, stream sockets aredescribed as connection-oriented. The term peer socket refers to the socket at the otherend of a connection; peer address denotes the address of that socket; and peerapplication denotes the application utilizing the peer socket. Sometimes, the termremote (or foreign) is used synonymously with peer. Analogously, sometimes the termlocal is used to refer to the application, socket, or address for this end of the con-nection. A stream socket can be connected to only one sockets (SOCK_DGRAM) allow data to be exchanged in the form of mes-sages called datagrams.
10 With datagram sockets , message boundaries are preserved,but data transmission is not reliable. Messages may arrive out of order, be dupli-cated, or not arrive at sockets are an example of the more generic concept of aconnectionless socket. Unlike a stream socket, a datagram socket doesn t need to beconnected to another socket in order to be used. (In Section , we ll see thatdatagram sockets may be connected with one another, but this has somewhat dif-ferent semantics from connected stream sockets .)In the Internet domain, datagram sockets employ the User Datagram Protocol(UDP), and stream sockets (usually) employ the Transmission Control Protocol (TCP).