牢靠UNIXDomainSockets
牢靠 音讯 过程 通信 协定 网络 机制 对等 效力 序号 框架 混乱 相似 接口 当时 主机 数据
UNIXDomainSocketssocket API原本是为网络通信设想的!但当时正在socket的框架上开展出壹种IPC机制!就是UNIX Domain Socket固然网络socket也可用于同壹台主机的过程间通信!但是UNIX Domain Socket用于IPC更有效力不需求经由网络协定栈!不需求打包拆包!盘算校验和!保护序号和应对等!只是将使用层数据从壹个过程拷贝到另壹个过程这是因为!IPC机制实质上是牢靠的通信!而网络协定是为不牢靠的通信设想的UNIX Domain Socket也提供面向流和面向数据包两种API接口!相似于TCP和UDP!但是面向音讯的UNIX Domain Socket也是牢靠的!音讯既不会丢失也不会次第混乱 UNIX Domain Sockets 的使用参考 http://book.chinaunix.net/special/ebook/addisonWes ley/APUE2/0201433079/ch17lev1sec3.html UNIX domain sockets are used to communicate with processes running on the same machine. Although Internet domain sockets can be used for this same purpose! UNIX domain sockets are more efficient. UNIX domain sockets only copy data。 they have no protocol processing to perform! no network headers to add or remove! no checksums to calculate! no sequence numbers to generate! and no acknowledgements to send. UNIX domain sockets provide both stream and datagram interfaces. The UNIX domain datagram service is reliable! however. Messages are neither lost nor delivered out of order. UNIX domain sockets are like a cross between sockets and pipes. You can use the network-oriented socket interfaces with them! or you can use the socketpair function to create a pair of unnamed! connected! UNIX domain sockets. Although the interface is sufficiently general to allow socketpair to be used with arbitrary domains! operating systems typically provide support only for the UNIX domain. Figure 17.13 shows the socket-based version of the s_pipe function previously shown in Figure 17.6. The function creates a pair of connected UNIX domain stream sockets. Some BSD-based systems use UNIX domain sockets to implement pipes. But when pipe is called! the write end of the first descriptor and the read end of the second descriptor are both closed. To get a full-duplex pipe! we must call socketpair directly. Although the socketpair function creates sockets that are connected to each other! the individual sockets dont have names. This means that they cant be addressed by unrelated processes. In Section 16.3.4! we learned how to bind an address to an Internet domain socket. Just as with Internet domain sockets! UNIX domain sockets can be named and used to advertise services. The address format used with UNIX domain sockets differs from Internet domain sockets! however. Recall from Section 16.3 that socket address formats differ from one implementation to the next. An address for a UNIX domain socket is represented by a sockaddr_un structure. On Linux 2.4.22 and Solaris 9! the sockaddr_un structure is defined in the header lt。sys/un.hgt。 as follows: On FreeBSD 5.2.1 and Mac OS X 10.3! however! the sockaddr_un structure is defined as The sun_path member of the sockaddr_un structure contains a pathname. When we bind an address to a UNIX domain socket! the system creates a file of type S_IFSOCK with the same name. This file exists only as a means of advertising the socket name to clients. The file cant be opened or otherwise used for communication by applications. If the file already exists when we try to bind the same address! the bind request will fail. When we close the socket! this file is not automatically removed! so we need to make sure that we unlink it before our application exits. The program in Figure 17.14 shows an example of binding an address to a UNIX domain socket. When we run this program! the bind request succeeds! but if we run the program a second time! we get an error! because the file already exists. The program wont succeed again until we remove the file. The way we determine the size of the address to bind is to determine the offset of the sun_path member in the sockaddr_un structure and add to this the length of the pathname! not including the terminating null byte. Since implementations vary in what members precede sun_path in the sockaddr_un structure! we use the offsetof macro from lt。stddef.hgt。 to calculate the offset of the sun_path member from the start of the structure. If you look in lt。stddef.hgt。! youll see a definition similar to the following: The expression evaluates to an integer! which is the starting address of the member! assuming that the structure begins at address 0. include "apue.h" include lt。sys/socket.hgt。 include lt。sys/un.hgt。 int main int fd! size。 struct sockaddr_un un。 un.sun_family = AF_UNIX。 strcpy。 if ) lt。 0) err_sys。 size = offsetof + strlen。 if un! size) lt。 0) err_sys。 printf。 exit。socket API原本是为网络通信设想的!但当时正在socket的框架上开展出壹种IPC机制!就是UNIX Domain Socket固
非凡教程网 http://www.ffjc.info
UNIXDomainSocketssocket API原本是为网络通信设想的!但当时正在socket的框架上开展出壹种IPC机制!就是UNIX Domain Socket固然网络socket也可用于同壹台主机的过程间通信!但是UNIX Domain Socket用于IPC更有效力不需求经由网络协定栈!不需求打包拆包!盘算校验和!保护序号和应对等!只是将使用层数据从壹个过程拷贝到另壹个过程这是因为!IPC机制实质上是牢靠的通信!而网络协定是为不牢靠的通信设想的UNIX Domain Socket也提供面向流和面向数据包两种API接口!相似于TCP和UDP!但是面向音讯的UNIX Domain Socket也是牢靠的!音讯既不会丢失也不会次第混乱 UNIX Domain Sockets 的使用参考 http://book.chinaunix.net/special/ebook/addisonWes ley/APUE2/0201433079/ch17lev1sec3.html UNIX domain sockets are used to communicate with processes running on the same machine. Although Internet domain sockets can be used for this same purpose! UNIX domain sockets are more efficient. UNIX domain sockets only copy data。 they have no protocol processing to perform! no network headers to add or remove! no checksums to calculate! no sequence numbers to generate! and no acknowledgements to send. UNIX domain sockets provide both stream and datagram interfaces. The UNIX domain datagram service is reliable! however. Messages are neither lost nor delivered out of order. UNIX domain sockets are like a cross between sockets and pipes. You can use the network-oriented socket interfaces with them! or you can use the socketpair function to create a pair of unnamed! connected! UNIX domain sockets. Although the interface is sufficiently general to allow socketpair to be used with arbitrary domains! operating systems typically provide support only for the UNIX domain. Figure 17.13 shows the socket-based version of the s_pipe function previously shown in Figure 17.6. The function creates a pair of connected UNIX domain stream sockets. Some BSD-based systems use UNIX domain sockets to implement pipes. But when pipe is called! the write end of the first descriptor and the read end of the second descriptor are both closed. To get a full-duplex pipe! we must call socketpair directly. Although the socketpair function creates sockets that are connected to each other! the individual sockets dont have names. This means that they cant be addressed by unrelated processes. In Section 16.3.4! we learned how to bind an address to an Internet domain socket. Just as with Internet domain sockets! UNIX domain sockets can be named and used to advertise services. The address format used with UNIX domain sockets differs from Internet domain sockets! however. Recall from Section 16.3 that socket address formats differ from one implementation to the next. An address for a UNIX domain socket is represented by a sockaddr_un structure. On Linux 2.4.22 and Solaris 9! the sockaddr_un structure is defined in the header lt。sys/un.hgt。 as follows: On FreeBSD 5.2.1 and Mac OS X 10.3! however! the sockaddr_un structure is defined as The sun_path member of the sockaddr_un structure contains a pathname. When we bind an address to a UNIX domain socket! the system creates a file of type S_IFSOCK with the same name. This file exists only as a means of advertising the socket name to clients. The file cant be opened or otherwise used for communication by applications. If the file already exists when we try to bind the same address! the bind request will fail. When we close the socket! this file is not automatically removed! so we need to make sure that we unlink it before our application exits. The program in Figure 17.14 shows an example of binding an address to a UNIX domain socket. When we run this program! the bind request succeeds! but if we run the program a second time! we get an error! because the file already exists. The program wont succeed again until we remove the file. The way we determine the size of the address to bind is to determine the offset of the sun_path member in the sockaddr_un structure and add to this the length of the pathname! not including the terminating null byte. Since implementations vary in what members precede sun_path in the sockaddr_un structure! we use the offsetof macro from lt。stddef.hgt。 to calculate the offset of the sun_path member from the start of the structure. If you look in lt。stddef.hgt。! youll see a definition similar to the following: The expression evaluates to an integer! which is the starting address of the member! assuming that the structure begins at address 0. include "apue.h" include lt。sys/socket.hgt。 include lt。sys/un.hgt。 int main int fd! size。 struct sockaddr_un un。 un.sun_family = AF_UNIX。 strcpy。 if ) lt。 0) err_sys。 size = offsetof + strlen。 if un! size) lt。 0) err_sys。 printf。 exit。socket API原本是为网络通信设想的!但当时正在socket的框架上开展出壹种IPC机制!就是UNIX Domain Socket固
非凡教程网 http://www.ffjc.info
朋友新绝代双骄之鱼戏江湖
重点系统进修嵌入式Lin