mynameisNULL
mynameisNULL
采纳率0%
2020-02-12 20:30

求助!谁知道C++UDP打洞?

为什么我的代码不在一个局域网内无法连接,也无反应?

服务器(UDP打洞):

    #include <Winsock2.h>  
    #include <stdio.h>  
    #include <stdlib.h>  

    #define DEFAULT_PORT 4768  
    #define BUFFER_SIZE 100  

    int main() {  
        int serverPort = DEFAULT_PORT;  
        WSADATA wsaData;  
        SOCKET serverListen;  
        struct sockaddr_in serverAddr;  

        if (WSAStartup(MAKEWORD(2,2),&wsaData) != 0 ) {  
            printf("Failed to load Winsock.\n");  
            return -1;  
        }  

        serverListen = socket(AF_INET,SOCK_DGRAM,0);  
        if (serverListen == INVALID_SOCKET) {  
            printf("socket() failed:%d\n",WSAGetLastError());  
            return -1;  
        }  

        serverAddr.sin_family = AF_INET;  
        serverAddr.sin_port = htons(serverPort);  
        serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);  

        if (bind(serverListen,(LPSOCKADDR)&serverAddr,sizeof(serverAddr)) == SOCKET_ERROR) {  
            printf("bind() failed:%d\n",WSAGetLastError());  
            return -1;  
        }  

        struct sockaddr_in sourceAddr1;  
        int sourceAddrLen1 = sizeof(sourceAddr1);  
        SOCKET sockC1 = socket(AF_INET,SOCK_DGRAM,0);  
        char bufRecv1[BUFFER_SIZE];  
        int len;  

        len = recvfrom(serverListen, bufRecv1, sizeof(bufRecv1), 0,(struct sockaddr*)&sourceAddr1,&sourceAddrLen1);  
        if (len == SOCKET_ERROR) {  
            printf("recv() failed:%d\n", WSAGetLastError());  
            return -1;  
        }  

        printf("C1 IP:[%s],PORT:[%d]\n",inet_ntoa(sourceAddr1.sin_addr)  
                    ,ntohs(sourceAddr1.sin_port));  

        struct sockaddr_in sourceAddr2;  
        int sourceAddrLen2 = sizeof(sourceAddr2);  
        SOCKET sockC2 = socket(AF_INET,SOCK_DGRAM,0);  
        char bufRecv2[BUFFER_SIZE];  

        len = recvfrom(serverListen, bufRecv2, sizeof(bufRecv2), 0,(struct sockaddr*)&sourceAddr2,&sourceAddrLen2);  
        if (len == SOCKET_ERROR) {  
            printf("recv() failed:%d\n", WSAGetLastError());  
            return -1;  
        }  

        printf("C2 IP:[%s],PORT:[%d]\n",inet_ntoa(sourceAddr2.sin_addr)  
                    ,ntohs(sourceAddr2.sin_port));    


        char bufSend1[BUFFER_SIZE];  
        memset(bufSend1,'\0',sizeof(bufSend1));  
        char* ip2 = inet_ntoa(sourceAddr2.sin_addr); 
        char port2[10];
        itoa(ntohs(sourceAddr2.sin_port),port2,10);
        for (int i=0;i<strlen(ip2);i++) {  
            bufSend1[i] = ip2[i];  
        }  
        bufSend1[strlen(ip2)] = '^';  
        for (int i=0;i<strlen(port2);i++) {  
            bufSend1[strlen(ip2) + 1 + i] = port2[i];  
        }  

        len = sendto(sockC1,bufSend1,sizeof(bufSend1),0,(struct sockaddr*)&sourceAddr1,sizeof(sourceAddr1));  
        if (len == SOCKET_ERROR) {  
            printf("send() failed:%d\n",WSAGetLastError());  
            return -1;  
        } else if (len == 0) {  
            return -1;  
        } else {  
            printf("send() byte:%d\n",len);  
        }  


        char bufSend2[BUFFER_SIZE];  
        memset(bufSend2,'\0',sizeof(bufSend2));  
        char* ip1 = inet_ntoa(sourceAddr1.sin_addr);
        char port1[10];//C1的port  
        itoa(ntohs(sourceAddr1.sin_port),port1,10);  
        for (int i=0;i<strlen(ip1);i++) {  
            bufSend2[i] = ip1[i];  
        }  
        bufSend2[strlen(ip1)] = '^';  
        for (int i=0;i<strlen(port1);i++) {  
            bufSend2[strlen(ip1) + 1 + i] = port1[i];  
        }  

        len = sendto(sockC2,bufSend2,sizeof(bufSend2),0,(struct sockaddr*)&sourceAddr2,sizeof(sourceAddr2));  
        if (len == SOCKET_ERROR) {  
            printf("send() failed:%d\n",WSAGetLastError());  
            return -1;  
        } else if (len == 0) {  
            return -1;  
        } else {  
            printf("send() byte:%d\n",len);  
        }  

        closesocket(serverListen);  
        closesocket(sockC1);  
        closesocket(sockC2);  
        WSACleanup();  

        return 0;  
    }  

