码迷,mamicode.com
首页 > 其他好文 > 详细

tcp_listen函数

时间:2019-04-21 14:25:27      阅读:454      评论:0      收藏:0      [点我收藏+]

标签:pass   fine   address   toc   mil   pre   protocol   char   style   

#include    <netdb.h>
#include    <unistd.h>
#include    <stddef.h>
#include    <strings.h>
#include    <sys/socket.h>

#define     LISTENQ     1024

int 
tcp_listen(const char *host, const char *serv, socklen_t *addrlenp)
{
    int                listenfd, n;
    const int        on = 1;
    struct addrinfo    hints, *res, *ressave;

    bzero(&hints, sizeof(struct addrinfo));
    hints.ai_flags = AI_PASSIVE;
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0) {
        err_quit (
            "tcp_listen error for %s, %s: %s",
            host, serv, gai_strerror(n)
        );
    }
    ressave = res;

    do {
        listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
        if (listenfd < 0) {
            continue;        /* error, try next one */
        }

        setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
        if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0) {
            break;            /* success */
        }

        close(listenfd);    /* bind error, close and try next one */
    } while ( (res = res->ai_next) != NULL);

    if (res == NULL) {        /* errno from final socket() or bind() */
        err_sys("tcp_listen error for %s, %s", host, serv);
    }

    listen(listenfd, LISTENQ);

    if (addrlenp) {
        *addrlenp = res->ai_addrlen;    /* return size of protocol address */
    }

    freeaddrinfo(ressave);

    return(listenfd);
}

 

tcp_listen函数

标签:pass   fine   address   toc   mil   pre   protocol   char   style   

原文地址:https://www.cnblogs.com/soldierback/p/10744882.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!