Time Acquisition Program client UDP protocol irrelevant

Source: Internet
Author: User
This article is senlie original, reproduced Please retain this address: http://blog.csdn.net/zhengsenlie

/**
* UDP: the Protocol is irrelevant. Call getaddrinfo and udp_client.
**/
# Include "unp. H"


Int
Udp_client (const char * Host, const char * serv, sa ** saptr, socklen_t * lenp)
{
Int sockfd, N;
Struct addrinfohints, * res, * ressave;


// 1. Call getaddrinfo
// The Protocol address family is af_unspec, And the socket type is sock_dgram.
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;


// 2. Try the structure of each addrinfo until it is successful or reaches the end of the linked list.
Do {
// Create a socket
Sockfd = socket (res-> ai_family, res-> ai_socktype, res-> ai_protocol );
If (sockfd> = 0)
Break;/* success */
} While (RES = res-> ai_next )! = NULL );


// 3. If getaddrinfo fails or the socket creation fails, this function will terminate
If (RES = NULL)/* errno set from final socket ()*/
Err_sys ("udp_client error for % s, % s", host, SERV );


// 4. Save the server socket address Structure
* Saptr = malloc (res-> ai_addrlen );
Memcpy (* saptr, res-> ai_addr, res-> ai_addrlen );
* Lenp = res-> ai_addrlen;


// 5. Call freeaddrinfo to clear the dynamic storage space returned by getaddrinfo
Freeaddrinfo (ressave );


// 6. Return socket
Return (sockfd );
}
/* End udp_client */


Int
Udp_client (const char * Host, const char * serv, sa ** saptr, socklen_t * lenptr)
{
Return (udp_client (host, serv, saptr, lenptr ));
}


/**
* UDP: the Protocol is irrelevant. Call getaddrinfo and udp_client.
**/
# 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: daytimeudpcli1 <Hostname/IPaddress> <service/port #> ");


// 1. Use udp_client to obtain the UDP socket and server socket address Structure
Sockfd = udp_client (argv [1], argv [2], (void **) & SA, & salen );


// 2. display the server address
Printf ("sending to % s \ n", sock_ntop_host (SA, salen ));


// 3. Send a 1-byte Datagram
Sendto (sockfd, "", 1, 0, SA, salen);/* send 1-byte datasync */


// 4. Read and display the response Datagram
N = recvfrom (sockfd, recvline, maxline, 0, null, null );
Recvline [N] = '\ 0';/* null terminate */
Fputs (recvline, stdout );


Exit (0 );
}

Time Acquisition Program client UDP protocol irrelevant

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.