Chinaunix首页 | 论坛 | 博客
  • 博客访问: 204055
  • 博文数量: 18
  • 博客积分: 1579
  • 博客等级: 上尉
  • 技术积分: 238
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-05 17:33
个人简介

运维运维

文章分类

全部博文(18)

文章存档

2016年(1)

2015年(3)

2014年(5)

2013年(1)

2011年(3)

2010年(5)

我的朋友

分类: C/C++

2016-04-14 10:18:43

1、server端server.c

点击(此处)折叠或打开

  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <time.h>
  11. int main(int argc, char *argv[])
  12. {
  13.     int listenfd = 0, connfd = 0;
  14.     struct sockaddr_in serv_addr;

  15.     char sendBuff[1025];
  16.     time_t ticks;
  17.     listenfd = socket(AF_INET, SOCK_STREAM, 0);
  18.     memset(&serv_addr, '0', sizeof(serv_addr));
  19.     memset(sendBuff, '0', sizeof(sendBuff));

  20.     serv_addr.sin_family = AF_INET;
  21.     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  22.     serv_addr.sin_port = htons(5000);

  23.     bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

  24.     listen(listenfd, 10);

  25.     while(1)
  26.     {
  27.         connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
  28.         ticks = time(NULL);
  29.     FILE *pf = popen("ip add list", "r");
  30.     char res[1024];
  31.     fread(res, 1024, 1, pf);
  32.         snprintf(sendBuff, sizeof(sendBuff),res );
  33.         write(connfd, sendBuff, strlen(sendBuff));
  34.         sleep(1);
  35.      }
  36. }
2、server端client.c

点击(此处)折叠或打开

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

  11. int main(int argc, char *argv[])
  12. {
  13.     int sockfd = 0, n = 0;
  14.     char recvBuff[1024];
  15.     struct sockaddr_in serv_addr;

  16.     if(argc != 2)
  17.     {
  18.         printf("\n Usage: %s \n",argv[0]);
  19.         return 1;
  20.     }

  21.     memset(recvBuff, '0',sizeof(recvBuff));
  22.     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  23.     {
  24.         printf("\n Error : Could not create socket \n");
  25.         return 1;
  26.     }

  27.     memset(&serv_addr, '0', sizeof(serv_addr));

  28.     serv_addr.sin_family = AF_INET;
  29.     serv_addr.sin_port = htons(5000);

  30.     if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
  31.     {
  32.         printf("\n inet_pton error occured\n");
  33.         return 1;
  34.     }

  35.     if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  36.     {
  37.        printf("\n Error : Connect Failed \n");
  38.        return 1;
  39.     }
  40.     while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
  41.     {
  42.         recvBuff[n] = 0;
  43.         if(fputs(recvBuff, stdout) == EOF)
  44.        {
  45.             printf("\n Error : Fputs error\n");
  46.         }
  47.     }

  48.     if(n < 0)
  49.     {
  50.         printf("\n Read error \n");
  51.     }

  52.     return 0;
  53. }



阅读(1656) | 评论(0) | 转发(0) |
0

上一篇:http 408 504

下一篇:没有了

给主人留下些什么吧!~~