嵌入式设备网络一般都是自己配置的,hi3516的网络配置在/etc/init.d/rcS 这个文件中。
这是他的初始化配置。
但是网络ping 百度还是ping不通,是因为dns服务器没有配置。
配置DNS后,测试可以正常ping 通域名。
在/etc下添加resolv.conf文件,并在文件中至少添加一个有效DNS服务器地址, 则域名访问成功。
内容如图所示。
屁话不多说,直接上代码,调用ntpdate 函数就好,返回时间。
#include
#include
#include
#include
#include
#include
#include
#include
#include
int get_yue(char *secons);
void ntpdate();
#include
using namespace std;
string getFormatTime(int year, int mon, int day, int hour, int min, int sec, int type);
void get_alarm_time();
int main()
{
ntpdate();
return 0;
}
void ntpdate()
{
//char *hostname=(char *)"163.117.202.33";
//char *hostname=(char *)"pool.ntp.br";
char *hostname = (char *)"200.20.186.76";
int portno = 123; //NTP is port 123
int maxlen = 1024; //check our buffers
int i; // misc var i
unsigned char msg[48] = {010, 0, 0, 0, 0, 0, 0, 0, 0}; // the packet we send
unsigned long buf[maxlen]; // the buffer we get back
//struct in_addr ipaddr; //
struct protoent *proto; //
struct sockaddr_in server_addr;
int s; // socket
long tmit; // the time -- This is a time_t sort of
//use Socket;
//
//#we use the system call to open a UDP socket
//socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "socket: $!";
proto = getprotobyname("udp");
s = socket(PF_INET, SOCK_DGRAM, proto->p_proto);
perror("socket");
//
//#convert hostname to ipaddress if needed
//$ipaddr = inet_aton($HOSTNAME);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(hostname);
//argv[1] );
//i = inet_aton(hostname,&server_addr.sin_addr);
server_addr.sin_port = htons(portno);
//printf("ipaddr (in hex): %x\n",server_addr.sin_addr);
/*
* build a message. Our message is all zeros except for a one in the
* protocol version field
* msg[] in binary is 00 001 000 00000000
* it should be a total of 48 bytes long
*/
// send the data
//printf("sending data..\n");
i = sendto(s, msg, sizeof(msg), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
perror("sendto");
// get the data back
struct sockaddr saddr;
socklen_t saddr_l = sizeof(saddr);
i = recvfrom(s, buf, 48, 0, &saddr, &saddr_l);
perror("recvfr:");
//We get 12 long words back in Network order
/*
for(i=0;i<12;i++) {
//printf("%d\t%-8x\n",i,ntohl(buf[i]));
long tmit2=ntohl((time_t)buf[i]);
std::cout << "Round number " << i << " time is " << ctime(&tmit2) << std::endl;
}
*/
/*
* The high word of transmit time is the 10th word we get back
* tmit is the time in seconds not accounting for network delays which
* should be way less than a second if this is a local NTP server
*/
//tmit=ntohl((time_t)buf[10]); //# get transmit time
tmit = ntohl((time_t)buf[4]); //# get transmit time是将一个无符号长整形数从网络字节顺序转换为主机字节顺序
//printf("tmit=%d\n",tmit);
/*
* Convert time to unix standard time NTP is number of seconds since 0000
* UT on 1 January 1900 unix time is seconds since 0000 UT on 1 January
* 1970 There has been a trend to add a 2 leap seconds every 3 years.
* Leap seconds are only an issue the last second of the month in June and
* December if you don't try to set the clock then it can be ignored but
* this is importaint to people who coordinate times with GPS clock sources.
*/
tmit -= 2208988800U;
//printf("tmit=%d\n",tmit);
/* use unix library function to show me the local time (it takes care
* of timezone issues for both north and south of the equator and places
* that do Summer time/ Daylight savings time.
*/
//#compare to system time
//printf("Time: %s",ctime(&tmit));
// 一月JAN
// 二月FEB
// 三月MAR
// 四月APR
// 五月MAY
// 六月JUN
// 七月JUL
// 八月AUG
// 九月SEP
// 十月OCT
// 十一月NOV
// 十二月DEC
//Thu Mar 19 05:43:42 2020
//std::cout << "time is " << ctime(&tmit) << std::endl;
char *gTime = ctime(&tmit);
char time_str[32] = {0};
char year[5] = {0};
char secons[3] = {0};
char day[3] = {0};
char s1[3] = {0};
char f[3] = {0};
char m[3] = {0};
int tmp = 0;
//年
year[0] = gTime[20];
year[1] = gTime[21];
year[2] = gTime[22];
year[3] = gTime[23];
//月
secons[0] = gTime[4];
secons[1] = gTime[5];
secons[2] = gTime[6];
get_yue(secons);
printf("get_yue:%s\n", secons);
//日
day[0] = gTime[8];
day[1] = gTime[9];
//时
s1[0] = gTime[11];
s1[1] = gTime[12];
tmp = atoi(s1);
tmp += 8; //调整时区到东八区。
if (tmp >= 24)
tmp %= 24;
sprintf(s1, "%d", tmp);
//分
f[0] = gTime[14];
f[1] = gTime[15];
//秒
m[0] = gTime[17];
m[1] = gTime[18];
//date -s "2020-03-24 17:03:50"
sprintf(time_str, "date -s \"%s-%s-%s %s:%s:%s\"", year, secons, day, s1, f, m);
printf("%s -----%s\n", time_str, gTime);
system(time_str);
}
int get_yue(char *secons)
{
printf("secons %s\n", secons);
int seconss = -1;
if (strncmp(secons, "Jan", 3) == 0)
{
secons[0] = '0';
secons[1] = '1';
}
else if (strncmp(secons, "Feb", 3) == 0)
{
secons[0] = '0';
secons[1] = '2';
}
else if (strncmp(secons, "Mar", 3) == 0)
{
secons[0] = '0';
secons[1] = '3';
}
else if (strncmp(secons, "Apr", 3) == 0)
{
secons[0] = '0';
secons[1] = '4';
}
else if (strncmp(secons, "May", 3) == 0)
{
secons[0] = '0';
secons[1] = '5';
}
else if (strncmp(secons, "Jun", 3) == 0)
{
secons[0] = '0';
secons[1] = '6';
}
else if (strncmp(secons, "Jul", 3) == 0)
{
secons[0] = '0';
secons[1] = '7';
}
else if (strncmp(secons, "Aug", 3) == 0)
{
secons[0] = '0';
secons[1] = '8';
}
else if (strncmp(secons, "Sep", 3) == 0)
{
secons[0] = '0';
secons[1] = '9';
}
else if (strncmp(secons, "Oct", 3) == 0)
{
secons[0] = '1';
secons[1] = '0';
}
else if (strncmp(secons, "Nov", 3) == 0)
{
secons[0] = '1';
secons[1] = '1';
}
else if (strncmp(secons, "Dec", 3) == 0)
{
secons[0] = '1';
secons[1] = '2';
}
else
{
printf("Error!\n");
}
seconss = 1;
secons[2] = 0;
return seconss;
}
void get_alarm_time()
{
time_t time_seconds = time(0);
struct tm now_time;
localtime_r(&time_seconds, &now_time);
string alarmTimeAsName = getFormatTime(now_time.tm_year + 1900, now_time.tm_mon + 1, now_time.tm_mday, now_time.tm_hour, now_time.tm_min, now_time.tm_sec, 0);
string alarmTimeAsDate = getFormatTime(now_time.tm_year + 1900, now_time.tm_mon + 1, now_time.tm_mday, now_time.tm_hour, now_time.tm_min, now_time.tm_sec, 1);
}
std::string getFormatTime(int year, int mon, int day, int hour, int min, int sec, int type)
{
std::string yearStr = std::to_string(year);
std::string monStr, dayStr, hourStr, minStr, secStr;
if (mon >= 10)
monStr = std::to_string(mon);
else
monStr = "0" + std::to_string(mon);
if (day >= 10)
dayStr = std::to_string(day);
else
dayStr = "0" + std::to_string(day);
if (hour >= 10)
hourStr = std::to_string(hour);
else
hourStr = "0" + std::to_string(hour);
if (min >= 10)
minStr = std::to_string(min);
else
minStr = "0" + std::to_string(min);
if (sec >= 10)
secStr = std::to_string(sec);
else
secStr = "0" + std::to_string(sec);
if (!type)
return (yearStr + monStr + dayStr + hourStr + minStr + secStr);
if (type == 1)
return (yearStr + "-" + monStr + "-" + dayStr + "%20" + hourStr + ":" + minStr + ":" + secStr);
}