#include "unp.h"

int main(int argc, char **argv)
{
    int sockfd, n;
    char recvline[MAXLINE+1];
    socklen_t salen;
    struct sockaddr *sa;

    if(argc!=3)
        err_quit("usage: daytimeudpclil <hostname/IPaddress&gt;<service/port#&gt;");
    
    sockfd=Udp_client(argv[1], argv[2], (void **)&amp;sa, &amp;salen);
    printf("sending to %sn", Sock_ntop_host(sa, salen));
    Sendto(sockfd, "", 1 ,0, sa ,salen);
    n=Recvfrom(sockfd, recvlien, MAXLINE, 0, NULL,NULL);
    recvlien[n]='';
    Fputs(recvline, stdout);
    
    return 0;
}

UDP daytime client using our udp_client function

udp_connect’ Function

Our udp_connect function creates a connected UDP socket.

#include "unp.h"
int udp_connect (const char *hostname, const char *service);
#include "unp.h"

int udp_connect(const char *host, const char *serv)
{
    int sockfd, n;
    struct addrinfo hints, *res, *ressave;
    bzero(&amp;hints, sizeof(struct addrinfo));
    hints.ai_family=AF_UNSPEC;
    hints.ai_socktype=SOCK_DGRAM;

    if((n=getaddrinfo(host,serv,&amp;hints, &amp;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;
        if(connect(sockfd, res->ai_addr, res->ai_addrlen)==0)
            break;
        
        Close(sockfd);

    }while((res=res->ai_next)!=NULL);

    if(res==NULL)
        err_sys("udp_connect error for %s, %s", host, serv);
    
    freeaddrinfo(ressave);
    return sockfd;
}

udp_connect function: creates a connected UDP socket

udp_server’ Function

Our final UDP function that provides a simpler interface to getaddrinfo is udp_server.

#include "unp.h"
int udp_server (const char *hostname, const char *service, socklen_t *lenptr);
#include "unp.h"

int udp_server(const char *host, const char *serv, soclen_t *addrlenp)
{
    int sockfd, n;
    struct addrinfo hints, *res, *ressave;

    bzero(&amp;hints, sizeof(struct addrinfo));
    hints.ai_flags=Ai_PASSIVE;
    hints.ai_family=AF_UNSPEC;
    hints.ai_socktype=SOCK_DGRAM;
    if((n=getaddrinfo(host, serv, &amp;hints, &amp;res))!=0)
        err_quit("udp_server 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)
            contnue;
        if(bind(sockfd, res->ai_addr, res->ai_addrlen)==0)
            break;
        
        Close(socfd);
    }while((res=res->ai_next)!=NULL);

    if(res==NULL)
        err_sys("udp_server error for %s, %s", host, serv);

    if(addrlenp)
        *addrlenp=res->ai_addrlen;

    freeaddrinfo(ressave);

    return sockfd;
}

udp_server function: creates an unconnected socket for a UDP server

原文地址:https://blog.csdn.net/myfather103/article/details/82744041

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_47580.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注