zoukankan      html  css  js  c++  java
  • udp_connect函数

    #include    <netdb.h>
    #include    <stdlib.h>
    #include    <unistd.h>
    #include    <string.h>
    #include    <sys/socket.h>
    
    
    int
    udp_connect(const char *host, const char *serv)
    {
        int                sockfd, n;
        struct addrinfo    hints, *res, *ressave;
    
        bzero(&hints, sizeof(struct addrinfo));
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_DGRAM;
    
        if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0) {
            err_quit (
                "udp_connect error for %s, %s: %s",
                host, serv, gai_strerror(n)
            );
        }
        ressave = res;
    
        do {
            sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
            if (sockfd < 0) {
                continue;    /* ignore this one */
            }
    
            if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0) {
                break;        /* success */
            }
    
            close(sockfd);    /* ignore this one */
        } while ( (res = res->ai_next) != NULL);
    
        if (res == NULL) {    /* errno set from final connect() */
            err_sys("udp_connect error for %s, %s", host, serv);
        }
    
        freeaddrinfo(ressave);
    
        return(sockfd);
    }
  • 相关阅读:
    Chp18: Hard
    内存泄漏 和 内存溢出
    Chp4: Trees and Graphs
    trie树--详解
    Tries
    Comparable & Comparator
    memory leak
    8个月从CS菜鸟到拿到Google Offer的经历+内推
    Word Ladder II
    Java垃圾回收机制
  • 原文地址:https://www.cnblogs.com/soldierback/p/10744875.html
Copyright © 2011-2022 走看看