socketp.c
/* create a client socket connected to PORT on HOSTNAME */
create a client socket connected to PORT on HOSTNAME
create a client socket connected to PORT on HOSTNAME
int create_client_socket(char *bind_address, char *host, char *service,
                         struct sockaddr *addr, socklen_t *addrlen)
{
    struct addrinfo hints ;
    struct addrinfo *res ;
    struct addrinfo *ressave ;
    struct addrinfo *bindres ;
    int n ;
    int s ;
    memset(&hints, 0, sizeof(hints)) ;
    hints.ai_family = protocol_family ;
    hints.ai_socktype = SOCK_STREAM ;
    if ((n = resolve_hostname_and_service(host, service, &hints, &res)) != 0) {
        fprintf(stderr, "%s: failed to resolve address or service: %s\n",
                progname, gai_strerror(n)) ;
        exit(5) ;
    }
    ressave = res ;
    do {
        if ((s = socket(res->ai_family, res->ai_socktype,
                        res->ai_protocol)) < 0) {
            continue ;
        }
        if (bind_address != NULL) {
            memset(&hints, 0, sizeof(hints)) ;
            hints.ai_flags = AI_PASSIVE ;
            hints.ai_family = res->ai_family ;
            hints.ai_socktype = SOCK_STREAM ;
            if ((n = resolve_hostname_and_service(bind_address, NULL, &hints,
                                                  &bindres)) != 0) {
                continue ;
            }
            if (bind(s, bindres->ai_addr, bindres->ai_addrlen) < 0) {
                close(s) ;
                freeaddrinfo(bindres) ;
                continue ;
            }
            freeaddrinfo(bindres) ;
        }
        if (verboseflag >= 2) {
            fprintf(stderr, "trying to connect to %s ... ",
                    get_ipaddr(res->ai_addr, res->ai_addrlen)) ;
        }
        alarm(timeout) ;
        if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
            if (verboseflag >= 2) {
                fprintf(stderr, "failed\n") ;
            }
            alarm(0) ;
            close(s) ;
            continue ;
        }
        if (verboseflag >= 2) {
            fprintf(stderr, "succeeded\n") ;
        }
        alarm(0) ;
        if (*addrlen > res->ai_addrlen) {
            *addrlen = res->ai_addrlen ;
        }
        memcpy(addr, res->ai_addr, *addrlen) ;
        break ;
    } while ((res = res->ai_next) != NULL) ;
    if (res == NULL) {
        perror2("failed to create or connect socket") ;
        s = -1 ;
    }
    freeaddrinfo(ressave) ;
    return s ;
}
int create_client_socket ( char * bind_address , char * host , char * service , struct sockaddr * addr , socklen_t * addrlen ) { struct addrinfo hints ; struct addrinfo * res ; struct addrinfo * ressave ; struct addrinfo * bindres ; int n ; int s ; memset ( & hints , 0 , sizeof ( hints ) ) ; hints . ai_family = protocol_family ; hints . ai_socktype = SOCK_STREAM ; if ( ( n = resolve_hostname_and_service ( host , service , & hints , & res ) ) != 0 ) { fprintf ( stderr , " \n " , progname , gai_strerror ( n ) ) ; exit ( 5 ) ; } ressave = res ; do { if ( ( s = socket ( res -> ai_family , res -> ai_socktype , res -> ai_protocol ) ) < 0 ) { continue ; } if ( bind_address != NULL ) { memset ( & hints , 0 , sizeof ( hints ) ) ; hints . ai_flags = AI_PASSIVE ; hints . ai_family = res -> ai_family ; hints . ai_socktype = SOCK_STREAM ; if ( ( n = resolve_hostname_and_service ( bind_address , NULL , & hints , & bindres ) ) != 0 ) { continue ; } if ( bind ( s , bindres -> ai_addr , bindres -> ai_addrlen ) < 0 ) { close ( s ) ; freeaddrinfo ( bindres ) ; continue ; } freeaddrinfo ( bindres ) ; } if ( verboseflag >= 2 ) { fprintf ( stderr , " " , get_ipaddr ( res -> ai_addr , res -> ai_addrlen ) ) ; } alarm ( timeout ) ; if ( connect ( s , res -> ai_addr , res -> ai_addrlen ) < 0 ) { if ( verboseflag >= 2 ) { fprintf ( stderr , " \n " ) ; } alarm ( 0 ) ; close ( s ) ; continue ; } if ( verboseflag >= 2 ) { fprintf ( stderr , " \n " ) ; } alarm ( 0 ) ; if ( * addrlen > res -> ai_addrlen ) { * addrlen = res -> ai_addrlen ; } memcpy ( addr , res -> ai_addr , * addrlen ) ; break ; } while ( ( res = res -> ai_next ) != NULL ) ; if ( res == NULL ) { perror2 ( " " ) ; s = -1 ; } freeaddrinfo ( ressave ) ; return s ; }
create a client socket connected to PORT on HOSTNAME
create a client socket connected to PORT on HOSTNAME