#include <sys/socket.h> //提供socket函数及数据结构
#include <netinet/in.h> //定义数据结构sockaddr_in
#include <arpa/inet.h> //提供IP地址转换函数
#include <unistd.h> //提供通用的文件,目录,程序及进程操作的函数
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h> //提供getprotobyname函数
#define LENGTH 128
int connect_call(char *des_IP, int port, char *protocol)
{
struct hostent *host;
struct servent *server;
struct protoent *pro;
struct sockaddr_in addr_server;
int s, type;
memset(&addr_server, 0, sizeof(addr_server));
addr_server.sin_family = AF_INET;
addr_server.sin_port = port;
//通过通信协议的名称来获取该协议的其他信息
pro = getprotobyname(protocol);
//失败则退出程序
if(pro == NULL)
{
printf("can't get \"%s\" protocol entry\n", protocol);
exit(1);
}
//返回对应于给定主机名的主机信息
host = gethostbyname(des_IP);
if(host != NULL)
memcpy(&addr_server.sin_addr, host->h_addr, host->h_length);
//将一个点间隔地址转换成一个in_addr
else if((addr_server.sin_addr.s_addr = inet_addr(des_IP) == 0xffffffff))
{
printf("can't get \"%s\" host entry\n", des_IP);
exit(1);
}
if(protocol == "tcp")
type = SOCK_STREAM;
else
type = SOCK_DGRAM;
//创建一个套接字,失败则退出程序,0表示IP协议
s = socket(PF_INET, type, 0);
if(s < 0)
{
printf("can't create socket\n");
exit(1);
}
//与服务器建立连接并取得socket
if(connect(s, (struct socket *)&addr_server, sizeof(addr_server)) < 0)
{
printf("cant't connect to %s through %d", des_IP, port);
exit(1);
}
return s;
}
//将时间字符串转换成秒并返回
long cal_in_sec(char *time_string)
{
long result;
result = ((time_string[11]) - 48) * 10 + (time_string[12] - 48) * 3600 +
((time_string[14] - 48) * 10 + (time_string[15] - 48)) * 60 +
(time_string[17] + time_string[18] - 96);
return result;
}
Time(char *des_IP, int port)
{
char buf[LENGTH + 1];
int s, n;
char *client_time;
time_t time_client;
int i;
long sec_ser; //服务器的时间,以秒为单位
long sec_cli; //客户机的时间,以秒为单位
long compare; //客户机时间与服务器时间的差
//建立连接
s = connect_call(des_IP, port, "tcp");
while((n = read(s, buf, LENGTH)) > 0)
{
//打印服务器时间
buf[0] = '\0';
printf("Server Time: ");
for(i = 11;i <= 18;i++)
printf("%c", buf[i]);
printf("\n");
}
//打印客户机的时间
time(&time_client);
client_time = ctime(&time_client);
printf("Client Time: ");
for(i = 11; i <= 18;i++)
printf("%s", client_time);
printf("\n");
sec_ser = cal_in_sec(buf);
sec_cli = cal_in_sec(client_time);
//显示比较结果
compare = sec_cli - sec_ser;
printf("The transport time between server and client is %ld seconds\n", compare);
}
int main(int argc, char *argv[])
{
char *des_IP;
int port;
if(argc != 2)
{
printf("The number of parameter is wrong!\n");
exit(1);
}
else
{
//参数为服务器IP
des_IP = argv[1];
port = 13;
Time(des_IP, port);
}
exit(0);
}
没有合适的资源?快使用搜索试试~ 我知道了~
计算机网络课程设计—socket编程之服务器与客户机的时间比较
共2个文件
c:2个
5星 · 超过95%的资源 需积分: 12 20 下载量 199 浏览量
2010-06-16
14:51:31
上传
评论
收藏 3KB RAR 举报
温馨提示
针对TIME服务的TCP客户机及服务器的实现。程序把TIME服务返回的时间值和接收到应答时客户机的本机时间值相比较,并显示比较结果
资源推荐
资源详情
资源评论
收起资源包目录
上机.rar (2个子文件)
TimeClient.c 3KB
TimeServer.c 3KB
共 2 条
- 1
资源评论
- WaNioN-往年2015-07-26写的不错,高等计算机网络作业
- libogu07062014-10-21收获很大,代码是linux环境下的
- himawariegao2014-10-15linux平台两个文件编译都没问题,非常感谢
- dayfly4112015-12-01可以借鉴的资源
- brankyu2014-10-15还好吧,收获很大,代码是linux环境下的,要是windows环境下的就好了,不过已经更改的可以运行了
jinlai_ch
- 粉丝: 0
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功