博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UDP 打洞示例 包含 服务器 客户端
阅读量:6234 次
发布时间:2019-06-22

本文共 5496 字,大约阅读时间需要 18 分钟。

客户端示例:

 

 

[cpp]   
 
 
  1. #include "Net.h"  
  2. #include "../p2pInfo.h"  
  3.   
  4. int main()  
  5. {  
  6.     CUdp  udp;  
  7.     if (0!=udp.Open(16888))  
  8.     {  
  9.         printf("client udp open failed \n");  
  10.         return -1;  
  11.     }  
  12.   
  13.     P2P_CLIENT_INFO clientInfo;  
  14.     memset(&clientInfo,0,sizeof(P2P_CLIENT_INFO));  
  15.   
  16.     clientInfo.nClientPort=16888;  
  17.     clientInfo.nID=0;  
  18.     sprintf(clientInfo.sClientIP,"%s","10.10.2.161");  
  19.   
  20.     P2P_CMD_HEAD cmdHead;  
  21.   
  22.     cmdHead.nCmd=P2P_CMD_REGISTER_REQ;  
  23.     cmdHead.nPayloadLen=sizeof(P2P_CLIENT_INFO);  
  24.     memcpy(cmdHead.sPayLoad,&clientInfo,sizeof(P2P_CLIENT_INFO));  
  25.   
  26.     long lDestIP=0;  
  27.     CBSocket::ConvertStringToIP((int*)&lDestIP,"114.247.165.37");  
  28.     int nRet=0;  
  29.     nRet=udp.Send((unsigned char *)&cmdHead,CMD_HEAD_LEN+cmdHead.nPayloadLen,lDestIP,P2P_SERVER_PORT);  
  30.     printf("send register len %d \n",nRet);  
  31.     nRet=udp.Recv();  
  32.     if (nRet>0)  
  33.     {  
  34.         printf("recv data \n");  
  35.     }  
  36.     else  
  37.     {  
  38.         printf("recv P2P_CMD_REGISTER_ACK time out \n");  
  39.         return 0;  
  40.     }     
  41.   
  42.       
  43.     P2P_CLIENT_INFO remoteClientInfo;  
  44.     memset(&remoteClientInfo,0,sizeof(P2P_CLIENT_INFO));  
  45.     P2P_CMD_HEAD recvHead;  
  46.     memset(&recvHead,0,sizeof(P2P_CMD_HEAD));  
  47.   
  48.     while (1)  
  49.     {  
  50.         cmdHead.nCmd=P2P_CMD_COMUNICATION_REQ;  
  51.         cmdHead.nValue=1;    // remote id  
  52.         cmdHead.nPayloadLen=sizeof(P2P_CLIENT_INFO);  
  53.   
  54.         nRet=udp.Send((unsigned char *)&cmdHead,CMD_HEAD_LEN+cmdHead.nPayloadLen,lDestIP,P2P_SERVER_PORT);  
  55.         printf("send commuication len %d \n",nRet);  
  56.         nRet=udp.Recv();          
  57.           
  58.         if (nRet>0)  
  59.         {  
  60.             memcpy(&recvHead,udp.GetBuffer(),nRet);  
  61.             if (0==recvHead.nValue)  
  62.             {             
  63.                 memcpy(&remoteClientInfo,recvHead.sPayLoad,sizeof(P2P_CLIENT_INFO));  
  64.                 printf("recv P2P_CMD_COMUNICATION_ACK remote ip:%s port:%d\n",  
  65.                     remoteClientInfo.sClientPublicIP,remoteClientInfo.nClientPublicPort);  
  66.                 break;  
  67.             }  
  68.             usleep(100*1000);  
  69.         }  
  70.         else  
  71.         {  
  72.             printf("recv P2P_CMD_COMUNICATION_ACK time out \n");              
  73.         }  
  74.     }  
  75.       
  76.       
  77.   
  78.     // send udp hole to remote ip  
  79.     CBSocket::ConvertStringToIP((int*)&lDestIP,remoteClientInfo.sClientPublicIP);  
  80.     cmdHead.nCmd=P2P_CMD_UDP_HOLE_REQ;  
  81.     cmdHead.nPayloadLen=0;  
  82.     udp.Send((unsigned char *)&cmdHead,CMD_HEAD_LEN,lDestIP,remoteClientInfo.nClientPublicPort);  
  83.     usleep(1000*1000);  
  84.     udp.Send((unsigned char *)&cmdHead,CMD_HEAD_LEN,lDestIP,remoteClientInfo.nClientPublicPort);  
  85.     usleep(1000*1000);  
  86.     udp.Send((unsigned char *)&cmdHead,CMD_HEAD_LEN,lDestIP,remoteClientInfo.nClientPublicPort);  
  87.   
  88.       
  89.     nRet=udp.Recv();  
  90.     if (nRet>0)  
  91.     {  
  92.         memcpy(&recvHead,udp.GetBuffer(),nRet);  
  93.           
  94.         if (recvHead.nCmd==P2P_CMD_UDP_HOLE_ACK || recvHead.nCmd==P2P_CMD_UDP_HOLE_REQ)  
  95.         {  
  96.             printf("recv P2P_CMD_UDP_HOLE  udp hole success\n");  
  97.         }  
  98.     }  
  99.     else  
  100.     {  
  101.         printf("P2P_CMD_UDP_HOLE recv out \n");  
  102.         return 0;  
  103.     }     
  104.       
  105.       
  106.     return 0;  
  107. }  

 

 

