Chinaunix首页 | 论坛 | 博客
  • 博客访问: 398934
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-03 18:24
文章分类

全部博文(75)

文章存档

2019年(1)

2018年(20)

2017年(14)

2016年(10)

2015年(30)

分类: LINUX

2016-05-29 21:37:59

server端程序

点击(此处)折叠或打开

  1. #include "stdio.h"
  2. #include "string.h"
  3. #include "sys/types.h"
  4. #include "sys/socket.h"
  5. #include "netinet/in.h"
  6. #include "arpa/inet.h"

  7. #define _BUF_SIZE_ 1024

  8. int main(int argc,char **argv)
  9. {
  10.     int ret_val = -1;
  11.     int sock_fd = -1;
  12.     int conn_fd = -1;
  13.     unsigned char recv_buf[_BUF_SIZE_];
  14.     struct sockaddr_in server_addr,client_addr;
  15.     socklen_t addr_len = -1;

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

  21.     /**/
  22.     sock_fd = socket(AF_INET,SOCK_STREAM,0);
  23.     if(sock_fd < 0)
  24.     {
  25.         printf("socket Failedn");
  26.         return -1;
  27.     }

  28.     /**/
  29.     memset(&server_addr,0,sizeof(struct sockaddr_in));
  30.     server_addr.sin_family = AF_INET;
  31.     server_addr.sin_port = htons(atoi(argv[1]));
  32.     server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

  33.     addr_len = sizeof(struct sockaddr_in);
  34.     ret_val = bind(sock_fd,(struct sockaddr *)&server_addr,addr_len);
  35.     if(ret_val < 0)
  36.     {
  37.         printf("bind Failed\n");
  38.         goto _OVER_;
  39.     }

  40.     /**/
  41.     ret_val = listen(sock_fd,2);
  42.     if(ret_val < 0)
  43.     {
  44.         printf("listen Failed\n");
  45.         goto _OVER_;
  46.     }

  47.     /**/
  48.     conn_fd = accept(sock_fd,(struct sockaddr *)&client_addr,&addr_len);
  49.     if(conn_fd < 0)
  50.     {
  51.         printf("accept Failed\n");
  52.         goto _OVER_;
  53.     }
  54.     printf("conn_fd:%d ip:%s port:%d\n",conn_fd,
  55.                 inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));

  56.     /**/
  57.     while(1)
  58.     {
  59.         /**/
  60.         memset(recv_buf,0,_BUF_SIZE_);
  61.         ret_val = recv(conn_fd,(void *)recv_buf,_BUF_SIZE_,0);
  62.         if(ret_val < 0)
  63.         {
  64.             printf("recv Failed\n");
  65.             goto _OVER_;
  66.         }
  67.         else if(ret_val == 0)
  68.         {
  69.             printf("peer is closed\n");
  70.             goto _OVER_;
  71.         }
  72.         else
  73.         {
  74.             printf("recv_buf:%s\n",recv_buf);
  75.         }
  76.     }

  77. _OVER_:
  78.     if(sock_fd > 0)
  79.     {
  80.         close(sock_fd);
  81.     }
  82.     if(conn_fd > 0)
  83.     {
  84.         close(conn_fd);
  85.     }
  86.     return ret_val;
client端程序

点击(此处)折叠或打开

  1. #include "stdio.h"
  2. #include "string.h"
  3. #include "sys/types.h"
  4. #include "sys/socket.h"
  5. #include "netinet/in.h"
  6. #include "arpa/inet.h"

  7. int main(int argc,char **argv)
  8. {
  9.     int ret_val = -1;
  10.     int sock_fd = -1;
  11.     socklen_t addrlen = -1;
  12.     struct sockaddr_in server_addr;
  13.     struct in_addr addr;
  14.     unsigned char sendbuf[10] = "123456789";

  15.     if(argc != 3)
  16.     {
  17.         printf("usage:%s ip port\n", argv[0]);
  18.         return -1;
  19.     }

  20.     /**/
  21.     sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  22.     if(sock_fd < 0)
  23.     {
  24.         printf("socket Failedn");
  25.         return -1;
  26.     }

  27.     /**/
  28.     inet_aton(argv[1], &addr);
  29.     /**/
  30.     memset(&server_addr,0,sizeof(struct sockaddr_in));
  31.     server_addr.sin_family = AF_INET;
  32.     server_addr.sin_port = htons(atoi(argv[2]));
  33.     server_addr.sin_addr = addr;
  34.     
  35.     addrlen = sizeof(struct sockaddr_in);
  36.     /**/
  37.     ret_val = connect(sock_fd, (struct sockaddr *)&server_addr, addrlen);
  38.     if(ret_val < 0)
  39.     {
  40.         printf("connect Failed\n");
  41.         goto _OVER_;
  42.     }

  43.     /**/
  44.     while(1)
  45.     {
  46.         ret_val = send(sock_fd, (void *)sendbuf, 10, 0);
  47.         if(ret_val < 0)
  48.         {
  49.             printf("send Failed\n");
  50.             goto _OVER_;
  51.         }

  52.         printf("sendbuf:%s\n",sendbuf);
  53.         sleep(1);
  54.     }

  55. _OVER_:
  56.     if(sock_fd > 0)
  57.     {
  58.         close(sock_fd);
  59.     }
  60.     return ret_val;

阅读(1589) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~