Chinaunix首页 | 论坛 | 博客
  • 博客访问: 133606
  • 博文数量: 43
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 175
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-10 12:33
文章存档

2016年(9)

2015年(34)

我的朋友

分类: LINUX

2015-12-05 12:49:51

花了点时间写了个线程版的回射服务器,下面记录代码和遇到的问题和解决的办法

点击(此处)折叠或打开

  1. /*************************************************************************
  2.     > File Name: echo_serv.c
  3.     > Author: gwjiaoj
  4.     > Mail: jiaojinawei@163.com
  5.     > Created Time: Fri 27 Nov 2015 02:15:38 GMT
  6.  ************************************************************************/
  7. #include <pthread.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #include <sys/socket.h>
  13. #include <sys/types.h>
  14. #include <netinet/in.h>
  15. #include <string.h>
  16. #include <strings.h>

  17. #define ERR_EXIT(exp) do\
  18.     {\
  19.         perror(exp);\
  20.         exit(EXIT_FAILURE);\
  21.     } while (0)

  22. void * do_it(void *pconnfd)
  23. {
  24.     int recvbuf[1500] = { 0 };
  25.     int connfd = (int)pconnfd;
  26.     pthread_detach(pthread_self());
  27.     printf("%d\n", connfd);
  28.     for( ; ; )
  29.     {
  30.         memset(recvbuf, 0, sizeof(recvbuf));
  31.         if(read(connfd, recvbuf, sizeof(recvbuf)) < 0)
  32.         {
  33.             ERR_EXIT("read");
  34.         }

  35.         printf("%s\n", recvbuf);

  36.         if(write(connfd, recvbuf, strlen(recvbuf)) < 0)
  37.         {
  38.             ERR_EXIT("write");
  39.         }
  40.     }

  41.     close(connfd);

  42.     return;
  43. }
  44. int main(int argc, void **argv)
  45. {
  46.     pthread_t tid = 0;
  47.     int listenfd = 0;
  48.     int connfd = 0;
  49.     struct sockaddr_in serveraddr = { 0 };
  50.     struct sockaddr_in peeraddr = { 0 };
  51.     char buff[1024] = { 0 };
  52.     socklen_t socklen = 0;

  53.     if((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
  54.     {
  55.         ERR_EXIT("socket");
  56.     }

  57.     memset(&serveraddr, 0, sizeof(serveraddr));
  58.     serveraddr.sin_family = AF_INET;
  59.     serveraddr.sin_port = htons(5188);
  60.     serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);

  61.     if(bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
  62.     {
  63.         ERR_EXIT("bind");
  64.     }

  65.     if(listen(listenfd, SOMAXCONN) < 0)
  66.     {
  67.         ERR_EXIT("listen");
  68.     }

  69.     while(1)
  70.     {
  71.         memset(&peeraddr, 0, sizeof(peeraddr));
  72.         socklen = sizeof(peeraddr);

  73.         if((connfd = accept(listenfd, (struct sockaddr *)&peeraddr, &socklen)) < 0)
  74.         {
  75.             ERR_EXIT("accept");
  76.         }
  77.         
  78.         inet_ntop(AF_INET, &peeraddr.sin_addr, buff, sizeof(buff));
  79.         printf("%d\n", connfd);
  80.         printf("ip: %s, port: %d\n", buff, peeraddr.sin_port);
  81.         if(0 != pthread_create(&tid, NULL, do_it, (void *)connfd))
  82.         {
  83.             ERR_EXIT("pthread");
  84.         }
  85.     }

  86.     return 0;
  87. }


点击(此处)折叠或打开

  1. /*************************************************************************
  2.     > File Name: echo_cli.c
  3.     > Author: gwjiaoj
  4.     > Mail: jiaojinawei@163.com
  5.     > Created Time: Fri 27 Nov 2015 02:15:38 GMT
  6.  ************************************************************************/
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <sys/socket.h>
  12. #include <sys/types.h>
  13. #include <netinet/in.h>
  14. #include <string.h>
  15. #include <strings.h>

  16. #define ERR_EXIT(exp) do\
  17.     {\
  18.         perror(exp);\
  19.         exit(EXIT_FAILURE);\
  20.     } while (0)

  21. int main(int argc, void **argv)
  22. {
  23.     int localfd = 0;
  24.     struct sockaddr_in serveraddr = { 0 };
  25.     struct sockaddr_in localaddr = { 0 };
  26.     socklen_t addrlen = 0;
  27.     char recvbuf[1024] = { 0 };
  28.     char sendbuf[1024] = { 0 };
  29.     char buff[1024] = { 0 };

  30.     if(2 != argc)
  31.     {
  32.         printf("usage: a.out \n");
  33.         return 0;
  34.     }

  35.     if((localfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
  36.     {
  37.         ERR_EXIT("socket error");
  38.     }

  39.     memset(&serveraddr, 0, sizeof(serveraddr));
  40.     serveraddr.sin_family = AF_INET;
  41.     serveraddr.sin_port = htons(5188);
  42.     serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
  43.     if(connect(localfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
  44.     {
  45.         ERR_EXIT("connect error");
  46.     }
  47.     memset(&localaddr, 0, sizeof(localaddr));
  48.     addrlen = sizeof(localaddr);
  49.     if(getsockname(localfd, (struct sockaddr *)&localaddr, &addrlen) < 0)
  50.     {
  51.         ERR_EXIT("getsockname error");
  52.     }

  53.     inet_ntop(AF_INET, &localaddr.sin_addr, buff, sizeof(buff));

  54.     printf("ip: %s, port %d\n", buff,
  55.              ntohs(localaddr.sin_port));

  56.     while(1)
  57.     {
  58.         if(fgets(sendbuf, sizeof(sendbuf), stdin) == NULL)
  59.         {
  60.             ERR_EXIT("fgets error");
  61.         }

  62.         if(write(localfd, sendbuf, strlen(sendbuf)) < 0)
  63.         {
  64.             ERR_EXIT("write error");
  65.         }

  66.         if(read(localfd, recvbuf, sizeof(recvbuf)) < 0)
  67.         {
  68.             ERR_EXIT("read error");
  69.         }

  70.         if(fputs(recvbuf, stdout) == EOF)
  71.         {
  72.             ERR_EXIT("fputs error");
  73.         }
  74.     }

  75.     return 0;
  76. }



在使用线程时,使用gcc或arm-linux-gcc编译时,会出现错误:undefined reference to 'pthread_create'
主要是以下两种原因:
1、#include  请确认头文件是否添加
2、-lpthread 编译选项,即在编译时需添加额外的编译选项,如使用gcc编译echo_serv.c文件,命令正确应该如下:
    gcc echo_serv.c -o echo_serv  -lpthread
    请注意后面的-lpthread,特别是-后边的'l',link = 'l'
阅读(1210) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~