udp_client函数

#include    <netdb.h>
#include    <stdlib.h>
#include    <string.h>
#include    <sys/socket.h>

#define     SA     struct sockaddr

int
udp_client(const char *host, const char *serv, SA **saptr, socklen_t *lenp)
{
    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_client 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) {
            break;        /* success */
        }
    } while ( (res = res->ai_next) != NULL);

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

    *saptr = malloc(res->ai_addrlen);
    memcpy(*saptr, res->ai_addr, res->ai_addrlen);
    *lenp = res->ai_addrlen;

    freeaddrinfo(ressave);

    return(sockfd);
}

 

上一篇:java生成二维码以及读取案例


下一篇:anetTcpGenericConnect 详解