Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8764643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:05:17+00:00 2026-06-13T16:05:17+00:00

I have a server-client program that use TCP connection to communicate. More than one

  • 0

I have a server-client program that use TCP connection to communicate. More than one client can connect to the server at the same time. I want to implement the tcp hole punching on this system.

On the client side, It calls to the public server to look up the public ip,port of my server. Then connect to it.

But on the server side it has to open a port to connect to the public server, and it also has to accept the client connection request on this same port.

What I’m going to do is opening a socket and bind to port X, then connect to the public server, then change this socket to listening state to accept the incoming connection for some period, then start connection to the public server again, over and over.

Is this the right approach ?

EDIT: I have another idea. It is to open a new port and connect to the public server. The main server port is left listening for incoming connection as usual. When the client want to connect, the public server will tell my server via the new port. It will stop the main port from listen the incoming connection, instead, It will connect to the client to do the hole punching. Then It connect to the public server, which will forward the server public ip address to the client, and goes back to listen for incoming connection as usual. The client will then use this address to connect to the server which TCP hole already opened.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:05:18+00:00Added an answer on June 13, 2026 at 4:05 pm

    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;
             }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Client program by tcp connection that send data to a server.
I have a simple TCP connection with a server and a client program. I
I have a client/server program in TCP written in C, and I would like
I have client and server programs which now communicate via TCP. I'm trying out
I have a client/server program that attempts to send and receive an object. There
I have a python program that is supposed to connect to a server and
I have a server and a client program that talks to eachother over a
I have a client-server program that uses sockets to transfer information in a stateful
I have a sample TCP client and server application that I want to run
all. I'm having a bit of weird problem with client server program. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.