Code: Select all | Expand
#include "FiveWin.ch"
function Main()
MsgInfo( Now() )
return nil
#pragma BEGINDUMP
#ifdef _CRT_SECURE_NO_WARNINGS
#undef _CRT_SECURE_NO_WARNINGS
#endif
#define _CRT_SECURE_NO_WARNINGS 1
#include <hbapi.h>
#ifndef __BORLANDC__
#define _WIN32_WINNT 0x0502
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ws2tcpip.h>
HB_FUNC( NOW )
{
struct addrinfo hints;
struct addrinfo *result;
int sockfd;
int rv;
char buf[64];
time_t t;
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; /* Allow IPv4 */
hints.ai_socktype = SOCK_STREAM; /* Stream socket */
hints.ai_flags = AI_CANONNAME; /* Return canonical name */
rv = getaddrinfo("www.google.com", "http", &hints, &result);
if (rv != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
exit(1);
}
/* Create socket */
sockfd = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (sockfd == -1) {
perror("socket");
exit(1);
}
/* Connect */
if (connect(sockfd, result->ai_addr, result->ai_addrlen) == -1) {
perror("connect");
exit(1);
}
/* Get time */
t = time(NULL);
snprintf(buf, sizeof(buf), "GET / HTTP/1.0\r\n\r\n");
send(sockfd, buf, strlen(buf), 0);
recv(sockfd, buf, sizeof(buf), 0);
snprintf(buf, sizeof( buf ), "Current time: %s", ctime(&t));
closesocket(sockfd);
freeaddrinfo(result);
hb_retc( buf );
}