ivaneeo's blog

          自由的力量,自由的生活。

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

          socket API原本是為網(wǎng)絡(luò)通訊設(shè)計的,但后來在socket的框架上發(fā)展出一種IPC機制,就是UNIX Domain Socket。雖然網(wǎng)絡(luò)socket也可用于同一臺主機的進程間通訊(通過loopback地址127.0.0.1),但是UNIX Domain Socket用于IPC更有效率:不需要經(jīng)過網(wǎng)絡(luò)協(xié)議棧,不需要打包拆包、計算校驗和、維護序號和應(yīng)答等,只是將應(yīng)用層數(shù)據(jù)從一個進程拷貝到另一個進程。這是因為,IPC機制本質(zhì)上是可靠的通訊,而網(wǎng)絡(luò)協(xié)議是為不可靠的通訊設(shè)計的。UNIX Domain Socket也提供面向流和面向數(shù)據(jù)包兩種API接口,類似于TCP和UDP,但是面向消息的UNIX Domain Socket也是可靠的,消息既不會丟失也不會順序錯亂。

          UNIX Domain Socket是全雙工的,API接口語義豐富,相比其它IPC機制有明顯的優(yōu)越性,目前已成為使用最廣泛的IPC機制,比如X Window服務(wù)器和GUI程序之間就是通過UNIX Domain Socket通訊的。

          使用UNIX Domain Socket的過程和網(wǎng)絡(luò)socket十分相似,也要先調(diào)用socket()創(chuàng)建一個socket文件描述符,address family指定為AF_UNIX,type可以選擇SOCK_DGRAM或SOCK_STREAM,protocol參數(shù)仍然指定為0即可。

          UNIX Domain Socket與網(wǎng)絡(luò)socket編程最明顯的不同在于地址格式不同,用結(jié)構(gòu)體sockaddr_un表示,網(wǎng)絡(luò)編程的socket地址是IP地址加端口號,而UNIX Domain Socket的地址是一個socket類型的文件在文件系統(tǒng)中的路徑,這個socket文件由bind()調(diào)用創(chuàng)建,如果調(diào)用bind()時該文件已存在,則bind()錯誤返回。

          以下程序?qū)NIX Domain socket綁定到一個地址。

          #include <stdlib.h>
          #include <stdio.h>
          #include <stddef.h>
          #include <sys/socket.h>
          #include <sys/un.h>
          int main(void)
          {
          int fd, size;
          struct sockaddr_un un;
          memset(&un, 0, sizeof(un));
          un.sun_family = AF_UNIX;
          strcpy(un.sun_path, "foo.socket");
          if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
          perror("socket error");
          exit(1);
          }
          size = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path);
          if (bind(fd, (struct sockaddr *)&un, size) < 0) {
          perror("bind error");
          exit(1);
          }
          printf("UNIX domain socket bound\n");
          exit(0);
          }

          注意程序中的offsetof宏,它在stddef.h頭文件中定義:

          #define offsetof(TYPE, MEMBER) ((int)&((TYPE *)0)->MEMBER)

          offsetof(struct sockaddr_un, sun_path)就是取sockaddr_un結(jié)構(gòu)體的sun_path成員在結(jié)構(gòu)體中的偏移,也就是從結(jié)構(gòu)體的第幾個字節(jié)開始是sun_path成員。想一想,這個宏是如何實現(xiàn)這一功能的?

          該程序的運行結(jié)果如下。

          $ ./a.out
          UNIX domain socket bound
          $ ls -l foo.socket
          srwxrwxr-x 1 user        0 Aug 22 12:43 foo.socket
          $ ./a.out
          bind error: Address already in use
          $ rm foo.socket
          $ ./a.out
          UNIX domain socket bound

          以下是服務(wù)器的listen模塊,與網(wǎng)絡(luò)socket編程類似,在bind之后要listen,表示通過bind的地址(也就是socket文件)提供服務(wù)。

          #include <stddef.h>
          #include <sys/socket.h>
          #include <sys/un.h>
          #include <errno.h>
          #define QLEN 10
          /*
          * Create a server endpoint of a connection.
          * Returns fd if all OK, <0 on error.
          */
          int serv_listen(const char *name)
          {
          int                 fd, len, err, rval;
          struct sockaddr_un  un;
          /* create a UNIX domain stream socket */
          if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
          return(-1);
          unlink(name);   /* in case it already exists */
          /* fill in socket address structure */
          memset(&un, 0, sizeof(un));
          un.sun_family = AF_UNIX;
          strcpy(un.sun_path, name);
          len = offsetof(struct sockaddr_un, sun_path) + strlen(name);
          /* bind the name to the descriptor */
          if (bind(fd, (struct sockaddr *)&un, len) < 0) {
          rval = -2;
          goto errout;
          }
          if (listen(fd, QLEN) < 0) { /* tell kernel we're a server */
          rval = -3;
          goto errout;
          }
          return(fd);
          errout:
          err = errno;
          close(fd);
          errno = err;
          return(rval);
          }

          以下是服務(wù)器的accept模塊,通過accept得到客戶端地址也應(yīng)該是一個socket文件,如果不是socket文件就返回錯誤碼,如果是socket文件,在建立連接后這個文件就沒有用了,調(diào)用unlink把它刪掉,通過傳出參數(shù)uidptr返回客戶端程序的user id。

          #include <stddef.h>
          #include <sys/stat.h>
          #include <sys/socket.h>
          #include <sys/un.h>
          #include <errno.h>
          int serv_accept(int listenfd, uid_t *uidptr)
          {
          int                 clifd, len, err, rval;
          time_t              staletime;
          struct sockaddr_un  un;
          struct stat         statbuf;
          len = sizeof(un);
          if ((clifd = accept(listenfd, (struct sockaddr *)&un, &len)) < 0)
          return(-1);     /* often errno=EINTR, if signal caught */
          /* obtain the client's uid from its calling address */
          len -= offsetof(struct sockaddr_un, sun_path); /* len of pathname */
          un.sun_path[len] = 0;           /* null terminate */
          if (stat(un.sun_path, &statbuf) < 0) {
          rval = -2;
          goto errout;
          }
          if (S_ISSOCK(statbuf.st_mode) == 0) {
          rval = -3;      /* not a socket */
          goto errout;
          }
          if (uidptr != NULL)
          *uidptr = statbuf.st_uid;   /* return uid of caller */
          unlink(un.sun_path);        /* we're done with pathname now */
          return(clifd);
          errout:
          err = errno;
          close(clifd);
          errno = err;
          return(rval);
          }

          以下是客戶端的connect模塊,與網(wǎng)絡(luò)socket編程不同的是,UNIX Domain Socket客戶端一般要顯式調(diào)用bind函數(shù),而不依賴系統(tǒng)自動分配的地址。客戶端bind一個自己指定的socket文件名的好處是,該文件名可以包含客戶端的pid以便服務(wù)器區(qū)分不同的客戶端。

          #include <stdio.h>
          #include <stddef.h>
          #include <sys/stat.h>
          #include <sys/socket.h>
          #include <sys/un.h>
          #include <errno.h>
          #define CLI_PATH    "/var/tmp/"      /* +5 for pid = 14 chars */
          /*
          * Create a client endpoint and connect to a server.
          * Returns fd if all OK, <0 on error.
          */
          int cli_conn(const char *name)
          {
          int                fd, len, err, rval;
          struct sockaddr_un un;
          /* create a UNIX domain stream socket */
          if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
          return(-1);
          /* fill socket address structure with our address */
          memset(&un, 0, sizeof(un));
          un.sun_family = AF_UNIX;
          sprintf(un.sun_path, "%s%05d", CLI_PATH, getpid());
          len = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path);
          unlink(un.sun_path);        /* in case it already exists */
          if (bind(fd, (struct sockaddr *)&un, len) < 0) {
          rval = -2;
          goto errout;
          }
          /* fill socket address structure with server's address */
          memset(&un, 0, sizeof(un));
          un.sun_family = AF_UNIX;
          strcpy(un.sun_path, name);
          len = offsetof(struct sockaddr_un, sun_path) + strlen(name);
          if (connect(fd, (struct sockaddr *)&un, len) < 0) {
          rval = -4;
          goto errout;
          }
          return(fd);
          errout:
          err = errno;
          close(fd);
          errno = err;
          return(rval);
          }
          posted on 2011-02-21 19:53 ivaneeo 閱讀(539) 評論(0)  編輯  收藏 所屬分類: GNU牛力
          主站蜘蛛池模板: 平武县| 高雄县| 无为县| 灵山县| 庆元县| 天镇县| 潮州市| 金沙县| 章丘市| 贵阳市| 托克逊县| 阳东县| 沭阳县| 中牟县| 潼南县| 牟定县| 武隆县| 六盘水市| 铜梁县| 济阳县| 瑞安市| 喀什市| 平泉县| 易门县| 霍邱县| 新营市| 平阴县| 黎城县| 琼海市| 兴和县| 修武县| 元氏县| 澄城县| 大石桥市| 曲松县| 高阳县| 新蔡县| 平江县| 雷山县| 大方县| 綦江县|