Establish a standard, non-local, non-SSL TCP connection to a server at a known IP address.

Return TypeFunction nameArguments
hzEcodehzTcpClient::ConnectIP(hzIpaddr&,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:m_nPort items m_SvrAddr items m_SvrAddr 6:unknown 7:Return hzerr(E_NOSOCKET,Could not create socket (errno=%d),errno) 8:unknown 9:Return hzerr(E_HOSTFAIL,Could not connect to host [%s] on port %d (errno=%d),*m_Hostname,m_nPort,errno) 10:rc 11:unknown 12:rc 13:Return E_OK

Function body:

hzEcode hzTcpClient::ConnectIP (hzIpaddr& ipa)uint32_t nPort, uint32_t nTimeoutR, uint32_t nTimeoutS, 
{
   //  Establish a standard, non-local, non-SSL TCP connection to a server at a known IP address.
   //  
   //  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_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::ConnectIP") ;
   hzEcode rc = E_OK ;     //  Return code
   //  Check we are not already connected
   if (m_nSock)
   {
       //  if (m_Hostname == hostname && m_nPort == nPort)
       if (m_nPort == nPort)
           return E_OK ;
       m_Hostname.Clear() ;
       m_pHost = 0;
       Close() ;
   }
   //  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 as client to host
   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 ;
}