Esempio n. 1
0
int CServerLinker::Link(void)
{
    closeSock();

    int iRtn = Q_RTN_OK;
    CNETAddr objAddr;
    int iKeepAlive = 1;

    iRtn = objAddr.setAddr(m_strDesIp.c_str(), m_usDesPort);
    if (Q_RTN_OK != iRtn)
    {
        return iRtn;
    }

    m_Sock = socket(AF_INET, SOCK_STREAM, 0);
    if (Q_INVALID_SOCK == m_Sock)
    {
        iRtn = Q_SockError();
        Q_Printf("create socket error. error code %d, message %s", 
            iRtn, Q_SockError2Str(iRtn));

        return iRtn;
    }

    iRtn = connect(m_Sock, objAddr.getAddr(), objAddr.getAddrSize());
    if (Q_RTN_OK != iRtn)
    {
        iRtn = Q_SockError();
        Q_Printf("connect %s on port %d error. error code %d, message %s", 
            objAddr.getIp().c_str(), objAddr.getPort(), iRtn, Q_SockError2Str(iRtn));

        return iRtn;
    }

    (void)setsockopt(m_Sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&iKeepAlive, sizeof(iKeepAlive));

    return Q_RTN_OK;
}