• UDP 打洞示例 包含 服务器 客户端


    客户端示例:

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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  ");  
    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  ",nRet);  
    31.     nRet=udp.Recv();  
    32.     if (nRet>0)  
    33.     {  
    34.         printf("recv data  ");  
    35.     }  
    36.     else  
    37.     {  
    38.         printf("recv P2P_CMD_REGISTER_ACK time out  ");  
    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  ",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 ",  
    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  ");              
    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 ");  
    97.         }  
    98.     }  
    99.     else  
    100.     {  
    101.         printf("P2P_CMD_UDP_HOLE recv out  ");  
    102.         return 0;  
    103.     }     
    104.       
    105.       
    106.     return 0;  
    107. }  

    服务器端示例: 

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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  ");  
    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 ",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 ");  
    104.                         P2P_CLIENT_INFO* pTargetClientInfo=FindClient(pCmdHead->nValue);  
    105.   
    106.                         if (!pTargetClientInfo)  
    107.                         {  
    108.                             printf("not find client info id:%d ",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 ",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  ");  
    142.         }  
    143.     }  
    144.     return 0;  
    145. }  



    示例下载地址:    http://download.csdn.net/detail/mtour/8119489

  • 相关阅读:
    JVM -- Full GC触发条件总结以及解决策略
    java实现-图的相关操作
    Integer的intValue()方法
    Java transient关键字
    Redis 单线程模型介绍
    String类的intern()方法 -- 重用String对象,节省内存消耗
    数据库阿里连接池 druid配置详解
    redis 实现发布/订阅模式
    Redis实现队列
    redis 实现分布式锁
  • 原文地址:https://www.cnblogs.com/lidabo/p/6405380.html
Copyright © 2020-2023  润新知