Linux 网络编程: xinetd time
前言
终于把 xinetd 服务装好了,那就在来实现一下 TCP 协议从服务器和本机获取时间吧。那么多思想汇报还没写,我也是醉了。
安装 xinetd
apt-get install xinetd
配置开启 time 服务
vi /etc/xinetd.d/time
把 disable = yes
改成 disable = no
,看注释很清楚 time 服务返回的是1900年1月1日到现在的秒数。如果没有写的权限,就要 chmod
chmod 777 time
重启 xinetd 服务
service xinetd restart
客户端
errexit.c
/* errexit.c - errexit */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
/*------------------------------------------------------------------------
* errexit - print an error message and exit
*------------------------------------------------------------------------
*/
int errexit(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(1);
}
errno.h
/* Error constants. Linux specific version.
Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#ifdef _ERRNO_H
# undef EDOM
# undef EILSEQ
# undef ERANGE
# include <linux/errno.h>
/* Linux has no ENOTSUP error code. */
# define ENOTSUP EOPNOTSUPP
/* Linux also had no ECANCELED error code. Since it is not used here
we define it to an invalid value. */
# ifndef ECANCELED
# define ECANCELED 125
# endif
# ifndef __ASSEMBLER__
/* Function to get address of global `errno' variable. */
extern int *__errno_location (void) __THROW __attribute__ ((__const__));
# if !defined _LIBC || defined _LIBC_REENTRANT
/* When using threads, errno is a per-thread value. */
# define errno (*__errno_location ())
# endif
# endif /* !__ASSEMBLER__ */
#endif /* _ERRNO_H */
#if !defined _ERRNO_H && defined __need_Emath
/* This is ugly but the kernel header is not clean enough. We must
define only the values EDOM, EILSEQ and ERANGE in case __need_Emath is
defined. */
# define EDOM 33 /* Math argument out of domain of function. */
# define EILSEQ 84 /* Illegal byte sequence. */
# define ERANGE 34 /* Math result not representable. */
#endif /* !_ERRNO_H && __need_Emath */
connectsock.c
/* connectsock.c - connectsock */
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif /* INADDR_NONE */
extern int errno;
int errexit(const char *format, ...);
/*------------------------------------------------------------------------
* connectsock - allocate & connect a socket using TCP or UDP
*------------------------------------------------------------------------
*/
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;
}
connectTCP.c
/* connectTCP.c - connectTCP */
int connectsock(const char *host, const char *service,
const char *transport);
/*------------------------------------------------------------------------
* connectTCP - connect to a specified TCP service on a specified host
*------------------------------------------------------------------------
*/
int connectTCP(const char *host, const char *service )
/*
* Arguments:
* host - name of host to which connection is desired
* service - service associated with the desired port
*/
{
return connectsock( host, service, "tcp");
}
TCPtime.c
/* TCPtime.c - TCPtime, main */
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
#include <time.h>
extern int errno;
int TCPtime(const char *host, const char *service);
int errexit(const char *format, ...);
int connectTCP(const char *host, const char *service);
#define LINELEN 128
/*------------------------------------------------------------------------
* main - TCP client for DAYTIME service
*------------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
char *host1 = "localhost"; /* host to use if none supplied */
char *host2 = "localhost"; /* host to use if none supplied */
switch (argc) {
case 1:
host1 = "localhost";
host2 = "localhost";
break;
case 2:
host2 = argv[1];
break;
default:
fprintf(stderr, "The number of parameter is to much! Just need the other host~\n");
exit(1);
}
TCPtime(host1, host2);
exit(0);
}
/*------------------------------------------------------------------------
* TCPtime - invoke Daytime on specified host and print results
*------------------------------------------------------------------------
*/
int TCPtime(const char *host1, const char *host2)
{
char buf[LINELEN+1]; /* buffer for one line of text */
int s1, s2, n; /* socket, read count */
time_t time1, time2;
s1 = connectTCP(host1, "time");
s2 = connectTCP(host2, "time");
while( (n = read(s1, (char *)&time1, sizeof(time1))) > 0) {
time1 = ntohl((unsigned long)time1);
time1 -= 2208988800UL;
time1 += 4294967296UL;
printf("time in %s is %s", host1, ctime(&time1));
}
while( (n = read(s2, (char *)&time2, sizeof(time2))) > 0) {
time2 = ntohl((unsigned long long)time2);
time2 -= 2208988800UL;
time2 += 4294967296UL;
printf("time in %s is %s", host2, ctime(&time2));
}
printf("the difference is %d seconds.\n", abs(time1 - time2));
return 0;
}
time_t 类型的长度在32位机器下是32位,在64位机器下是64位,所以在减去 2208988800UL 秒(变成1970年到现在的秒数)后,还要加上2的32次方秒。(我觉得如果能自己实现64位数的网络序转主机序应该也是可以实现的)。
编译运行
Linux 网络编程: xinetd time的更多相关文章
- 【深入浅出Linux网络编程】 "开篇 -- 知其然,知其所以然"
[深入浅出Linux网络编程]是一个连载博客,内容源于本人的工作经验,旨在给读者提供靠谱高效的学习途径,不必在零散的互联网资源中浪费精力,快速的掌握Linux网络编程. 连载包含4篇,会陆续编写发出, ...
- 【linux草鞋应用编程系列】_5_ Linux网络编程
一.网络通信简介 第一部分内容,暂时没法描述,内容实在太多,待后续专门的系列文章. 二.linux网络通信 在linux中继承了Unix下“一切皆文件”的思想, 在linux中要实现网 ...
- Linux 网络编程(IO模型)
针对linux 操作系统的5类IO模型,阻塞式.非阻塞式.多路复用.信号驱动和异步IO进行整理,参考<linux网络编程>及相关网络资料. 阻塞模式 在socket编程(如下图)中调用如下 ...
- linux网络编程 no route to host 解决方案
linux网络编程 no route to host 解决方案 [整合资料] (2013-05-13 21:38:12) 转载▼ 标签: net iptables it 分类: Linux 参考资料h ...
- linux网络编程-(socket套接字编程UDP传输)
今天我们来介绍一下在linux网络环境下使用socket套接字实现两个进程下文件的上传,下载,和退出操作! 在socket套接字编程中,我们当然可以基于TCP的传输协议来进行传输,但是在文件的传输中, ...
- Linux网络编程&内核学习
c语言: 基础篇 1.<写给大家看的C语言书(第2版)> 原书名: Absolute Beginner's Guide to C (2nd Edition) 原出版社: Sams 作者: ...
- linux网络编程_1
本文属于转载,稍有改动,以利于学习. (一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端 网络程序和普通的程序有一个最大的区别是网络程序是由两个 ...
- Linux网络编程入门 (转载)
(一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端 网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户 ...
- Linux网络编程必看书籍推荐
首先要说讲述计算机网络和TCP/IP的书很多. 先要学习网络知识才谈得上编程 讲述计算机网络的最经典的当属Andrew S.Tanenbaum的<计算机网络>第五版,这本书难易适中. &l ...
随机推荐
- 使用border-image实现类似iOS7的1px底边
使用border-image实现类似iOS7的1px底边 iOS7已经发布有一段时间,扁平化设计风格有很多值得称赞的地方,其中有很多设计细节都是值得研究的. 首先,来看下面iOS设置的截图中的bord ...
- Android重启应用程序代码
Intent i = getBaseContext().getPackageManager() .getLaunchIntentForPackage(getBaseContext().getPacka ...
- POJ 1077 HDU 1043 Eight (IDA*)
题意就不用再说明了吧......如此经典 之前想用双向广搜.a*来写,但总觉得无力,现在用IDA*感觉其他的解法都弱爆了..............想法活跃,时间,空间消耗很小,给它跪了 启发式搜索关 ...
- html---textarea初始化时就有个table空格以及tab键操作无效
1 初始化时就有一个tab空格 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRnJlZUFwZQ==/font/5a6L5L2T/fontsize/400 ...
- ORACLE查看当前连接用户的权限信息或者角色信息
关于当前用户的相关信息,可以通过如下语句找到: SQL> select * from all_objects where object_name like 'SESSION%'; OWNER O ...
- linux查看与开启ssh
首先通过物理终端进入到linux上,手工检查ssh发现没运行/etc/init.d/sshd statussshd is stopped手工启动服务,发现报告权限错误./etc/init.d/sshd ...
- IE8,9下的ajax缓存问题
最近在做一个网站的登录注册框,前端使用了jquery.由于sign和login不是在单独的页面上,而是以一个弹出框出现.所以决定使用ajax来实现注册和登录功能.本以为可以一帆风顺,结果在测试的时候发 ...
- JS兼容性问题列表
记录平时遇见的兼容性问题,有更好的解决办法希望各位提出,会随着开发遇到问题而更新,标记为黄色的为未解决和猜测答案 提出时间 问题描述 解决方案 2014/10/22 submit按钮阻止了默认事件不能 ...
- MySQL 基础学习
http://www.w3school.com.cn/sql/ 1.limit x,y 或 limit z :选取从x开始的y条数据 或 选取最开始的 z条数据 , 2.like '%N%' : ...
- K-means算法[聚类算法]
聚类算法k-Means的实现 <?php /* *Kmeans法(聚类算法的实现) */ /* *求误差平方和J */ //----------------------------------- ...