Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4628
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-19 08:24
文章分类

全部博文(8)

文章存档

2014年(8)

我的朋友
最近访客

分类: 网络与安全

2014-11-19 09:40:15

client端

  1. #include <iostream>
  2. #include <string>
  3. #define _WIN32_WINNT 0x501
  4. #include <winsock2.h>
  5. #include <ws2tcpip.h>
  6. #include <w32api.h>
  7. #include <stdio.h>
  8. #include <process.h>
  9. #include <windows.h>
  10. using namespace std;
  11. int socketfd;
  12. unsigned _stdcall Receiving(void*)
  13. {
  14.     while (true)
  15.     {
  16.         ssize_t bytes_received;
  17.         char incoming_data_buffer[1000];
  18.         bytes_received = recv(socketfd,incoming_data_buffer,1000,0);
  19.         if (bytes_received == 0)
  20.         {
  21.             cout << "host shut down."<<endl;
  22.             break;
  23.         }
  24.             
  25.         if (bytes_received == -1)
  26.             continue;
  27.        incoming_data_buffer[bytes_received]='\0';
  28.        cout << bytes_received <<" "<<"bytes received :"<<endl;
  29.        cout << incoming_data_buffer <<endl;
  30.        cout <<"---------------Received--------------"<<endl<<""<<" ";
  31.     }
  32.     
  33. }
  34. unsigned _stdcall Sending(void*)
  35. {
  36.     string str,code=">q";
  37.     fflush(stdin);
  38.     while (true)
  39.     {
  40.         cout << " ";
  41.         getline(cin,str);
  42.         const char *msg=str.data();
  43.         const char *c_ode=code.data();
  44.         if (strcmp(msg,c_ode)==0)
  45.             break;
  46.         int len;
  47.         ssize_t bytes_sent;
  48.         len=strlen(msg);
  49.         bytes_sent = send(socketfd,msg,len,0);
  50.         cout << "----------------Sent----------------"<<endl;
  51.     }
  52. }
  53. int main()
  54. {
  55.     WSADATA wsadata;
  56.     if (WSAStartup(MAKEWORD(2,2),&wsadata)!=0)
  57.     {
  58.         cout <<"WSAStartup failed to initialize!"<<endl;
  59.         return 0;
  60.     }
  61.     int status;
  62.     addrinfo host_info;
  63.     addrinfo *host_info_list;
  64.     memset(&host_info,0,sizeof(host_info));
  65.     cout << "Setting up the struct..."<<endl;
  66.     host_info.ai_family=AF_UNSPEC;
  67.     host_info.ai_socktype = SOCK_STREAM;
  68.     char ip[16],port[9999];
  69.     cout <<"IP Address:";
  70.     cin>>ip;
  71.     cout<<"Port:";
  72.     cin >>port;
  73.     status=getaddrinfo(ip,port,&host_info,&host_info_list);
  74.     if (status!=0)
  75.         cout << "getaddrinfo error"<<gai_strerror(status);
  76.     cout << "Creating a socket..."<<endl;
  77.     socketfd = socket(host_info_list->ai_family,host_info_list->ai_socktype,host_info_list->ai_protocol);
  78.     if (socketfd == -1)
  79.     {
  80.         cout <<"Socket error."<<endl;
  81.         return 0;
  82.     }
  83.     cout <<"Connecting to "<<ip<<endl;
  84.     status=connect(socketfd,host_info_list->ai_addr,host_info_list->ai_addrlen);
  85.     if (status == -1)
  86.     {
  87.         cout << "connect error"<<endl;
  88.         return 0;
  89.     }
  90.     cout << "Connection established."<<endl;
  91.     unsigned int thID1,thID2;
  92.     HANDLE hth1,hth2;
  93.     hth2=(HANDLE)_beginthreadex(NULL,0,Receiving,NULL,0,&thID2);
  94.     hth1=(HANDLE)_beginthreadex(NULL,0,Sending,NULL,0,&thID1);
  95.     WaitForSingleObject(hth1,INFINITE);
  96.     cout << "Closing threads: ID: "<<thID1<<" "<<thID2<<endl;
  97.     CloseHandle(hth1);
  98.     CloseHandle(hth2);
  99.     cout << "Closing socket..."<<endl;
  100.     freeaddrinfo(host_info_list);
  101.     closesocket(socketfd);
  102.     return 0;
  103. }


