ubuntu下启动时间服务

一、启动时间服务(time服务的知名端口是13)

1、发现xinetd服务还未安装,可以用apt-get install xinetd安装


2、vi编辑/etc/xinetd.d/daytime文件,将disable = yes改为disable = no


3、注销系统或重启xinetd服务,用service xinetd stop然后service xinetd start


4、查看网络状态netstat -ant,即可看到


tcp        0      0 0.0.0.0:13              0.0.0.0:*               LISTEN


这是daytime服务的网络监听端口状态


二、编写一个c程序测试

time.c

/*
 * Send a UDP datagram to the daytime server on some other host,
 * read the reply,and print the time and the date on the server.
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 





#define BUFFSIZE 150	/* arbitrary size */
int
main()
{
    struct sockaddr_in serv;
    char   buff[BUFFSIZE];
    int    sockfd, n;
    
    if((sockfd = socket(PF_INET, SOCK_DGRAM, 0))<0)
	printf("socket error");

    bzero((char *) &serv, sizeof(serv));
    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = inet_addr("121.249.151.123");
    serv.sin_port = htons(13);

    if(sendto(sockfd, buff, BUFFSIZE, 0, (struct sockaddr *) &serv, sizeof(serv)) != BUFFSIZE)
	printf("sendto error");


    if((n = recvfrom(sockfd, buff, BUFFSIZE, 0, (struct sockaddr *) NULL,(int *)NULL)) < 2)
	printf("recvfrom error");

    buff[n - 2] = 0;	/* null terminate */

    printf("%s\n", buff);


    exit(0);

}

编译:gcc -o time.c time

运行:./time







你可能感兴趣的:(Ubuntu)