int Connect(char szIP[], int nPort, int* nRetSocktFd)
{
    int                 nResult   = false;
    int                 nSocket   = -1;
    int                 nRetCode  = 0;
    struct sockaddr_in  ServerAddr;

    nSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (nSocket == -1)
    {
        perror("fcntl");
        return false;
    }

    nRetCode =  SetSocketNoBlock(nSocket);
    if (!nRetCode)
    {
        perror("SetSocketNoBlock");
        return false;
    }

    ServerAddr.sin_family       = AF_INET;
    ServerAddr.sin_addr.s_addr  = inet_addr(szIP);
    ServerAddr.sin_port         = (u_short)htons(nPort);
    memset(ServerAddr.sin_zero, 0, 8);

    errno = 0;
    nRetCode = connect(nSocket, (struct sockaddr*)&ServerAddr, sizeof(ServerAddr));

    if (nRetCode != -1 && errno != EINPROGRESS) //EINPROGRESS connecting.
    {
        perror("connect");
        return false;
    }

    *nRetSocktFd = nSocket;

    return true;
}
Beispiel #2
0
	bool TCPSocket::SetNoBlock()
	{
		return SetSocketNoBlock(sockfd);
	}