OwnCode/HttpFetcher/http-fetcher-orig/HttpFetcher.cpp
/*
 * Opens a TCP socket and returns the descriptor
 * Returns:
 *  socket descriptor, or
 *  -1 on error
 */
Opens a TCP socket and returns the descriptor
Returns:
socket descriptor, or
-1 on error
Opens a TCP socket and returns the descriptor Returns : socket descriptor or - 1 on error
int makeSocket(const char *host)
{
    int sock;                                       
    struct sockaddr_in sa;                          
    struct hostent *hp;                             
    int ret;
    int port;
    char *p;
    struct timeval t;
    fd_set s;
    int connected = 0;
    p = (char *)strchr(host, ':');
    if (p) {
        port = atoi(p + 1);
        *p = '\0';
    } else
        port = PORT_NUMBER;
    hp = gethostbyname(host);
    if (hp == NULL) {
        errorSource = H_ERRNO;
        return -1;
    }
    memcpy((char *)&sa.sin_addr, (char *)hp->h_addr, hp->h_length);
    sa.sin_family = hp->h_addrtype;     
    sa.sin_port = htons(port);          
    sock = socket(hp->h_addrtype, SOCK_STREAM, 0);
    if (sock == -1) {
        errorSource = ERRNO;
        return -1;
    }
    fcntl(sock, F_SETFL, O_NONBLOCK);
    ret = connect(sock, (struct sockaddr *)&sa, sizeof(sa));
    if (ret < 0) {
        if ((errno == EINPROGRESS) || (errno == EAGAIN)) {
            FD_ZERO( &s );
            FD_SET( sock, &s );
            t.tv_sec = 10;
            t.tv_usec = 0;
            if ( select(sock + 1, NULL, &s, NULL, &t) > 0) {
                connected = 1;
            } else
                connected = 0;
        }
    } else {
        connected = 1;
    }
    if (connected) {
        fcntl(sock, F_SETFL, 0);
        return sock;
    } else {
        close(sock);
        errorSource = ERRNO;
        return -1;
    }
}
int makeSocket ( const char * host ) { int sock ; struct sockaddr_in sa ; struct hostent * hp ; int ret ; int port ; char * p ; struct timeval t ; fd_set s ; int connected = 0 ; p = ( char * ) strchr ( host , ' ' ) ; if ( p ) { port = atoi ( p + 1 ) ; * p = ' \0 ' ; } else port = PORT_NUMBER ; hp = gethostbyname ( host ) ; if ( hp == NULL ) { errorSource = H_ERRNO ; return -1 ; } memcpy ( ( char * ) & sa . sin_addr , ( char * ) hp -> h_addr , hp -> h_length ) ; sa . sin_family = hp -> h_addrtype ; sa . sin_port = htons ( port ) ; sock = socket ( hp -> h_addrtype , SOCK_STREAM , 0 ) ; if ( sock == -1 ) { errorSource = ERRNO ; return -1 ; } fcntl ( sock , F_SETFL , O_NONBLOCK ) ; ret = connect ( sock , ( struct sockaddr * ) & sa , sizeof ( sa ) ) ; if ( ret < 0 ) { if ( ( errno == EINPROGRESS ) || ( errno == EAGAIN ) ) { FD_ZERO ( & s ) ; FD_SET ( sock , & s ) ; t . tv_sec = 10 ; t . tv_usec = 0 ; if ( select ( sock + 1 , NULL , & s , NULL , & t ) > 0 ) { connected = 1 ; } else connected = 0 ; } } else { connected = 1 ; } if ( connected ) { fcntl ( sock , F_SETFL , 0 ) ; return sock ; } else { close ( sock ) ; errorSource = ERRNO ; return -1 ; } }
Opens a TCP socket and returns the descriptor
 Returns:
  socket descriptor, or
  -1 on error
Opens a TCP socket and returns the descriptor Returns : socket descriptor or - 1 on error
/* Socket descriptor */ /* Socket address */ /* Host entity */ /* Check for port number specified in URL */ /* Copy host address from hostent to (server) socket address */ /* Set service sin_family to PF_INET */ /* Put portnum into sockaddr */ //printf("errno %d\n",errno); //perror("connect error");