Establish a standard, non-local, non-SSL TCP connection to a server. Note that to re-connect in the event of a drop-out, just call this function again. A subsequent call will not repeat the DNS query unless it is to a different hostname. Note also that this function will do nothing if called with the same hostname, no error flag has been set and the socket is non-zero. The error flag is set by send or recv errors.

Return TypeFunction nameArguments
hzEcodehzTcpClient::ConnectStd(const char*,uint32_t,uint32_t,uint32_t,)

Declared in file: hzTcpClient.h
Defined in file : hzTcpClient.cpp

Function Logic:

0:START 1:unknown 2:unknown 3:Return E_OK 4:items m_pHost items 5:unknown 6:m_Hostname m_pHost 7:unknown 8:m_Hostname 9:unknown 10:m_pHost 11:unknown 12:unknown 13:Return E_DNS_RETRY 14:unknown 15:Return E_DNS_NOHOST 16:unknown 17:Return E_DNS_FAILED 18:unknown 19:Return E_DNS_NODATA 20:items 21:Return hzerr(E_DNS_NOHOST,Unknown Host [%s]\n,hostname) 22:m_nPort items m_SvrAddr items m_SvrAddr 23:unknown 24:Return hzerr(E_NOSOCKET,Could not create socket (errno=%d),errno) 25:unknown 26:Return hzerr(E_HOSTFAIL,Could not connect to host [%s] on port %d (errno=%d),*m_Hostname,m_nPort,errno) 27:rc 28:unknown 29:rc 30:Return E_OK

Function body:

hzEcode hzTcpClient::ConnectStd (const char* hostname)uint32_t nPort, uint32_t nTimeoutR, uint32_t nTimeoutS, 
{
   //  Establish a standard, non-local, non-SSL TCP connection to a server. Note that to re-connect in the event of a drop-out, just call this function again.
   //  A subsequent call will not repeat the DNS query unless it is to a different hostname. Note also that this function will do nothing if called with the
   //  same hostname, no error flag has been set and the socket is non-zero. The error flag is set by send or recv errors.
   //  
   //  Arguments: 1) hostname The server name or IP address
   //     2) nPort  The port number
   //     3) nTimoutR Socket option read timeout (default 30 seconds)
   //     4) nTimoutW Socket option write timeout (default 30 seconds)
   //  
   //  Returns: E_DNS_NOHOST If the domain does not exist
   //     E_DNS_FAILED If the domain settings were invalid
   //     E_DNS_NODATA If the domain exists but no server found
   //     E_DNS_RETRY  If the DNS was busy
   //     E_NOSOCKET  If a socket could not be obtained
   //     E_HOSTFAIL  If no connection could be established or if socket options were not set.
   //     E_OK   If a connection to the host was established
   _hzfunc("hzTcpClient::ConnectStd") ;
   hzEcode rc = E_OK ;     //  Return code
   //  Check we are not already connected
   if (m_nSock)
   {
       if (m_Hostname == hostname && m_nPort == nPort)
           return E_OK ;
       m_Hostname.Clear() ;
       m_pHost = 0;
       Close() ;
   }
   if (m_Hostname && m_Hostname != hostname)
   {
       //  At the point of call there was no socket but this hzTcpClient instance has a hostname from a previous connection. If this differs from the host now
       //  being sought then the m_pHost value will be invalid and a fresh call to gethostbyname is required.
       m_Hostname = hostname ;
       m_pHost = 0;
   }
   if (!m_Hostname)
       m_Hostname = hostname ;
   //  If we have not got the hostname from a previous connect, get the hostname now
   if (!m_pHost)
   {
       m_pHost = gethostbyname(hostname) ;
       if (!m_pHost)
       {
           if (h_errno == TRY_AGAIN)       return E_DNS_RETRY ;
           if (h_errno == HOST_NOT_FOUND)  return E_DNS_NOHOST ;
           if (h_errno == NO_RECOVERY)     return E_DNS_FAILED ;
           if (h_errno == NO_DATA || h_errno == NO_ADDRESS)
               return E_DNS_NODATA ;
           m_Hostname.Clear() ;
           return hzerr(E_DNS_NOHOST, "Unknown Host [%s]\n", hostname) ;
       }
   }
   //  Create the socket
   m_nPort = nPort ;
   memset(&m_SvrAddr, 0,sizeof(m_SvrAddr)) ;
   m_SvrAddr.sin_family = AF_INET ;
   memcpy(&m_SvrAddr.sin_addr, m_pHost->h_addr, m_pHost->h_length) ;
   m_SvrAddr.sin_port = htons(nPort) ;
   if ((m_nSock = socket(AF_INET, SOCK_STREAM, 0))< 0)
       return hzerr(E_NOSOCKET, "Could not create socket (errno=%d)", errno) ;
   //  Connect stage
   if (connect(m_nSock, (SOCKADDR*) &m_SvrAddr, sizeof(m_SvrAddr)) < 0)
       return hzerr(E_HOSTFAIL, "Could not connect to host [%s] on port %d (errno=%d)", *m_Hostname, m_nPort, errno) ;
   //  Apply timeouts
   rc = SetRecvTimeout(nTimeoutR) ;
   if (rc == E_OK)
       rc = SetSendTimeout(nTimeoutR) ;
   return E_OK ;
}