Chinaunix首页 | 论坛 | 博客
  • 博客访问: 446807
  • 博文数量: 89
  • 博客积分: 2713
  • 博客等级: 少校
  • 技术积分: 938
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-18 21:19
个人简介

为了成为自由自在的人而奋斗!

文章分类

全部博文(89)

文章存档

2016年(5)

2015年(9)

2014年(2)

2013年(10)

2012年(1)

2011年(30)

2010年(32)

分类: C/C++

2011-06-15 16:00:50

服务端:
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <sys/un.h>
  5. #include <sys/socket.h>
  6. #include <sys/unistd.h>
  7. #include <sys/select.h>
  8. #include <pthread.h>


  9. #define MAXSIZE (4096)

  10. static void pthread_proc(int *peer_ptr)
  11. {
  12.     char recvmsg[MAXSIZE], sendmsg[MAXSIZE];
  13.     int peer = *peer_ptr;
  14.     static int ii;
  15.     
  16.     printf("phread %d\n", ii++);
  17.     while (1) {
  18.         memset(recvmsg, 0, MAXSIZE);
  19.         memset(sendmsg, 0, MAXSIZE);
  20.         
  21.         if (read(peer, recvmsg, sizeof(recvmsg)) < 0) {
  22.             perror("recv err");
  23.             break;
  24.         }

  25.         printf("client: %s\n", recvmsg);
  26.         if (!strcmp(recvmsg, "quit")) {
  27.             break;
  28.         }
  29.         
  30.         strcpy(sendmsg, recvmsg);
  31.         strcat(sendmsg, " send back");
  32.         write(peer, sendmsg, strlen(sendmsg));
  33.     }
  34.     close(peer);

  35.     return;
  36. }

  37. int main(int argc, char *argv[])
  38. {
  39.     int sockfd, peer, len, ready;
  40.     fd_set fds;
  41.     struct sockaddr_un serv_addr;
  42.     pthread_t phd;
  43.         
  44.     sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  45.     if (sockfd < 0) {
  46.         perror("socket error");
  47.         return -1;
  48.     }

  49.     serv_addr.sun_family = AF_UNIX;
  50.     strcpy((char *)serv_addr.sun_path, "e:\\sockfile");
  51.     unlink("e:\\sockfile");
  52.     
  53.     if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
  54.         perror("bind err");
  55.         return -1;
  56.     }
  57.     
  58.     if (listen(sockfd, 1) < 0) {
  59.         perror("listen error");
  60.         close(sockfd);
  61.         return -1;
  62.     }

  63.     len = sizeof(serv_addr);
  64.     while (1) {
  65.         printf("begin to accept\n");
  66.         FD_ZERO(&fds);
  67.         FD_SET(sockfd, &fds);

  68.         ready = select(sockfd + 1, &fds, 0, 0, NULL);
  69.         if (ready > 0 && FD_ISSET(sockfd, &fds)) {
  70.             peer = accept(sockfd, (struct sockaddr *)&serv_addr, &len);
  71.      if (peer < 0) {
  72.      perror("Server: accept failed.\n");
  73.      close(sockfd);
  74.                 return -1;
  75.             }
  76.             if (pthread_create(&phd, NULL, (void *)pthread_proc, (void *)&peer) != 0) {
  77.                 close(peer);
  78.             }
  79.         }
  80.     }

  81.     return 0;
  82. }

客户端:

 

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <sys/un.h>
  5. #include <sys/socket.h>
  6. #include <sys/unistd.h>
  7. #include <sys/select.h>

  8. #define MAXSIZE (4096)

  9. int main(int argc, char *argv[])
  10. {
  11.     int sockfd, ret;
  12.     unsigned long opt;
  13.     struct sockaddr_un serv_addr;
  14.     char recvmsg[MAXSIZE], sendmsg[MAXSIZE];
  15.         
  16.     sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  17.     if (sockfd < 0) {
  18.         perror("socket error");
  19.         return -1;
  20.     }

  21.     memset(&serv_addr, 0, sizeof(serv_addr));
  22.     serv_addr.sun_family = AF_UNIX;
  23.     strcpy((char *)serv_addr.sun_path, "e:\\sockfile");

  24.     ret = 0;
  25.     if ((ret = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) < 0) {
  26.         perror("connect err");
  27.         return -1;
  28.     }
  29.     printf("connect to server success!\n");
  30.     
  31.     while (1) {
  32.         memset(sendmsg, 0, sizeof(sendmsg));
  33.         memset(recvmsg, 0, sizeof(recvmsg));

  34.         printf("client:\n");
  35.         gets(sendmsg);
  36.         
  37.         if (write(sockfd, sendmsg, strlen(sendmsg)) < 0) {
  38.             printf("send msg failed!\n");
  39.             break;
  40.         }

  41.         if (!strcmp(sendmsg, "quit")) {
  42.             break;
  43.         }
  44.         if (!strcmp(sendmsg, "quit server")) {
  45.             break;
  46.         }
  47.       
  48.         if (read(sockfd, recvmsg, MAXSIZE) < 0) {
  49.             printf("recv msg failed!\n");
  50.             break;
  51.         }

  52.         printf("server:\n%s\n", recvmsg);
  53.     }

  54.     printf("\nclose connection!\n\n");
  55.     close(sockfd);
  56.     
  57.     return 0;
  58. }
阅读(1116) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~