红联Linux门户
Linux帮助

【求助】大家能通过TCP/IP卷2这个程序获取时间吗

发布时间:2011-04-02 17:07:22来源:红联作者:bjtulq
感觉这个程序不给力啊,难道斯蒂芬没测过。感觉不如用ntpdate 202.112.10.60[code]/*
*Send a UDP datagram to a daytime server on some other host,
*read the reply, and print the time and date on the server.
*/
#include
#include
#include
#include
#include
#include
#include
#include

#define BUFFSIZE 150
#define SERVER_ADDR "202.112.10.60"
#define SERVER_PORT 123
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(SERVER_ADDR);
serv.sin_port = htons(SERVER_PORT);

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, (socklen_t *) NULL)) < 2)
printf("recvfrom error");
buff[n-2] = 0;
printf("%s\n", buff);

exit(0);
}[/code]
文章评论

共有 2 条评论

  1. hml1006 于 2011-04-06 10:07:22发表:

    没看过

  2. ubuntulover 于 2011-04-03 22:41:31发表:

    明天试试去