Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1775855
  • 博文数量: 198
  • 博客积分: 4088
  • 博客等级: 上校
  • 技术积分: 2391
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-15 16:29
个人简介

游戏开发,系统架构; 博客迁移到:http://www.jianshu.com/u/3ac0504b3b8c

文章分类

全部博文(198)

文章存档

2017年(1)

2016年(12)

2015年(1)

2014年(3)

2013年(13)

2012年(18)

2011年(150)

分类: LINUX

2011-08-24 09:10:36

服务器端:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<errno.h>
  4. #include<string.h>
  5. #include<sys/types.h>
  6. #include<sys/wait.h>
  7. #include<sys/socket.h>
  8. #include<netinet/in.h>
  9. #include<arpa/inet.h>
  10. #include<iostream>

  11. using namespace std;

  12. #define PORT 4002
  13. #define BACKLOG 5

  14. int main()
  15. {
  16.     int listensock, clientsock;
  17.     struct sockaddr_in local_addr;
  18.     struct sockaddr_in client_addr;

  19.     char buf[1024];

  20.     if((listensock = socket(AF_INET,SOCK_STREAM, 0)) == -1)
  21.     {
  22.         cout << "socket create error" << endl;
  23.         exit(1);
  24.     }

  25.     local_addr.sin_family = AF_INET;
  26.     local_addr.sin_port = htons(PORT);
  27.     local_addr.sin_addr.s_addr = INADDR_ANY;
  28.     bzero(&(local_addr.sin_zero), 8);

  29.     if(bind(listensock, (struct sockaddr*)&local_addr, sizeof(struct sockaddr)) == -1)
  30.     {
  31.         cout << "bind error" << endl;
  32.         exit(1);
  33.     }
  34.     else
  35.     {
  36.         cout << "bind success" << endl;
  37.     }

  38.     if(listen(listensock, BACKLOG) == -1)
  39.     {
  40.         cout << "listen error" << endl;
  41.         exit(1);
  42.     }
  43.     else
  44.     {
  45.         cout << "listen success" << endl;
  46.     }

  47.     int addrlen = sizeof(struct sockaddr_in);

  48.     if((clientsock = accept(listensock, (struct sockaddr*)&client_addr,(socklen_t*)&addrlen)) == -1)
  49.     {
  50.         cout << "accept error" << endl;
  51.         exit(1);
  52.     }
  53.     else
  54.     {
  55.         cout << "accept success" << endl;
  56.     }

  57.     cout << "receive a connetion from Ip: " << inet_ntoa(client_addr.sin_addr) << " Port:" << ntohs(client_addr.sin_port) << endl;

  58.     cout << "prepare send data" << endl;

  59.     while(1)
  60.     {
  61.         cin >> buf;
  62.         int sendlen = strlen(buf);
  63.         if((send(clientsock, buf, sendlen, 0)) == -1)
  64.         {
  65.             cout << "send data error" << endl;
  66.         }
  67.     }

  68.     close(clientsock);

  69. }

客户端:

 

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

  11. using namespace std;

  12. #define PORT 4002

  13. int main()
  14. {
  15.     int sockfd;
  16.     struct sockaddr_in serv_addr;

  17.     char buf[1024];
  18.     int recvlen;

  19.     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  20.     {
  21.         cout << "socket create error" << endl;
  22.         exit(1);
  23.     }

  24.     serv_addr.sin_family = AF_INET;
  25.     serv_addr.sin_port = htons(PORT);
  26.     serv_addr.sin_addr.s_addr = inet_addr("10.0.30.66");
  27.     bzero(&(serv_addr.sin_zero), 8);

  28.     if((connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr))) == -1)
  29.     {
  30.         cout << "connect error" << endl;
  31.         exit(1);
  32.     }

  33.     cout << "prepare recv data" << endl;

  34.     while(1)
  35.     {

  36.         if((recvlen = recv(sockfd, buf, sizeof(buf), 0)) == -1)
  37.         {
  38.             cout << "recv error" << endl;
  39.             exit(1);
  40.         }

  41.         buf[recvlen] = '\0';
  42.         cout << "received: " << buf << endl;
  43.     }

  44.     close(sockfd);
  45. }
阅读(2036) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~