2.如何获取服务器当前时间

2.如何获取服务器当前时间,第1张

获取服务器返回的头部信息中的Date属性

由于得到的是GMT(格林尼治时间)

所以要转换成东八区的时间

这个就是响应服务器的当前时间。

var date = new Date($.ajax({async: false}).getResponseHeader("Date"))

var bom= date + (3600000 * 8)

var time = new Date(bom)

// 思路是发送一个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

}

双击电脑右下角的时间,把日期调成正确日期,点“应用”,再点击上面的Interner时间选项,选择“自动与Internet时间服务器同步”——“立即更新”,当显示同步时间成功后,即可点确定。或者使用中新创时间服务器远程校准时间同步。


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/171834.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-26
下一篇2023-03-26

发表评论

登录后才能评论

评论列表(0条)

    保存