Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5445
  • 博文数量: 1
  • 博客积分: 214
  • 博客等级: 二等列兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-24 23:15
文章分类
文章存档

2011年(1)

我的朋友
最近访客

分类: C/C++

2011-07-04 16:53:02

client.c
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>

  7. #define SERV_PORT 8930
  8. #define MAX_LINE 1024

  9. int main(int argc, char** argv)
  10. {
  11.     int sockfd;
  12.     struct sockaddr_in servaddr;
  13.     char sendline[MAX_LINE];
  14.     int sendlen;
  15.     char* ptr;
  16.     int rv;
  17.     int read_cnt;
  18.     char read_buff[MAX_LINE];
  19.     errno = 0;

  20.     if (argc < 2)
  21.     {
  22.         return 0;
  23.     }

  24.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  25.     memset(&servaddr, 0 ,sizeof(servaddr));
  26.     servaddr.sin_family = AF_INET;
  27.     servaddr.sin_port = htons(SERV_PORT);
  28.     inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
  29.     
  30.     rv = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  31.     if (rv != 0)
  32.     {
  33.         printf("connect() errno: %d, strerror: %s\n", errno, strerror(errno));
  34.         close(sockfd);
  35.         return 0;
  36.     }
  37.     
  38.     while (fgets(sendline, MAX_LINE, stdin) != NULL)
  39.     {
  40.         sendlen = strlen(sendline);
  41.         ptr = sendline;
  42.         while (sendlen > 0)
  43.         {
  44.             if ((rv = write(sockfd, ptr, sendlen)) <= 0)
  45.             {
  46.                 if (errno == EINTR)
  47.                 {
  48.                     rv = 0;
  49.                 }
  50.                 else
  51.                 {
  52.                     printf("write() errno: %d, strerror: %s\n", errno, strerror(errno));
  53.                     close(sockfd);
  54.                     return 0;
  55.                 }
  56.             }
  57.             sendlen -= rv;
  58.             ptr = rv;
  59.         }

  60.         memset(read_buff, 0, sizeof(read_buff));
  61. again_read:
  62.         if ((read_cnt = read(sockfd, read_buff, sizeof(read_buff))) < 0)
  63.         {
  64.             if (errno == EINTR)
  65.             {
  66.                 goto again_read;
  67.             }
  68.             printf("read() errno: %d, strerror: %s\n", errno, strerror(errno));
  69.             close(sockfd);
  70.             exit(0);
  71.         }
  72.         else if (read_cnt == 0) //close by peer
  73.         {
  74.             printf("close connection by peer\n");
  75.             close(sockfd);
  76.             exit(0);
  77.         }

  78.         printf("receive from server: %s\n", read_buff);
  79.     }
  80. }
server.c
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>

  7. #define SERV_PORT 8930
  8. #define MAX_BUFF 1024

  9. int main(int argc, char** argv)
  10. {
  11.     int listenfd;
  12.     int connfd;
  13.     pid_t childpid;
  14.     socklen_t clilen;
  15.     struct sockaddr_in cliaddr;
  16.     struct sockaddr_in servaddr;
  17.     char clientip[16];
  18.     int rv;
  19.     int i;
  20.     char read_buff[MAX_BUFF];
  21.     int read_cnt;
  22.     int write_len;
  23.     char *ptr;
  24.     errno = 0;

  25.     listenfd = socket(AF_INET, SOCK_STREAM, 0);
  26.     if (listenfd < 0)
  27.     {
  28.         printf("socket() errno: %d, strerror: %s\n", errno, strerror(errno));
  29.         return 0;
  30.     }

  31.     memset(&servaddr, 0, sizeof(servaddr));
  32.     servaddr.sin_family = AF_INET;
  33.     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  34.     servaddr.sin_port = htons(SERV_PORT);
  35.     
  36.     rv = bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  37.     if (0 != rv)
  38.     {
  39.         printf("bind() errno: %d, strerror: %s\n", errno, strerror(errno));
  40.         close(listenfd);
  41.         return 0;
  42.     }
  43.     rv = listen(listenfd, 5);
  44.     if (0 != rv)
  45.     {
  46.         printf("listen() errno: %d, strerror: %s\n", errno, strerror(errno));
  47.         close(listenfd);
  48.         return 0;
  49.     }

  50.     while(1)
  51.     {
  52.         clilen = sizeof(cliaddr);
  53.         connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
  54.         if ((childpid = fork()) == 0) //child process
  55.         {
  56.             if (connfd < 0)
  57.             {
  58.                 printf("accept() errno: %d, strerror: %s\n", errno, strerror(errno));
  59.                 close(listenfd);
  60.                 return 0;
  61.            }
  62.             close(listenfd);

  63. again_read:
  64.             if ((read_cnt = read(connfd, read_buff, sizeof(read_buff))) < 0)
  65.             {
  66.                 if (errno == EINTR)
  67.                 {
  68.                     goto again_read;
  69.                 }
  70.                 printf("read() errno: %d, strerror: %s\n", errno, strerror(errno));
  71.                 close(connfd);
  72.                 exit(0);
  73.             }
  74.             else if (read_cnt == 0) //close by peer
  75.             {
  76.                 printf("close connection by peer\n");
  77.                 close(connfd);
  78.                 exit(0);
  79.             }

  80.             for (i = 0; i < read_cnt; i )
  81.             {
  82.                 if (read_buff[i] == '\n' || read_buff[i] == 0)
  83.                 {
  84.                     read_buff[i] = 0;
  85.                     i ;
  86.                     break;
  87.                 }
  88.             }
  89.             write_len = i;

  90.             inet_ntop(AF_INET, &(cliaddr.sin_addr.s_addr), clientip, sizeof(clientip));
  91.             printf("client ip: %s, client port: %d, receive mesage: %s\n", clientip, cliaddr.sin_port, read_buff);

  92.             ptr = read_buff;
  93.             while (write_len > 0)
  94.             {
  95.                 if ((rv = write(connfd, ptr, write_len)) <= 0)
  96.                 {
  97.                     if (errno == EINTR)
  98.                     {
  99.                         rv = 0;
  100.                     }
  101.                     else
  102.                     {
  103.                         printf("write() errno: %d, strerror: %s\n", errno, strerror(errno));
  104.                         close(connfd);
  105.                         exit(0);
  106.                     }
  107.                 }
  108.                 write_len -= rv;
  109.                 ptr = rv;
  110.             }

  111.             exit(0);
  112.         }
  113.         close(connfd);
  114.     }
  115. }
阅读(582) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

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