Example: quiz answers

SOCKETS: INTRODUCTION - Denison University

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. 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.

SOCKETS: INTRODUCTION ... socket I/O, a more detailed look at the TCP protocol, and the use of socket ... Sockets programming, especially for network communication, is an enormous topic in its own right, and forms the subject of entire books. Sources of further information are listed in …

Tags:

  Introduction, Programming, Sockets

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

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. 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.

2 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, 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.

3 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. 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.

4 ( used the name AF_LOCAL as a synonym for AF_UNIX, butthis name is not used in SUSv3.)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. 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.

5 (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. 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 ).

6 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. For this reason, stream sockets aredescribed as connection-oriented.

7 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. 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.

8 (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).Instead of using the terms Internet domain datagram socket and Internet domain streamsocket, we ll often just use the terms UDP socket and TCP socket, system callsThe key socket system calls are the following:zThe socket() system call creates a new bind() system call binds a socket to an address. Usually, a server employsthis call to bind its socket to a well-known address so that clients can locatethe listen() system call allows a stream socket to accept incoming connectionsfrom other accept() system call accepts a connection from a peer application on a listen-ing stream socket, and optionally returns the address of the peer connect() system call establishes a connection with another most Linux architectures (the exceptions include Alpha and IA-64), all ofthe sockets system calls are actually implemented as library functions multi-plexed through a single system call, socketcall().

9 (This is an artifact of the originaldevelopment of the Linux sockets implementation as a separate project.)Nevertheless, we refer to all of these functions as system calls in this book,since this is what they were in the original BSD implementation, as well as inmany other contemporary UNIX : Introduction1153 Socket I/O can be performed using the conventional read() and write() system calls,or using a range of socket-specific system calls ( , send(), recv(), sendto(), andrecvfrom()). By default, these system calls block if the I/O operation can t be com-pleted immediately. Nonblocking I/O is also possible, by using the fcntl() F_SETFL operation (Section ) to enable the O_NONBLOCK open file status Linux, we can call ioctl(fd, FIONREAD, &cnt) to obtain the number ofunread bytes available on the stream socket referred to by the file descriptorfd.

10 For a datagram socket, this operation returns the number of bytes in thenext unread datagram (which may be zero if the next datagram is of zerolength), or zero if there are no pending datagrams. This feature is not speci-fied in Creating a Socket: socket()The socket() system call creates a new domain argument specifies the communication domain for the socket. The typeargument specifies the socket type. This argument is usually specified as eitherSOCK_STREAM, to create a stream socket, or SOCK_DGRAM, to create a datagram protocol argument is always specified as 0 for the socket types we describe inthis book. Nonzero protocol values are used with some socket types that we don tdescribe. For example, protocol is specified as IPPROTO_RAW for raw sockets (SOCK_RAW).On success, socket() returns a file descriptor used to refer to the newly createdsocket in later system with kernel , Linux provides a second use for the type argument,by allowing two nonstandard flags to be ORed with the socket type.


Related search queries