socketp.c
/*
 * create a server socket on PORT accepting QUEUE_LENGTH connections
 */
create a server socket on PORT accepting QUEUE_LENGTH connections
create a server socket on PORT accepting QUEUE_LENGTH connections
int create_server_socket(char *bind_address, char *service, int queue_length,
                         struct sockaddr *addr, socklen_t *addrlen)
{
    struct addrinfo hints ;
    struct addrinfo *res ;
    struct addrinfo *ressave ;
    int n ;
    int s ;
    int one = 1 ;
    memset(&hints, 0, sizeof(hints)) ;
    hints.ai_flags = AI_PASSIVE ;
    hints.ai_family = protocol_family ;
    hints.ai_socktype = SOCK_STREAM ;
    if ((n = resolve_hostname_and_service(bind_address, service, &hints, &res))
        != 0) {
        fprintf(stderr, "%s: failed to resolve bind 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 (Reuseflag) {            
            if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&one,
                           sizeof(one)) < 0) {
                close(s) ;
                continue ;
            }
        }
        if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
            close(s) ;
            continue ;
        }
        if (listen(s, queue_length) < 0) {
            close(s) ;
            continue ;
        }
        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 listen socket") ;
        s = -1 ;
    }
    freeaddrinfo(ressave) ;
    return s ;
}
int create_server_socket ( char * bind_address , char * service , int queue_length , struct sockaddr * addr , socklen_t * addrlen ) { struct addrinfo hints ; struct addrinfo * res ; struct addrinfo * ressave ; int n ; int s ; int one = 1 ; memset ( & hints , 0 , sizeof ( hints ) ) ; hints . ai_flags = AI_PASSIVE ; hints . ai_family = protocol_family ; hints . ai_socktype = SOCK_STREAM ; if ( ( n = resolve_hostname_and_service ( bind_address , 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 ( Reuseflag ) { if ( setsockopt ( s , SOL_SOCKET , SO_REUSEADDR , ( char * ) & one , sizeof ( one ) ) < 0 ) { close ( s ) ; continue ; } } if ( bind ( s , res -> ai_addr , res -> ai_addrlen ) < 0 ) { close ( s ) ; continue ; } if ( listen ( s , queue_length ) < 0 ) { close ( s ) ; continue ; } 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 server socket on PORT accepting QUEUE_LENGTH connections
create a server socket on PORT accepting QUEUE_LENGTH connections
/* This is default. */