服务端

  1. #include <iostream>
  2. #include <cstring>
  3. #define _WIN32_WINNT 0x501
  4. #include <winsock2.h>
  5. #include <ws2tcpip.h>
  6. #include <w32api.h>
  7. #include <stdio.h>
  8. #include <process.h>
  9. #include <windows.h>
  10. using namespace std;
  11. int new_sd;
  12. unsigned _stdcall Receiving(void*)
  13. {
  14.     while (true)
  15.     {
  16.         ssize_t bytes_received;
  17.         char incoming_data_buffer[1000];
  18.         bytes_received = recv(new_sd,incoming_data_buffer,1000,0);
  19.         if (bytes_received == 0)
  20.         {
  21.             cout << "host shut down."<<endl;
  22.             break;
  23.         }
  24.         if (bytes_received == -1)
  25.             continue;
  26.         incoming_data_buffer[bytes_received]='\0';//important.it will appear "\" at the end of letter without it.
  27.         cout << bytes_received <<" "<<"bytes received :"<<endl;
  28.         cout << incoming_data_buffer <<endl;
  29.         cout <<"-------------------Received-------------------"<<endl<<" ";
  30.     }
  31. }
  32. unsigned _stdcall Sending(void*)
  33. {
  34.     string str,code=">q";
  35.     fflush(stdin);
  36.     while (true)
  37.     {
  38.         cout << " ";
  39.         getline(cin,str);
  40.         const char *msg=str.data();
  41.         const char *c_ode=code.data();
  42.         if (strcmp(msg,c_ode)==0)
  43.             break;
  44.         int len;
  45.         ssize_t bytes_sent;
  46.         len=strlen(msg);
  47.         bytes_sent = send(new_sd,msg,len,0);
  48.         cout << "---------------------Sent--------------------"<<endl;
  49.     }
  50. }
  51. int main()
  52. {
  53.     WSADATA wsadata;
  54.     if (WSAStartup(MAKEWORD(2,2),&wsadata)!=0)
  55.     {
  56.         cout <<"WSAStartup failed to initialize!"<<endl;
  57.         return 0;
  58.     }
  59.     int status;
  60.     addrinfo host_info;
  61.     addrinfo *host_info_list;
  62.     memset(&host_info,0,sizeof(host_info));
  63.     cout << "Setting up the struct..."<<endl;
  64.     host_info.ai_family=AF_UNSPEC;
  65.     host_info.ai_socktype = SOCK_STREAM;
  66.     host_info.ai_flags = AI_PASSIVE;
  67.     char *ip=NULL;
  68.     char name[32]="";
  69.     PHOSTENT hostinfo;
  70.     if(gethostname (name, sizeof(name)) == 0)
  71.     {
  72.             //如果成功 将本地主机名存放入name缓冲区
  73.         if((hostinfo = gethostbyname(name)) != NULL)
  74.         {
  75.           ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);//获取主机IP
  76.         }
  77.     }
  78.     cout <<"Server IP: "<<ip<<endl;
  79.     status=getaddrinfo(ip,"5555",&host_info,&host_info_list);
  80.     if (status!=0)
  81.         cout << "getaddrinfo error"<<gai_strerror(status);
  82.     cout << "Creating a socket..."<<endl;
  83.     int socketfd;
  84.     socketfd = socket(host_info_list->ai_family,host_info_list->ai_socktype,host_info_list->ai_protocol);
  85.     if (socketfd == -1)
  86.         cout <<"socket error";
  87.     cout <<"Binding socket..."<<endl;
  88.     status=setsockopt(socketfd,SOL_SOCKET,SO_REUSEADDR,"1",sizeof(int));
  89.     status=bind(socketfd,host_info_list->ai_addr,host_info_list->ai_addrlen);
  90.     if (status == -1)
  91.         cout << "bind error"<<endl;
  92.     cout <<"Listening for connections..."<<endl;
  93.     status=listen(socketfd,5);
  94.     if (status == -1)
  95.         cout << "listen error"<<endl;
  96.     struct sockaddr_storage their_addr;
  97.     socklen_t addr_size = sizeof(their_addr);
  98.     new_sd=accept(socketfd,(struct sockaddr *)&their_addr,&addr_size);
  99.     if (new_sd == -1)
  100.     {
  101.         cout <<"listen error"<<endl;
  102.     }
  103.     else
  104.     {
  105.         cout <<"Connections accepted.Using new socketfd : "<<new_sd<<endl;
  106.     }
  107.     unsigned int thID1,thID2;
  108.     HANDLE hth1,hth2;
  109.     hth1=(HANDLE)_beginthreadex(NULL,0,Receiving,NULL,0,&thID1);
  110.     hth2=(HANDLE)_beginthreadex(NULL,0,Sending,NULL,0,&thID2);
  111.     WaitForSingleObject(hth2,INFINITE);
  112.     cout << "Closing threads: ID: "<<thID1<<" "<<thID2<<endl;
  113.     CloseHandle(hth1);
  114.     CloseHandle(hth2);
  115.     cout << "Stopping Server..."<<endl;
  116.     freeaddrinfo(host_info_list);
  117.     closesocket(new_sd);
  118.     closesocket(socketfd);
  119.     return 0;
  120. }


先贴代码,有空再解释。
阅读(136) | 评论(0) | 转发(0) |
0

上一篇:拆分素数和

下一篇:没有了

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