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 Type | Function name | Arguments |
|---|---|---|
| hzEcode | hzTcpClient::ConnectStd | (const char*,uint32_t,uint32_t,uint32_t,) |
Declared in file: hzTcpClient.h
Defined in file : hzTcpClient.cpp
Function Logic:
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 ;
}