Chinaunix首页 | 论坛 | 博客
  • 博客访问: 170034
  • 博文数量: 25
  • 博客积分: 548
  • 博客等级: 中士
  • 技术积分: 229
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-11 18:48
文章分类

全部博文(25)

文章存档

2012年(2)

2011年(23)

分类: C/C++

2011-07-14 16:35:44

Server:
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <Winsock2.h>
  4. #include <windows.h>
  5. #include <string>

  6. #include "msg.pb.h"

  7. #pragma comment(lib,"ws2_32.lib")
  8. #pragma commen (lib,"libprotobuf.lib")

  9. using namespace std;
  10. int _tmain(int argc, _TCHAR* argv[])
  11. {
  12.     WORD wVersionRequested;
  13.     WSADATA wsaData;
  14.     int err;

  15.     wVersionRequested = MAKEWORD(1,1);
  16.     err = WSAStartup(wVersionRequested,&wsaData);
  17.     if ( err != 0)
  18.         return 0;
  19.     
  20.     if ( LOBYTE( wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
  21.     {
  22.         WSACleanup();
  23.         return 0;
  24.     }
  25.     
  26.     SOCKET sockSrv = socket(AF_INET,SOCK_STREAM,0);

  27.     SOCKADDR_IN addrSrv;
  28.     addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
  29.     addrSrv.sin_family = AF_INET;
  30.     addrSrv.sin_port = htons(8000);

  31.     bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
  32.     printf("Bind OK...\n");

  33.     listen(sockSrv,5);
  34.     printf("Listen OK ...\n");

  35.     SOCKADDR_IN addrClient;
  36.     int len = sizeof(SOCKADDR);

  37.     while(1)
  38.     {
  39.         SOCKET sockConn = accept(sockSrv,(SOCKADDR*)&addrClient,&len);
  40.         printf("Receive data...\n");
  41.         
  42.         char recvBuf[500];
  43.         memset(recvBuf,0,500);       
  44.         recv(sockConn, recvBuf, 500, 0);
  45.   
  46.         test::Vo_CharacterInfo Info;
  47.         Info.ParseFromString(string(recvBuf);

  48.         closesocket(sockConn);
  49.         printf("Close Socket...\n");
  50.     }

  51.     system("pause");
  52.     return 0;
  53. }

Client:

 

  1. // Client.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include "msg.pb.h"
  5. #include <stdio.h>
  6. #include <windows.h>
  7. #include <fstream>
  8. #include <string>

  9. #pragma comment(lib,"ws2_32.lib")
  10. #pragma comment(lib,"libprotobuf.lib")

  11. using namespace std;

  12. int _tmain(int argc, _TCHAR* argv[])
  13. {
  14.     WORD wVersionRequested;
  15.     WSADATA wsaData;
  16.     int err;
  17.     wVersionRequested = MAKEWORD(1,1);

  18.     err = WSAStartup( wVersionRequested, &wsaData);
  19.     if ( err != 0)
  20.     {
  21.         return 0;
  22.     }
  23.     if ( LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion != 1))
  24.     {
  25.         WSACleanup();
  26.         return 0;
  27.     }

  28.     SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);

  29.     SOCKADDR_IN addrSrv;
  30.     addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
  31.     addrSrv.sin_family = AF_INET;
  32.     addrSrv.sin_port = htons(8000);
  33.     
  34.     connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
  35.     printf("Connect Successful...\n");

  36.       test::Vo_CharacterInfo Info;
          Info.set_charid(123);
          Info.set_charname("name");

  1.     std::string info;
  2.     Info.SerializeToString(&info);


  3.     send(sockClient, info.data(), info.size(),0);
  4.     printf("Send Successful...\n");
  5.    

  6.     closesocket(sockClient);
  7.     WSACleanup();

  8.     system("pause");
  9.     return 0;
  10. }
阅读(7009) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~