设为首页 加入收藏

TOP

Linux下echo与time服务的程序实现(四)
2015-07-16 12:56:06 来源: 作者: 【 】 浏览:13
Tags:Linux echo time 服务 程序 实现
;


int errexit(const char *format,...);
int connectsock(const char *host, const char *service, const char *transport );
int connectUDP(const char *host, const char *service);


int main(int argc, char *argv[]){
? ? char *host= "localhost";
? ? char *service= "time";
? ? time_t now;
? ? int s,n;


? ? switch(argc){
case 1:
? ? host = "localhost";
? ? break;
case 3:
? ? service = argv[2];
case 2:
? ? host=argv[1];
? ? break;
default:
? ? fprintf(stderr,"usage: UDPtime[host[port]]\n");
? ? exit(1);
? ? }


? ? s=connectUDP(host,service);
? ? (void)write(s,MSG,strlen(MSG));


? ? n=read(s,(char *)&now,sizeof(now));
? ? if(n<0)? ? errexit("read failed: %s\n",strerror(errno));
? ? now=ntohl((unsigned long)now);
? ? now-=UNIXEPOCH;
? ? printf("%s",ctime(&now));
? ? exit(0);
}
int errexit(const char *format,...){
? ? va_list arg;
? ? va_start(arg, format);
? ? vfprintf(stderr,format,arg);
? ? va_end(arg);
? ? exit(1);
}
int connectsock(const char *host, const char *service, const char *transport )
/*
?* Arguments:
?*? ? ? host? ? ? - name of host to which connection is desired
?*? ? ? service? - service associated with the desired port
?*? ? ? transport - name of transport protocol to use ("tcp" or "udp")
?*/
{
? ? struct hostent? *phe;? /* pointer to host information entry? ? */
? ? struct servent? *pse;? /* pointer to service information entry */
? ? struct protoent *ppe;? /* pointer to protocol information entry*/
? ? struct sockaddr_in sin; /* an Internet endpoint address? ? */
? ? int s, type;? ? /* socket descriptor and socket type? ? */



? ? memset(&sin, 0, sizeof(sin));
? ? sin.sin_family = AF_INET;


? ? /* Map service name to port number */
? ? if ( pse = getservbyname(service, transport) )
? ? ? ? sin.sin_port = pse->s_port;
? ? else if ((sin.sin_port=htons((unsigned short)atoi(service))) == 0)
? ? ? ? errexit("can't get \"%s\" service entry\n", service);


? ? /* Map host name to IP address, allowing for dotted decimal */
? ? if ( phe = gethostbyname(host) )
? ? ? ? memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
? ? else if ( (sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE )
? ? ? ? errexit("can't get \"%s\" host entry\n", host);


? ? /* Map transport protocol name to protocol number */
? ? if ( (ppe = getprotobyname(transport)) == 0)
? ? ? ? errexit("can't get \"%s\" protocol entry\n", transport);


? ? /* Use protocol to choose a socket type */
? ? if (strcmp(transport, "udp") == 0)
? ? ? ? type = SOCK_DGRAM;
? ? else
? ? ? ? type = SOCK_STREAM;


? ? /* Allocate a socket */
? ? s = socket(PF_INET, type, ppe->p_proto);
? ? if (s < 0)
? ? ? ? errexit("can't create socket: %s\n", strerror(errno));


? ? /* Connect the socket */
? ? if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
? ? ? ? errexit("can't connect to %s.%s: %s\n", host, service,
? ? ? ? ? ? strerror(errno));
? ? return s;
}
int connectUDP(const char *host, const char *service){
? ? return connectsock(host,service,"udp");
}


四、针对TIME服务的UDP服务器端软件的实现


1.网络拓扑结构:



2.源码:


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


#define UNIXEPOCH 2208988800UL


extern int errno;
unsigned short portbase = 0;


int errexit(const char *format,...);
int passivesock(const char *service, const char *transport, int qlen);
int passiveUDP(const char *service);


int main(int argc, char *argv[]){
? ? char *service= "time";
? ? struct sockaddr_in fsin;
? ? char buf[1];
? ? int sock;
? ? time_t now;
? ? unsigned int alen;


? ? switch(argc){
case 1:
? ? break;
case 2:
? ? service=argv[1];
? ? break;
default:
? ? errexit("usage: UDPtimed [port]\n");
? ? }


? ? sock=passiveUDP(service);


? ? while(1){
? ? ? ? alen=sizeof(fsin);
? ?

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux C语言Select函数使用求解释 下一篇Java的Hashtable在遍历时的迭代器..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: