2

TCP接続を使用して通信するサーバークライアントプログラムがあります。複数のクライアントが同時にサーバーに接続できます。このシステムに TCP ホール パンチングを実装したいと考えています。

クライアント側では、パブリックサーバーを呼び出して、サーバーのパブリックIP、ポートを検索します。次に、それに接続します。

ただし、サーバー側では、公開サーバーに接続するためにポートを開く必要があり、この同じポートでクライアント接続要求を受け入れる必要もあります。

私がやろうとしているのは、ソケットを開いてポート X にバインドし、公開サーバーに接続してから、このソケットをリッスン状態に変更して着信接続を一定期間受け入れてから、再び公開サーバーへの接続を開始することです。オーバー。

これは正しいアプローチですか?

編集:別のアイデアがあります。新しいポートを開き、公開サーバーに接続することです。メイン サーバー ポートは、通常どおり着信接続をリッスンしたままになります。クライアントが接続したい場合、公開サーバーは新しいポートを介してサーバーに通知します。メインポートが着信接続をリッスンするのを止めます。代わりに、クライアントに接続して穴あけを行います。次に、サーバーのパブリック IP アドレスをクライアントに転送するパブリック サーバーに接続し、通常どおり受信接続をリッスンするために戻ります。クライアントは、このアドレスを使用して、TCP ホールが既に開いているサーバーに接続します。

4

1 に答える 1

0

Better have two socket and maintain separate the conection between server and client.

  1. m_nServerTCPSocket- used to connect and listner socket with server

  2. m_nPeerPrivateTCPSocket- to connect with peer (public address)

  3. m_nPeerPublicTCPSocket- to connect with peer (private address if other peer is in the same network)

  4. m_nListeningTCPSocket - used to listener socket for peer here u need to accept connection from peer.
  5. m_nConnectedPeerTCPSocket-> you get this socket once you connected with other peer.

    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();
        }
    

logic to create sockets

      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;
         }
于 2012-10-31T08:49:34.310 に答える