hwclock-w将当前时间和日期写入BIOS,避免重启后失效wclock--show或者clock--show查看硬件时间hwclock--set--date="07/07/0610:19"或者clock--set--date="2020。11。2310:10"设置硬件时间。
ntp时间服务器,同步时间。需要在服务器上安装好即可。
// 思路是发送一个http请求,然后从response字符串里截取出Date部分,这样得到的时间是GMT格式的,不过转本地时间也很容易了#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsaData
addrinfo hints, *rs
sockaddr_in serveraddr
SOCKET s
char host[256], hostIP[256], request[1024], *p
int rc, nBytes
WSAStartup(MAKEWORD(2, 2), &wsaData)
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
printf("plz input host name(eg.www.example.com):")
gets(host)
ZeroMemory(&hints, sizeof(addrinfo))
hints.ai_flags = AI_PASSIVE
hints.ai_family = AF_UNSPEC
hints.ai_socktype = SOCK_STREAM
hints.ai_protocol = IPPROTO_TCP
rc = getaddrinfo(host, "80", &hints, &rs)
if(rc != 0) {
printf("getaddrinfo failed with error code:%d\n", WSAGetLastError())
goto clean
}
getnameinfo(rs->ai_addr, rs->ai_addrlen, hostIP, NI_MAXHOST, NULL, 0, NI_NUMERICHOST)
printf("server ip is: %s\n", hostIP)
freeaddrinfo(rs)
serveraddr.sin_family = AF_INET
serveraddr.sin_addr.s_addr = inet_addr(hostIP)
serveraddr.sin_port = htons(80)
rc = connect(s, (SOCKADDR*) &serveraddr, sizeof(serveraddr))
if(rc != 0) {
printf("connect to server failed.")
goto clean
}
strcpy(request, "GET / HTTP/1.1\r\n")
strcat(request, "Host: ")
strcat(request, host)
strcat(request, "\r\nConnection: close\r\n\r\n")
send(s, request, strlen(request), 0)
while(1)
{
nBytes = recv(s, request, 1024, 0)
if(nBytes == SOCKET_ERROR)
{
printf("recv failed with error %d\n",WSAGetLastError())
goto clean
} else {
p = strstr(request, "Date:")
p += 5
while(*p == ' ') ++p
printf("server time: ")
while(*p != '\r')
putchar(*p++)
putchar('\n')
break
}
}
clean:
closesocket(s)
WSACleanup()
return 0
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)