《外文文献—UNIX环境高级编程》由会员分享,可在线阅读,更多相关《外文文献—UNIX环境高级编程(11页珍藏版)》请在金锄头文库上搜索。
1、英文原文Advanced Programming in the UNIX EnvironmentW.Richard Stevens, StephenA.RagoChapter 16 Network IPC: Sockets16.1. IntroductionIn the previous chapter, we looked at pipes, FIFOs, message queues, semaphores, and shared memory: the classical methods of IPC provided by various UNIX systems. These mec
2、hanisms allow processes running on the same computer to communicate with one another. In this chapter, we look at the mechanisms that allow processes running on different computers (connected to a common network) to communicate with one another: network IPC.In this chapter, we describe the socket ne
3、twork IPC interface, which can be used by processes to communicate with other processes, regardless of where they are running: on the same machine or on different machines. Indeed, this was one of the design goals of the socket interface. The same interfaces can be used for both intermachine communi
4、cation and intramachine communication. Although the socket interface can be used to communicate using many different network protocols, we will restrict our discussion to the TCP/IP protocol suite in this chapter, since it is the de facto standard for communicating over the Internet.The socket API a
5、s specified by POSIX.1 is based on the 4.4BSD socket interface. Although minor changes have been made over the years, the current socket interface closely resembles the interface when it was originally introduced in 4.2BSD in the early 1980s.This chapter is only an overview of the socket API. Steven
6、s, Fenner, and Rudoff 2004 discuss the socket interface in detail in the definitive text on network programming in the UNIX System.16.2. Socket DescriptorsA socket is an abstraction of a communication endpoint. Just as they would use file descriptors to access a file, applications use socket descrip
7、tors to access sockets. Socket descriptors are implemented as file descriptors in the UNIX System. Indeed, many of the functions that deal with file descriptors, such as read and write, will work with a socket descriptor.To create a socket, we call the socket function.The domain argument determines
8、the nature of the communication, including the address format (described in more detail in the next section). Figure 16.1 summarizes the domains specified by POSIX.1. The constants start with AF_ (for address family) because each domain has its own format for representing an address. #include int so
9、cket (int domain, int type, int protocal);Return: file(socket) descriptor if OK, 1 on errorThe domain argument determines the nature of the communication, including the address format (described in more detail in the next section). Figure 16.1 summarizes the domains specified by POSIX.1. The constan
10、ts start with AF_ (for address family) because each domain has its own format for representing an address.Figure 16.1 Socket communication domainsWe discuss the UNIX domain in Section 17.3. Most systems define the AF_LOCAL domain also, which is an alias for AF_UNIX. The AF_UNSPEC domain is a wildcar
11、d that represents any domain. Historically, some platforms provide support for additional network protocols, such as AF_IPX for the NetWare protocol family, but domain constants for these protocols are not defined by the POSIX.1 standard.The type argument determines the type of the socket, which fur
12、ther determines the communication characteristics. The socket types defined by POSIX.1 are summarized in Figure 16.2, but implementations are free to add support for additional types.Figure 16.2 Socket typesThe protocol argument is usually zero, to select the default protocol for the given domain an
13、d socket type. When multiple protocols are supported for the same domain and socket type, we can use the protocol argument to select a particular protocol. The default protocol for a SOCK_STREAM socket in the AF_INET communication domain is TCP (Transmission Control Protocol). The default protocol f
14、or a SOCK_DGRAM socket in the AF_INET communication domain is UDP (User Datagram Protocol).With a datagram (SOCK_DGRAM) interface, no logical connection needs to exist between peers for them to communicate. All you need to do is send a message addressed to the socket being used by the peer process.A
15、 datagram, therefore, provides a connectionless service. A byte stream (SOCK_STREAM), on the other hand, requires that, before you can exchange data, you set up a logical connection between your socket and the socket belonging to the peer you want to communicate with.A datagram is a self-contained m
16、essage. Sending a datagram is analogous to mailing someone a letter. You can mail many letters, but you cant guarantee the order of delivery, and some might get lost along the way. Each letter contains the address of the recipient, making the letter independent from all the others. Each letter can even go to different recipients.In contrast, using a connection-oriented protocol for communicating with a peer is like m