This article is senlie original, reproduced Please retain this address: http://blog.csdn.net/zhengsenlie
/**
* Call getaddrinfo and udp_server over UDP
**/
# Include "unp. H"
# Include <time. h>
Int
Main (INT argc, char ** argv)
{
Int sockfd;
Ssize_t N;
Char buff [maxline];
Time_t ticks;
Socklen_t Len;
Struct sockaddr_storagecliaddr;
// 1. Use udp_server to obtain the socket
If (argc = 2)
Sockfd = udp_server (null, argv [1], null );
Else if (argc = 3)
Sockfd = udp_server (argv [1], argv [2], null );
Else
Err_quit ("Usage: daytimeudpsrv [
// 2. Server loop. Accept customer requests and send responses
For (;;){
Len = sizeof (cliaddr );
// The server is blocked in the recvfrom call and waits for the client to connect
N = recvfrom (sockfd, buff, maxline, 0, (Sa *) & cliaddr, & Len );
Printf ("datainfrom % s \ n", sock_ntop (Sa *) & cliaddr, Len ));
// Send a response
Ticks = Time (null );
Snprintf (buff, sizeof (buff), "%. 24s \ r \ n", ctime (& ticks ));
Sendto (sockfd, buff, strlen (buff), 0, (Sa *) & cliaddr, Len );
// No connection, so you do not need to close the connected socket
}
}
Time Acquisition Program server UDP protocol irrelevant