服务器端示例: 

 

[cpp]   
 
 
  1. #include "Net.h"  
  2. #include "../p2pInfo.h"  
  3.   
  4. #define MAX_CLIENT_COUNT 16  
  5.   
  6. P2P_CLIENT_INFO* pClientList[MAX_CLIENT_COUNT];  
  7.   
  8. void AddClient(P2P_CLIENT_INFO* pClientInfo)  
  9. {  
  10.     for(int i=0;i<MAX_CLIENT_COUNT;i++)  
  11.     {  
  12.         if (!pClientList[i])  
  13.         {  
  14.             P2P_CLIENT_INFO* pClient=new P2P_CLIENT_INFO;  
  15.             memcpy(pClient,pClientInfo,sizeof(P2P_CLIENT_INFO));  
  16.             pClientList[i]=pClient;  
  17.             return;  
  18.         }  
  19.     }  
  20. }  
  21.   
  22. P2P_CLIENT_INFO* FindClient(int nID)  
  23. {  
  24.     for(int i=0;i<MAX_CLIENT_COUNT;i++)  
  25.     {  
  26.         if (pClientList[i])  
  27.         {  
  28.             if (nID==pClientList[i]->nID)  
  29.             {  
  30.                 return pClientList[i];  
  31.             }  
  32.         }  
  33.     }  
  34.     return NULL;  
  35. }  
  36.   
  37. P2P_CLIENT_INFO* FindClient(long ip, unsigned short nPort)  
  38. {  
  39.     for(int i=0;i<MAX_CLIENT_COUNT;i++)  
  40.     {  
  41.         if (pClientList[i])  
  42.         {  
  43.             int nIp=0;  
  44.             CBSocket::ConvertStringToIP(&nIp,pClientList[i]->sClientPublicIP);  
  45.             if (nPort==pClientList[i]->nClientPublicPort && ip==nIp)  
  46.             {  
  47.                 return pClientList[i];  
  48.             }  
  49.         }  
  50.     }  
  51.     return NULL;  
  52. }  
  53.   
  54.   
  55. int main()  
  56. {  
  57.     CUdp udp;  
  58.     udp.Open(P2P_SERVER_PORT);  
  59.     int nRet=0;  
  60.     P2P_CMD_HEAD* pCmdHead=new P2P_CMD_HEAD;  
  61.     P2P_CMD_HEAD* pSendCmdHead=new P2P_CMD_HEAD;  
  62.     memset(pCmdHead,0,sizeof(P2P_CMD_HEAD));  
  63.     memset(pSendCmdHead,0,sizeof(P2P_CMD_HEAD));  
  64.       
  65.     long lRemoteIP=0;  
  66.     unsigned short nRemotePort=0;  
  67.   
  68.   
  69.     int i=0;  
  70.     for (i = 0; i <16; i++)  
  71.     {  
  72.         pClientList[i]=NULL;  
  73.     }  
  74.       
  75.       
  76.     while (1)  
  77.     {  
  78.         nRet=udp.Recv(lRemoteIP,nRemotePort);  
  79.         if (nRet>0)  
  80.         {  
  81.             memcpy(pCmdHead,udp.GetBuffer(),nRet);  
  82.   
  83.             switch (pCmdHead->nCmd)  
  84.             {  
  85.                 case P2P_CMD_REGISTER_REQ:  
  86.                     {  
  87.                         printf("recv register req \n");  
  88.                         P2P_CLIENT_INFO* pClient=(P2P_CLIENT_INFO*)pCmdHead->sPayLoad;  
  89.                         CBSocket::ConvertIPToString(lRemoteIP,pClient->sClientPublicIP);  
  90.                         pClient->nClientPublicPort=nRemotePort;  
  91.                         AddClient(pClient);  
  92.   
  93.                         printf("recv command P2P_CMD_REGISTER_REQ from ip:%s port:%d\n",pClient->sClientPublicIP,pClient->nClientPublicPort);  
  94.   
  95.                         pSendCmdHead->nCmd=P2P_CMD_REGISTER_ACK;  
  96.                         pSendCmdHead->nPayloadLen=sizeof(P2P_CLIENT_INFO);  
  97.                         memcpy(pSendCmdHead->sPayLoad,pClient,sizeof(P2P_CLIENT_INFO));  
  98.                         udp.Send((unsigned char *)pSendCmdHead,CMD_HEAD_LEN+pSendCmdHead->nPayloadLen,lRemoteIP,nRemotePort);  
  99.                     }  
  100.                     break;  
  101.                 case P2P_CMD_COMUNICATION_REQ:  
  102.                     {  
  103.                         printf("recv command P2P_CMD_COMUNICATION_REQ\n");  
  104.                         P2P_CLIENT_INFO* pTargetClientInfo=FindClient(pCmdHead->nValue);  
  105.   
  106.                         if (!pTargetClientInfo)  
  107.                         {  
  108.                             printf("not find client info id:%d\n",pCmdHead->nValue);  
  109.                             continue;  
  110.                         }  
  111.   
  112.                         pSendCmdHead->nCmd=P2P_CMD_COMUNICATION_ACK;  
  113.                         pSendCmdHead->nPayloadLen=sizeof(P2P_CLIENT_INFO);  
  114.                         memcpy(pSendCmdHead->sPayLoad,pTargetClientInfo,sizeof(P2P_CLIENT_INFO));  
  115.                         udp.Send((unsigned char *)pSendCmdHead,CMD_HEAD_LEN+pSendCmdHead->nPayloadLen,lRemoteIP,nRemotePort);                          
  116.   
  117.   
  118.   
  119.                         // send to remote client  
  120.                         int nIP=0;  
  121.                         P2P_CLIENT_INFO* pSelfClientInfo=FindClient(lRemoteIP,nRemotePort);  
  122.                         if (pSelfClientInfo)  
  123.                         {  
  124.                             printf("find self client info ip:%s\n",pSelfClientInfo->sClientPublicIP);  
  125.                             memcpy(pSendCmdHead->sPayLoad,pSelfClientInfo,sizeof(P2P_CLIENT_INFO));  
  126.                             CBSocket::ConvertStringToIP(&nIP,pTargetClientInfo->sClientPublicIP);  
  127.                             udp.Send((unsigned char *)pSendCmdHead,CMD_HEAD_LEN+pSendCmdHead->nPayloadLen,nIP,pTargetClientInfo->nClientPublicPort);  
  128.                         }         
  129.                         else  
  130.                         {  
  131.                             printf("not find self client info");  
  132.                         }  
  133.                     }  
  134.                     break;  
  135.                 default:  
  136.                     break;  
  137.             }  
  138.         }  
  139.         else  
  140.         {  
  141.             printf("udp recv time out \n");  
  142.         }  
  143.     }  
  144.     return 0;  
  145. }  

 