用户C1:

    #include<Winsock2.h>  
    #include<stdio.h>  
    #include<stdlib.h>  
    #include <thread>
    #define PORT 7777  
    #define BUFFER_SIZE 100  




    int main(int argc,char* argv[]) { 

        char APTTTTT[]="127.0.0.1";
        scanf("%s",APTTTTT);
        int HYH=4768;


    WSADATA wsaData;  
    struct sockaddr_in serverAddr;  
    struct sockaddr_in thisAddr; 

      thisAddr.sin_family = AF_INET;  
        thisAddr.sin_port = htons(PORT);  
        thisAddr.sin_addr.s_addr = htonl(INADDR_ANY);  
        if (WSAStartup(MAKEWORD(2,2),&wsaData) != 0) {  
            printf("Failed to load Winsock.\n");  
            return -1;  
        }  

        serverAddr.sin_family = AF_INET;  
        serverAddr.sin_port = htons(HYH);  
        serverAddr.sin_addr.s_addr = inet_addr(APTTTTT);  

        SOCKET sockS = socket(AF_INET,SOCK_DGRAM,0);  
        if (sockS == INVALID_SOCKET) {  
            printf("socket() failed:%d\n",WSAGetLastError());  
            return -1;  
        }  
        if (bind(sockS,(LPSOCKADDR)&thisAddr,sizeof(thisAddr)) == SOCKET_ERROR) {  
            printf("bind() failed:%d\n",WSAGetLastError());  
            return -1;  
        }  
        SOCKET sockC = socket(AF_INET,SOCK_DGRAM,0); 
        if (sockC == INVALID_SOCKET) {  
            printf("socket() failed:%d\n",WSAGetLastError());  
            return -1;  
        }  

        char bufSend[] = "I am C1";  
        char bufRecv[BUFFER_SIZE];  
        memset(bufRecv,'\0',sizeof(bufRecv));  
        struct sockaddr_in sourceAddr;
        int sourceAddrLen = sizeof(sourceAddr); 
        struct sockaddr_in oppositeSideAddr;  

        int len;  

        len = sendto(sockS,bufSend,sizeof(bufSend),0,(struct sockaddr*)&serverAddr,sizeof(serverAddr));  
        if (len == SOCKET_ERROR) {  
            printf("sendto() failed:%d\n", WSAGetLastError());  
            return -1;  
        }  

        len = recvfrom(sockS, bufRecv, sizeof(bufRecv), 0,(struct sockaddr*)&sourceAddr,&sourceAddrLen);  
        if (len == SOCKET_ERROR) { 
            int TRYY=WSAGetLastError();
             if(TRYY!= WSAECONNRESET)
             {
                printf("recvfrom() failed:%d\n", WSAGetLastError());  
                return -1; 
             }
        }  

        closesocket(sockS);  
        if (bind(sockC,(LPSOCKADDR)&thisAddr,sizeof(thisAddr)) == SOCKET_ERROR) {  
            printf("bind() failed:%d\n",WSAGetLastError());  
            return -1;  
        } 

        char ip[20];  
        char port[10];  
        int i;  
        for (i=0;i<strlen(bufRecv);i++)  
            if (bufRecv[i] != '^')  
                ip[i] = bufRecv[i];  
            else break;  
        ip[i] = '\0';  
        int j;  
        for (j=i+1;j<strlen(bufRecv);j++)  
            port[j - i - 1] = bufRecv[j];  
        port[j - i - 1] = '\0';  

        oppositeSideAddr.sin_family = AF_INET;  
        oppositeSideAddr.sin_port = htons(atoi(port));  
        oppositeSideAddr.sin_addr.s_addr = inet_addr(ip);  


        unsigned long ul = 1;  
        ioctlsocket(sockC, FIONBIO, (unsigned long*)&ul);  

        while (1) {  
            Sleep(1000);   
            len = sendto(sockC,bufSend,sizeof(bufSend),0,(struct sockaddr*)&oppositeSideAddr,sizeof(oppositeSideAddr));  
            if (len == SOCKET_ERROR) {  
                printf("while sending package to C2 , sendto() failed:%d\n", WSAGetLastError());  
                return -1;  
            }else {  
                printf("successfully send package to C2\n");  
            }  

            len = recvfrom(sockC, bufRecv, sizeof(bufRecv), 0,(struct sockaddr*)&sourceAddr,&sourceAddrLen);  
            if (len == WSAEWOULDBLOCK) {  
                continue;
            }else {  
                printf("C2 IP:[%s],PORT:[%d]\n",inet_ntoa(sourceAddr.sin_addr)  
                    ,ntohs(sourceAddr.sin_port));  
                printf("C2 says:%s\n",bufRecv);  
                break;
            }  
        }  
        char buff[1024]={};
        while(1)
        {
            memset(buff, 0, 1024);
            scanf("%s",buff);
            sendto(sockC,buff,sizeof(buff),0,(struct sockaddr*)&oppositeSideAddr,sizeof(oppositeSideAddr));
            recvfrom(sockC, bufRecv, sizeof(bufRecv), 0,(struct sockaddr*)&sourceAddr,&sourceAddrLen);
            printf("%s\n\n",bufRecv);
        }
        closesocket(sockC);  


    } 

