Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
socket ntp连接,打开socket出错
发布于 2019-02-15 14:56:40 浏览:2065
订阅该版
我使用rtthread的ntp例程,使用过程中建立的socket成功几次后就打不开了,什么原因了?调试信息如下图: ![ntp.png](/uploads/201902/15/145535mlodydc5yypyyyg7.png) 程序如下: ```time_t ntp_get_time(const char *host_name) { rt_kprintf("
-----------------begin
"); int sockfd, n; // Socket file descriptor and the n return result from writing/reading from the socket. int portno = 123; // NTP UDP port number. time_t new_time = 0; struct timeval timeout; // Using default host name when host_name is NULL if (host_name == NULL) { host_name = NTP_HOSTNAME; } // Create and zero out the packet. All 48 bytes worth. memset(&packet, 0, sizeof(ntp_packet)); // Set the first byte's bits to 00,011,011 for li = 0, vn = 3, and mode = 3. The rest will be left set to zero. *((char *) &packet + 0) = 0x1b; // Represents 27 in base 10 or 00011011 in base 2. // Create a UDP socket, convert the host-name to an IP address, set the port number, // connect to the server, send the packet, and then read in the return packet. struct sockaddr_in serv_addr; // Server address data structure. struct hostent *server; // Server data structure. sockfd = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP); // Create a UDP socket. if (sockfd < 0) { ntp_error("opening socket %d",rt_get_errno()); return 0; } timeout.tv_sec = NTP_GET_TIMEOUT; timeout.tv_usec = 0; /* set receive and send timeout option */ setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *) &timeout, sizeof(timeout)); setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *) &timeout, sizeof(timeout)); // server = gethostbyname(host_name); // Convert URL to IP. // if (server == NULL) { // ntp_error("no such host"); // goto __exit; // } // Zero out the server address structure. memset((char *) &serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; // Copy the server's IP address to the server address structure. serv_addr.sin_addr.s_addr = inet_addr("192.168.94.111"); //memcpy((char *) &serv_addr.sin_addr.s_addr, (char *) server->h_addr, server->h_length); // Convert the port number integer to network big-endian style and save it to the server address structure. serv_addr.sin_port = htons(portno); // Call up the server using its IP address and port number. if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { ntp_error("connecting"); goto __exit; } // Send it the NTP packet it wants. If n == -1, it failed. n = send(sockfd, (char*) &packet, sizeof(ntp_packet), 0); if (n < 0) { ntp_error("writing to socket"); goto __exit; } // Wait and receive the packet back from the server. If n == -1, it failed. n = recv(sockfd, (char*) &packet, sizeof(ntp_packet), 0); if (n < 0) { if (errno == EWOULDBLOCK || errno == EAGAIN) { ntp_error("receive the socket timeout(%ds)", NTP_GET_TIMEOUT); } else { ntp_error("reading from socket, error code %d.", n); } goto __exit; } // These two fields contain the time-stamp seconds as the packet left the NTP server. // The number of seconds correspond to the seconds passed since 1900. // ntohl() converts the bit/byte order from the network's to host's "endianness". packet.txTm_s = ntohl(packet.txTm_s); // Time-stamp seconds. packet.txTm_f = ntohl(packet.txTm_f); // Time-stamp fraction of a second. // Extract the 32 bits that represent the time-stamp seconds (since NTP epoch) from when the packet left the server. // Subtract 70 years worth of seconds from the seconds since 1900. // This leaves the seconds since the UNIX epoch of 1970. // (1900)------------------(1970)**************************************(Time Packet Left the Server) new_time = (time_t) (packet.txTm_s - NTP_TIMESTAMP_DELTA); __exit: //shutdown(sockfd, 0); closesocket(sockfd); rt_kprintf("
-----------------end
"); return new_time; }```
查看更多
3
个回答
默认排序
按发布时间排序
flyboy
2019-02-20
Do my self();
楼主问题解决了吗
moss
2019-02-20
这家伙很懒,什么也没写!
能 ping 通 host 吗?
guoweilkd
2019-05-10
这家伙很懒,什么也没写!
>楼主问题解决了吗 --- 解决了 版本不兼容造成的
撰写答案
登录
注册新账号
关注者
0
被浏览
2.1k
关于作者
guoweilkd
这家伙很懒,什么也没写!
提问
18
回答
10
被采纳
0
关注TA
发私信
相关问题
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
GD32F450 片内 flash驱动适配
2
STM32H7R7运行CherryUSB
3
RT-Smart首次线下培训,锁定2024 RT-Thread开发者大会!
4
使用RC522软件包驱动FM1722
5
常量数据类型和表达式陷阱分享
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
UART
WIZnet_W5500
ota在线升级
freemodbus
PWM
flash
cubemx
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
中断
编译报错
Debug
SFUD
rt_mq_消息队列_msg_queue
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
a1012112796
10
个答案
1
次被采纳
踩姑娘的小蘑菇
4
个答案
1
次被采纳
红枫
4
个答案
1
次被采纳
张世争
4
个答案
1
次被采纳
Ryan_CW
4
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
YZRD
2
篇文章
5
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
Woshizhapuren
1
篇文章
5
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部