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);
    }
  • 相关阅读:
    总结:关于作用域的经典面试题
    解决JS拖拽出现的问题
    JS正则(3)总结
    JS正则(2)对正则的理解
    JS 闭包 正则(1)
    JS Date对象
    笔记
    9.13笔记
    9.12学习笔记
    9.11学习笔记
  • 原文地址:https://www.cnblogs.com/soldierback/p/10744875.html
Copyright © 2011-2022 走看看