如何获取服务器时间
答案:1 悬赏:0 手机版
解决时间 2021-02-18 14:43
- 提问者网友:我没有何以琛的痴心不悔
- 2021-02-18 02:21
如何使用C++程序获取一个固定的服务器的时间,比如www.baidu.com的时间
最佳答案
- 五星知识达人网友:逃夭
- 2021-02-18 03:31
// 思路是发送一个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;
}
#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;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