加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

AF_LOCAL和AF_INET两个套接口同时监听

(2013-02-28 16:38:17)
标签:

af_local

af_inet

分类: 网络编程
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <netdb.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <sys/types.h>
  10. #include <arpa/inet.h>

  11. #define SA struct sockaddr

  12. int main(int argc, char **argv)
  13. {
  14.     int use_tcp = 1;
  15.     int use_unix = 1;

  16.     int fdmax = 0;

  17.     int rt = 0;
  18.     int fdtcp, fdunix;

  19.     if (!getenv("USEUNIX"))
  20.         use_unix = 0;
  21.     if (!getenv("USETCP"))
  22.         use_tcp = 0;

  23.     if (use_tcp) {
  24.         // Enable TCP server socket
  25.         fdtcp = socket(AF_INET, SOCK_STREAM, 0);
  26.         if (fdtcp < 0) {
  27.             perror("socket( TCP )");
  28.         }
  29.         fdmax = fdmax > fdtcp ? fdmax : fdtcp;
  30.         struct sockaddr_in servaddr;
  31.         bzero(&servaddr, sizeof(servaddr));
  32.         servaddr.sin_family = AF_INET;
  33.         servaddr.sin_port = htons(1235);
  34.         int reuseaddr = 1;
  35.         socklen_t reuseaddr_len = sizeof(reuseaddr);
  36.         rt = setsockopt(fdtcp, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
  37.                         reuseaddr_len);
  38.         rt = bind(fdtcp, (SA *) & servaddr, sizeof(servaddr));
  39.         if (rt < 0) {
  40.             perror("bind( TCP )");
  41.             exit(1);
  42.         }
  43.         listen(fdtcp, 20);
  44.     }

  45.     if (use_unix) {
  46.         // Get ready the UNIX Domain Socket
  47.         fdunix = socket(AF_UNIX, SOCK_STREAM, 0);
  48.         if (fdunix < 0) {
  49.             perror("socket( UNIX )");
  50.             exit(1);
  51.         }
  52.         fdmax = fdmax > fdunix ? fdmax : fdunix;
  53.         struct sockaddr_un unixaddr;
  54.         bzero(&unixaddr, sizeof(unixaddr));
  55.         unixaddr.sun_family = AF_UNIX;
  56.         strncpy(&unixaddr.sun_path[0], "/tmp/test-unix.0", 17);
  57.         rt = bind(fdunix, (SA *) & unixaddr, sizeof(unixaddr));
  58.         //unlink( "/tmp/test-unix.0" );
  59.         if (rt < 0) {
  60.             perror("bind( UNIX )");
  61.             exit(1);
  62.         }

  63.         rt = listen(fdunix, 20);
  64.         if (rt < 0) {
  65.             perror("listen( UNIX )");
  66.             exit(1);
  67.         }
  68.     }

  69.     int selectrt = 0;
  70.     while (use_tcp || use_unix) {
  71.         fd_set ssset;
  72.         FD_ZERO(&ssset);
  73.         if (use_tcp)
  74.             FD_SET(fdtcp, &ssset);
  75.         if (use_unix)
  76.             FD_SET(fdunix, &ssset);

  77.         selectrt = select(fdmax + 1, &ssset, NULL, NULL, NULL);
  78.         while (selectrt > 0) {
  79.             if (use_tcp) {
  80.                 if (FD_ISSET(fdtcp, &ssset)) {
  81.                     printf("TCP Server is ready.\n");
  82.                     struct sockaddr_in caddr;
  83.                     bzero(&caddr, sizeof(caddr));
  84.                     socklen_t clen = sizeof(caddr);
  85.                     int client = accept(fdtcp, (SA *) & caddr, &clen);
  86.                     write(client, "HELO", 5);
  87.                     close(client);
  88.                     --selectrt;
  89.                 }
  90.             }
  91.             if (use_unix) {
  92.                 if (FD_ISSET(fdunix, &ssset)) {
  93.                     printf("UNIX Server is ready.\n");
  94.                     struct sockaddr_un caddr;
  95.                     bzero(&caddr, sizeof(caddr));
  96.                     socklen_t clen = sizeof(caddr);
  97.                     int client = accept(fdunix, (SA *) & caddr, &clen);
  98.                     rt = write(client, "HELO", 5);
  99.                     printf("UNIX Socket wrote %d bytes off.\n", rt);
  100.                     close(client);
  101.                     --selectrt;
  102.                 }
  103.             }
  104.         }
  105.         if ( selectrt < 0 ) {
  106.           perror( "select()" );
  107.           exit( 1 );
  108.         }
  109.         FD_ZERO(&ssset);
  110.     }
  111. }


  1. $ make sockd
  2. cc     sockd.c   -o sockd
  3. $ env USETCP='' USEUNIX='' ./sockd
  4. UNIX Server is ready.
  5. UNIX Socket wrote 5 bytes off.
  6. UNIX Server is ready.
  7. UNIX Socket wrote 5 bytes off.
  8. TCP Server is ready.
另外一边
  1. $ ./sockclient /tmp/test-unix.0
  2. UNIX Socket read 5 bytes.
  3. HELO
  4. $ ./sockclient /tmp/test-unix.0
  5. UNIX Socket read 5 bytes.
  6. HELO
  7. $ nc localhost 1235 -vvv
  8. gydebian [127.0.0.1] 1235 (?) open
  9. HELO sent 0, rcvd 5

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有