用户C2:

    #include<Winsock2.h>  
    #include<stdio.h>  
    #include<stdlib.h>  
    #include <thread>
    #define PORT 8888  
    #define BUFFER_SIZE 100  



    int main(int argc,char* argv[]) {  
        char APTTTTT[]="127.0.0.1";
        scanf("%s",APTTTTT);
        int HYH=4768;


        WSADATA wsaData;  
        struct sockaddr_in serverAddr;  
        struct sockaddr_in thisAddr;  
      thisAddr.sin_family = AF_INET;  
        thisAddr.sin_port = htons(PORT);  
        thisAddr.sin_addr.s_addr = htonl(INADDR_ANY);  
        if (WSAStartup(MAKEWORD(2,2),&wsaData) != 0) {  
            printf("Failed to load Winsock.\n");  
            return -1;  
        }  

        serverAddr.sin_family = AF_INET;  
        serverAddr.sin_port = htons(HYH);  
        serverAddr.sin_addr.s_addr = inet_addr(APTTTTT);  

        SOCKET sockS = socket(AF_INET,SOCK_DGRAM,0);  
        if (sockS == INVALID_SOCKET) {  
            printf("socket() failed:%d\n",WSAGetLastError());  
            return -1;  
        }  
        if (bind(sockS,(LPSOCKADDR)&thisAddr,sizeof(thisAddr)) == SOCKET_ERROR) {  
            printf("bind() failed:%d\n",WSAGetLastError());  
            return -1;  
        }  
        SOCKET sockC = socket(AF_INET,SOCK_DGRAM,0); 
        if (sockC == INVALID_SOCKET) {  
            printf("socket() failed:%d\n",WSAGetLastError());  
            return -1;  
        }  

        char bufSend[] = "I am C2";  
        char bufRecv[BUFFER_SIZE];  
        memset(bufRecv,'\0',sizeof(bufRecv));  
        struct sockaddr_in sourceAddr;
        int sourceAddrLen = sizeof(sourceAddr);
        struct sockaddr_in oppositeSideAddr; 

        int len;  

        len = sendto(sockS,bufSend,sizeof(bufSend),0,(struct sockaddr*)&serverAddr,sizeof(serverAddr));  
        if (len == SOCKET_ERROR) {  
            printf("sendto() failed:%d\n", WSAGetLastError());  
            return -1;  
        }  


        len = recvfrom(sockS, bufRecv, sizeof(bufRecv), 0,(struct sockaddr*)&sourceAddr,&sourceAddrLen);  
        if (len == SOCKET_ERROR) {  
        int TRYY=WSAGetLastError();
             if(TRYY!= WSAECONNRESET)
             {
                printf("recvfrom() failed:%d\n", WSAGetLastError());  
                return -1; 
             }
        }  

        closesocket(sockS);  
        if (bind(sockC,(LPSOCKADDR)&thisAddr,sizeof(thisAddr)) == SOCKET_ERROR) {  
            printf("bind() failed:%d\n",WSAGetLastError());  
            return -1;  
        }  

        char ip[20];  
        char port[10];  
        int i;  
        for (i=0;i<strlen(bufRecv);i++)  
            if (bufRecv[i] != '^')  
                ip[i] = bufRecv[i];  
            else break;  
        ip[i] = '\0';  
        int j;  
        for (j=i+1;j<strlen(bufRecv);j++)  
            port[j - i - 1] = bufRecv[j];  
        port[j - i - 1] = '\0';  

        oppositeSideAddr.sin_family = AF_INET;  
        oppositeSideAddr.sin_port = htons(atoi(port));  
        oppositeSideAddr.sin_addr.s_addr = inet_addr(ip);  


        unsigned long ul = 1;  
        ioctlsocket(sockC, FIONBIO, (unsigned long*)&ul);  

        while (1) {  
            Sleep(1000);  

            len = sendto(sockC,bufSend,sizeof(bufSend),0,(struct sockaddr*)&oppositeSideAddr,sizeof(oppositeSideAddr));  
            if (len == SOCKET_ERROR) {  
                printf("while sending package to C1 , sendto() failed:%d\n", WSAGetLastError());  
                return -1;  
            }else {  
                printf("successfully send package to C1\n");  
            }  

            len = recvfrom(sockC, bufRecv, sizeof(bufRecv), 0,(struct sockaddr*)&sourceAddr,&sourceAddrLen);  
            if (len == WSAEWOULDBLOCK) {  
                continue;
            }else {  
                printf("C1 IP:[%s],PORT:[%d]\n",inet_ntoa(sourceAddr.sin_addr)  
                    ,ntohs(sourceAddr.sin_port));  
                printf("C1 says:%s\n",bufRecv);  
                break;
            }  
        } 
        char buff[1024]={};
        while(1)
        {
            memset(buff, 0, 1024);
            scanf("%s",buff);
            sendto(sockC,buff,sizeof(buff),0,(struct sockaddr*)&oppositeSideAddr,sizeof(oppositeSideAddr));
            recvfrom(sockC, bufRecv, sizeof(bufRecv), 0,(struct sockaddr*)&sourceAddr,&sourceAddrLen);
            printf("%s\n\n",bufRecv);
        }
        closesocket(sockC);  


    }  
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

相关推荐