udp_client.c函数代码:
/* include udp_client */
#include "unp.h"
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);
}
/* end udp_client */
int Udp_client(const char *host, const char *serv, SA **saptr, socklen_t *lenptr)
{
return(udp_client(host, serv, saptr, lenptr));
}
#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#>");
sockfd = Udp_client(argv[1], argv[2], (void **) &sa, &salen);
printf("sending to %s\n", Sock_ntop_host(sa, salen));
Sendto(sockfd, "", 1, 0, sa, salen); /* send 1-byte datagram */
n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = '\0'; /* null terminate */
Fputs(recvline, stdout);
exit(0);
}
/* include udp_connect */
#include "unp.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);
}
/* end udp_connect */
int Udp_connect(const char *host, const char *serv)
{
int n;
if ( (n = udp_connect(host, serv)) < 0) {
err_quit("udp_connect error for %s, %s: %s",
host, serv, gai_strerror(-n));
}
return(n);
}
#include "unp.h"
int main(int argc, char **argv)
{
int sockfd, n;
char recvline[MAXLINE + 1];
if (argc != 3)
err_quit("usage: daytimeudpcli2 <hostname/IPaddress> <service/port#>");
sockfd = Udp_connect(argv[1], argv[2]);
Write(sockfd, "", 1); /* send 1-byte datagram */
n = Read(sockfd, recvline, MAXLINE);
recvline[n] = '\0'; /* null terminate */
Fputs(recvline, stdout);
exit(0);
}
/* include udp_server */
#include "unp.h"
int udp_server(const char *host, const char *serv, socklen_t *addrlenp)
{
int sockfd, n;
struct addrinfo hints, *res, *ressave;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ( (n = getaddrinfo(host, serv, &hints, &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)
continue; /* error - try next one */
if (bind(sockfd, res->ai_addr, res->ai_addrlen) == 0)
break; /* success */
Close(sockfd); /* bind error - close and try next one */
} while ( (res = res->ai_next) != NULL);
if (res == NULL) /* errno from final socket() or bind() */
err_sys("udp_server error for %s, %s", host, serv);
if (addrlenp)
*addrlenp = res->ai_addrlen; /* return size of protocol address */
freeaddrinfo(ressave);
return(sockfd);
}
/* end udp_server */
int Udp_server(const char *host, const char *serv, socklen_t *addrlenp)
{
return(udp_server(host, serv, addrlenp));
}
#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_storage cliaddr;
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 [ <host> ] <service or port>");
for ( ; ; ) {
len = sizeof(cliaddr);
n = Recvfrom(sockfd, buff, MAXLINE, 0, (SA *)&cliaddr, &len);
printf("datagram from %s\n", Sock_ntop((SA *)&cliaddr, len));
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
Sendto(sockfd, buff, strlen(buff), 0, (SA *)&cliaddr, len);
}
}
➜ names ./daytimeudpsrv2 1399
datagram from 127.0.0.1:52995
datagram from 127.0.0.1:50425
➜ names ./daytimeudpcli1 127.0.0.1 1399
sending to 127.0.0.1
Tue May 5 10:05:58 2015
➜ names ./daytimeudpcli2 127.0.0.1 1399
Tue May 5 10:06:04 2015