免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1757 | 回复: 2
打印 上一主题 下一主题

【求助】大家能通过TCP/IP卷2的这一程序获取时间吗 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-02 16:18 |只看该作者 |倒序浏览
本帖最后由 bjtulq 于 2011-04-02 17:05 编辑
  1. /*
  2. *Send a UDP datagram to a daytime server on some other host,
  3. *read the reply, and print the time and date on the server.
  4. */
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <errno.h>

  13. #define BUFFSIZE        150
  14. #define SERVER_ADDR        "202.112.10.60"
  15. #define SERVER_PORT        123       
  16. int
  17. main()
  18. {
  19.         struct sockaddr_in serv;
  20.         char        buff[BUFFSIZE];
  21.         int                sockfd, n;
  22.         if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  23.                 printf("socket error");
  24.                
  25.         bzero((char *) &serv, sizeof(serv));
  26.         serv.sin_family = AF_INET;
  27.         serv.sin_addr.s_addr = inet_addr(SERVER_ADDR);
  28.         serv.sin_port = htons(SERVER_PORT);
  29.        
  30.         if (sendto(sockfd, buff, BUFFSIZE, 0,
  31.                                 (struct sockaddr *) &serv, sizeof(serv)) != BUFFSIZE)
  32.                 printf("sendto error");
  33.                
  34.         if((n = recvfrom(sockfd, buff, BUFFSIZE, 0,
  35.                                         (struct sockaddr *) NULL, (socklen_t *) NULL)) < 2)
  36.                 printf("recvfrom error");
  37.         buff[n-2] = 0;
  38.         printf("%s\n", buff);
  39.        
  40.         exit(0);
  41. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-04-05 11:16 |只看该作者
我也试了,没有返回包。不知道怎么回事。

论坛徽章:
0
3 [报告]
发表于 2011-04-05 16:04 |只看该作者
你要把服务器的内容给修改一下才能返回正确的结果~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP