#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 ");
sockfd=Udp_client(argv[1], argv[2], (void **)&sa, &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(&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(sockfdai_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(&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(sockfdai_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
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: JVM虚拟机系统性学习-运行时数据区(方法区、程序计数器、直接内存)
方法区 方法区本质上是 Java 编译后代码的存储区域,存储了每一个类的结构信息,如:运行时常量池、成员变量、方法、构造方法和普通方法的字节码指令等内容 方法区主要存储的数据如下: Class 类型信息,如该 Class 为 class 类、接口、枚举、注解,…