如何控制对等端口[TCP打孔]

时间:2012-10-29 08:31:02

标签: networking tcp hole-punching

我有一个使用TCP连接进行通信的服务器 - 客户端程序。多个客户端可以同时连接到服务器。我想在这个系统上实现tcp打孔。

在客户端,它调用公共服务器查找我的服务器的公共IP端口。然后连接到它。

但是在服务器端,它必须打开一个端口连接到公共服务器,它还必须接受同一端口上的客户端连接请求。

我要做的是打开套接字并绑定到端口X,然后连接到公共服务器,然后将此套接字更改为侦听状态以接受传入连接一段时间,然后开始连接到公共服务器再次,一遍又一遍。

这是正确的方法吗?

编辑:我有另一个想法。它是打开一个新端口并连接到公共服务器。主服务器端口像往常一样正在侦听传入连接。当客户端想要连接时,公共服务器将通过新端口告诉我的服务器。它会阻止主端口监听传入连接,而是会连接到客户端进行打孔。然后它连接到公共服务器,它将服务器公共IP地址转发到客户端,然后像往常一样回去监听传入连接。然后,客户端将使用此地址连接到已打开TCP孔的服务器。

1 个答案:

答案 0 :(得分:0)

最好有两个套接字并维护服务器和客户端之间的连接。

  1. m_nServerTCPSocket-用于连接和列表器套接字与服务器

  2. m_nPeerPrivateTCPSocket-与同行(公共地址)连接

  3. m_nPeerPublicTCPSocket-与对等方连接(私有地址,如果其他对等方位于同一网络中)

  4. m_nListeningTCPSocket - 用于侦听同伴的套接字你需要接受来自peer的连接。
  5. m_nConnectedPeerTCPSocket->一旦你与其他同伴联系,你就会得到这个套接字。

    while(end_client)    
    {
    
       FD_ZERO(&fdRead);
       FD_ZERO(&fdWrite);
       FD_ZERO(&fdExcept);    
    
       if (pControlMgr->GetConnectionMgr()->GetListeningTCPSocket()>0)
    
       {
    
            FD_SET (pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),&fdRead);      
            FD_SET (pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),&fdExcept);
        }
    
        if (pControlMgr->GetConnectionMgr()->GetServerTCPSocket()>0)
        {
    
            FD_SET (pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),&fdRead); 
            FD_SET (pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),&fdExcept);
    }
        if (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket()>0)
    {
        FD_SET (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),&fdRead);      
        FD_SET (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),&fdExcept);        
    }
    
    timeval tv;
    tv.tv_sec = 2;
    tv.tv_usec = 0;
    
    nSelectRetVal =  select(NULL,&fdRead,NULL,&fdExcept,&tv);
    
    
    if (nSelectRetVal>0)
    {
        int nRecvRetVal = 0;
        /* TCP Server Socket handling */
        if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(), &fdRead ))
        {           
            try
            {
                pRecvBuffer =  new char[TCP_RECV_SIZE];
                nRecvRetVal = recv(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),
                    pRecvBuffer,TCP_RECV_SIZE,
                    0);
                int n = WSAGetLastError();
                if (nRecvRetVal>0)
                {
                    int nPeerNameRetVal = getpeername(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),(sockaddr*)&addrRemotePeer,&nSockAddrLen);
                    if ( pControlMgr->HandlePacket(pRecvBuffer,addrRemotePeer)== -1 )                       
                    {
                        if ( NULL != pRecvBuffer)
                        {
                            delete [] pRecvBuffer;
                            pRecvBuffer = NULL;
                            return 0 ;
                        }
                    }
                }                           
            }           
            catch (...)
            {
                if ( NULL != pRecvBuffer )
                {
                    delete [] pRecvBuffer;
                    pRecvBuffer = NULL;
                }
            }
    
            if ( NULL != pRecvBuffer)
            {
                delete [] pRecvBuffer;
                pRecvBuffer = NULL;
            }       
        } /* TCP Server Socket handling */      
    
        int n;
        /* TCP Exception Server Socket handling */
        if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(), &fdExcept ))
        {
            /*FD_CLR(pControlMgr->GetConnectionMgr().GetServerTCPSocket (),&fdRead);
            FD_CLR(pControlMgr->GetConnectionMgr().GetServerTCPSocket (),&fdExcept);*/
            n = WSAGetLastError();
            //return 0;
        }   
           if (FD_ISSET(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),&fdRead))
        {
            sockaddr_in addrConnectedPeer;
            int nAddrLen =sizeof(addrConnectedPeer) ;
            int nConnectedSock = accept( pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),
                (sockaddr*)&addrConnectedPeer,
                &nAddrLen);
            int n1 = WSAGetLastError();
            if (nConnectedSock>0)
            {
                pControlMgr->GetConnectionMgr()->SetConnectedTCPSocket(nConnectedSock);
                int n = pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket();
                continue;
            }
        }
        /* TCP Exception Listening Socket handling */
        if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(), &fdExcept ))
        {
            FD_CLR(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket (),&fdRead);
            FD_CLR(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket (),&fdExcept);
            //return 0;
        }   /* TCP Exception Listening Socket handling */   
    
        /* Connected Peer TCP Read Socket handling */
        if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(), &fdRead ))
        {           
            try
            {
                pRecvBuffer =  new char[TCP_RECV_SIZE];
                nRecvRetVal = recv (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),
                    pRecvBuffer,TCP_RECV_SIZE,
                    0);
                if (nRecvRetVal>0)
                {
                    int nPeerNameRetVal = getpeername(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),(sockaddr*)&addrRemotePeer,&nSockAddrLen);
                    if ( pControlMgr->HandlePacket(pRecvBuffer,addrRemotePeer)== -1 )                       
                    {
                        if ( NULL != pRecvBuffer)
                        {
                            delete [] pRecvBuffer;
                            pRecvBuffer = NULL;
                            return 0 ;
                        }
                    }
                }                           
            }           
            catch (...)
            {
                if ( NULL != pRecvBuffer )
                {
                    delete [] pRecvBuffer;
                    pRecvBuffer = NULL;
                }
            }
            //FD_CLR(pControlMgr->GetConnectionMgr().GetConnectedTCPSocket(),&fdRead);  
            if ( NULL != pRecvBuffer)
            {
                delete [] pRecvBuffer;
                pRecvBuffer = NULL;
            }       
        } /* Peer TCP Read Socket handling */   
        /* TCP Exception Connected Socket handling */
        if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(), &fdExcept ))
        {
            /*FD_CLR(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket (),&fdRead);
            FD_CLR(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket (),&fdExcept);
            return 0;*/
            n = WSAGetLastError();
        }
    
  6. 创建套接字的逻辑

          int CConnectionMgr::CreateSocket(const int nSockType)
          {
           //TODO: Add code here    
               if (InitWinSock() == -1) 
           {
            return -1;      
           }
           SetLocalIPAddress(); 
    
           m_nListeningTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );                    
           if ( GetListeningTCPSocket() <0 )
            return -1;  
           if (BindSocket(GetListeningTCPSocket())<0)
            return -1;
           int nListenRet = listen(GetListeningTCPSocket(),SOMAXCONN);
    
            if (nListenRet!=0)
            {
            return -1;
             }      
             m_nPeerPrivateTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );
             if (GetPeerPrivateTCPSocket()<0)
             return -1; 
            if (BindSocket(GetPeerPrivateTCPSocket())<0)
             return -1;
    
             m_nPeerPublicTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );
             if ( GetPeerPublicTCPSocket()<0)
               return -1;
              if (BindSocket(GetPeerPublicTCPSocket())<0)
               return -1;
    
              m_nServerTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );
              if (GetServerTCPSocket()<0)
               return -1;
              if (BindSocket(GetServerTCPSocket())<0)
               return -1;
            return 1;
             }