#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
}
获得当前日期+时间(date + time)函数:now()除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数:
current_timestamp() current_timestamp
localtime() localtime
localtimestamp() localtimestamp
这些日期时间函数,都等同于 now()。鉴于 now() 函数简短易记,建议总是使用 now() 来替代上面列出的函数。
1.2 获得当前日期+时间(date + time)函数:sysdate()
sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。
获得当前日期(date)函数:curdate()
其中,下面的两个日期函数等同于 curdate(): current_date(),current_date
获得当前时间(time)函数:curtime()
其中,下面的两个时间函数等同于 curtime():current_time(),current_time
获得当前 UTC 日期时间函数:utc_date(), utc_time(), utc_timestamp()
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)