示例下载地址:    

转载地址:http://unqna.baihongyu.com/

你可能感兴趣的文章
假如有一天,人工智能用于相亲
查看>>
0227 - 简单了解了 ETH 挖矿
查看>>
技术问答集锦(14)ThreadPoolExecutor
查看>>
macOS Charles 4.x版本的安装及使用(含破解激活)
查看>>
InnoDB透明页压缩与稀疏文件
查看>>
阅读SSM项目之scm【第二篇】
查看>>
聊聊springmvc中controller的方法的参数注解
查看>>
AspectJ 学习笔记
查看>>
GRU神经网络
查看>>
【log4】window用于设置小程序的状态栏、导航条、标题、窗口背景色。
查看>>
gpexpand分析
查看>>
前端每周清单第 44 期: 2017 JS 调查报告、REST 接口实时化、ESM 的过去与未来
查看>>
IP、UDP初探
查看>>
分布式系统中常见技术解决的问题是什么?
查看>>
WWDC 2018:理解崩溃以及崩溃日志
查看>>
「 iOS知识小集 」2018 · 第 40 期
查看>>
太极越狱重大安全后门
查看>>
一步一步学ROP之linux_x86篇
查看>>
【译】Ruby2.6的JIT功能,编译和解释型语言的相关说明
查看>>
架构设计知识梳理(2) Flux
查看>>