Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1774514
  • 博文数量: 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)

分类: WINDOWS

2012-08-07 15:13:51

  1. #include <Windows.h>
  2. #include <iostream>

  3. #pragma comment(lib, "ws2_32.lib")
  4. using namespace std;

  5. class WSAInit
  6. {
  7. public:
  8.     WSAInit()
  9.     {
  10.         WSADATA swaData;
  11.         WSAStartup(MAKEWORD(2, 2), &swaData);
  12.     }

  13.     ~WSAInit()
  14.     {
  15.         WSACleanup();
  16.     }
  17. }WSInit;

  18. const int port = 3002;

  19. int main()
  20. {
  21.     int i = 0;
  22.     cin >> i;
  23.     if(i)
  24.     {
  25.         // server

  26.         SOCKET client_socket = INVALID_SOCKET;
  27.         SOCKET server_socket = socket(AF_INET, SOCK_STREAM, 0);
  28.         
  29.         if(server_socket == INVALID_SOCKET)
  30.         {
  31.             cout << "create server socket error" << endl;
  32.             exit(0);
  33.         }

  34.         sockaddr_in serveraddr = {0};
  35.         sockaddr_in clientaddr = {0};

  36.         serveraddr.sin_family = AF_INET;
  37.         serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
  38.         serveraddr.sin_port = htons(port);

  39.         if(bind(server_socket, (sockaddr*)&serveraddr, sizeof(sockaddr_in)) == SOCKET_ERROR)
  40.         {
  41.             cout << "bind error" << endl;
  42.             exit(0);
  43.         }

  44.         if(listen(server_socket, 5) == SOCKET_ERROR)
  45.         {
  46.             cout << "listen error" << endl;
  47.             exit(0);
  48.         }

  49.         cout << "listen socket at " << port << endl;

  50.         int addr_len = sizeof(sockaddr_in);
  51.         if( (client_socket = accept(server_socket, (sockaddr*)&clientaddr, &addr_len)) == SOCKET_ERROR)
  52.         {
  53.             cout << "accept connect error" << endl;
  54.             exit(0);
  55.         }

  56.         cout << "accept a connection from " << inet_ntoa(clientaddr.sin_addr) << ":" << ntohs(clientaddr.sin_port) << endl;

  57.         char recvbuffer[1024] = {0};
  58.         while(true)
  59.         {
  60.             memset(recvbuffer, 0, sizeof(recvbuffer));
  61.             int ret = recv(client_socket, recvbuffer, sizeof(recvbuffer), 0);
  62.             if(ret == 0 || ret == SOCKET_ERROR)
  63.             {
  64.                 cout << "client socket closed!" << endl;
  65.                 break;
  66.             }
  67.             cout << "recv: " << recvbuffer << endl;
  68.         }

  69.         closesocket(server_socket);
  70.         closesocket(client_socket);

  71.     }
  72.     else
  73.     {
  74.         // client

  75.         SOCKET client_socket = socket(AF_INET, SOCK_STREAM, 0);
  76.         if(client_socket == INVALID_SOCKET)
  77.         {
  78.             cout << "create client socket error" << endl;
  79.             exit(0);
  80.         }

  81.         sockaddr_in serveraddr = {0};
  82.         serveraddr.sin_family = AF_INET;
  83.         serveraddr.sin_addr.S_un.S_addr = inet_addr("10.0.30.37");
  84.         serveraddr.sin_port= htons(port);

  85.         if(connect(client_socket, (sockaddr*)&serveraddr, sizeof(sockaddr_in)) == SOCKET_ERROR)
  86.         {
  87.             cout << "connect failed" << endl;
  88.             exit(0);
  89.         }

  90.         cout << "connect successfully" << endl;

  91.         char sendbuffer[1024] = {0};
  92.         while(true)
  93.         {
  94.             cin >> sendbuffer;
  95.             int ret = send(client_socket, sendbuffer, strlen(sendbuffer), 0);
  96.             if(ret == 0 || ret == SOCKET_ERROR)
  97.             {
  98.                 cout << "server closed" << endl;
  99.                 break;
  100.             }
  101.         }

  102.         closesocket(client_socket);
  103.     }

  104.     system("pause");
  105.     return 0;
  106. }

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