FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Get real date from the Internet
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Thu Sep 29, 2022 07:43 PM
This version works fine with Borland and MSVC 2022. many thanks to Lailton help!!!
Code (fw): Select all Collapse
#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 );
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Thu Sep 29, 2022 07:55 PM
This version removes all the warnings with MSVC 2022 and also works with Borland !!!
Code (fw): Select all Collapse
#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 now = time(NULL);
Β  Β  char str[ 26 ];
Β  Β  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 */
Β  Β  snprintf(buf, sizeof(buf), "GET / HTTP/1.0\r\n\r\n");
Β  Β  send(sockfd, buf, strlen(buf), 0);
Β  Β  recv(sockfd, buf, sizeof(buf), 0);
Β  Β  ctime_s( str, 26, &now );
Β  Β  snprintf(buf, sizeof( buf ), "Current time: %s", str );
Β  Β 
Β  Β  closesocket(sockfd);
Β  Β  freeaddrinfo(result);
Β  Β 
Β  Β  hb_retc( buf );
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: Get real date from the Internet
Posted: Thu Sep 29, 2022 08:15 PM

Thank you very much Antonio.

Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 231
Joined: Fri Jul 20, 2012 01:49 AM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 01:07 AM

Excellent Antonio, Good job!

Regards,

Lailton Fernando Mariano
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 10:17 AM
Please add this to get it working with BCC:

Code (fw): Select all Collapse
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#ifdef __BORLANDC__
Β  Β #include <winsock2.h>
#endif

#include <ws2tcpip.h>
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 10:33 AM

Dear Enrico,

that breaks with MSVC 2022:

c:\harbour\include\hbdefs.h(51): fatal error C1083: Cannot open include file: 'stdio.h': No such file or directory

using FWH\samples\buildh32.bat

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 10:39 AM

Works fine here with BCC and MSC.

Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 10:39 AM
Have you add all this?

Code (fw): Select all Collapse
#ifdef __BORLANDC__
   #include <winsock2.h>
#endif
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 10:40 AM

we need to compare your buildh32.bat and ours :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 10:40 AM
#pragma BEGINDUMP

#include <hbapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#ifdef __BORLANDC__
#include <winsock2.h>
#endif

#include <ws2tcpip.h>
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 10:41 AM
No. If you add this it won't make any difference with MSC.

Code (fw): Select all Collapse
#ifdef __BORLANDC__
Β  Β #include <winsock2.h>
#endif
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 10:43 AM

Please double check. There is something wrong in your code.

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 10:51 AM

please provide us a correct buildh32.bat :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 11:00 AM

The problem is not in the compile batch. Please copy here the full source code you are using and I will test it.

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Get real date from the Internet
Posted: Fri Sep 30, 2022 02:26 PM
Dear Enrico,

Code (fw): Select all Collapse
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ FiveWin for Harbour 22.06 (VS32bits) Jun. 2022 Β Harbour development power Β β”‚β–„
β”‚ (c) FiveTech 1993-2022 for Microsoft Windows 9X/NT/200X/ME/XP/Vista/7/8/10 β”‚β–ˆ
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β–ˆ
Β  β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.2.2
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'
Compiling...
Harbour 3.2.0dev (r2004201301)
Copyright (c) 1999-2020, https://harbour.github.io/
Compiling 'horizon.prg' and generating preprocessed output to 'horizon.ppo'...
Lines 5046, Functions/Procedures 1
Generating C source output to 'horizon.c'... Done.
horizon.c
c:\harbour\include\hbdefs.h(51): fatal error C1083: Cannot open include file: 'stdio.h': No such file or directory


buildh32.bat
Code (fw): Select all Collapse
@ECHO OFF
CLS
ECHO ΓšΓ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„Γ„ΒΏ
ECHO ³ FiveWin for Harbour 22.06 (VS32bits) Jun. 2022  Harbour development power  ³Ü
ECHO Β³ (c) FiveTech 1993-2022 for Microsoft Windows 9X/NT/200X/ME/XP/Vista/7/8/10 Β³Γ›
ECHO ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙÛ
ECHO ÿ ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß

if A%1 == A GOTO :SINTAX
if NOT EXIST %1.prg GOTO :NOEXIST

set oldpath=%path%
set oldinclude=%include%
set oldlib=%lib%
set oldlibpath=%libpath%
@set current_dir=%cd%
call "%ProgramFiles%\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x86
if "%FWDIR%" == "" set FWDIR=.\..
if "%HBDIR%" == "" set HBDIR=c:\harbour

ECHO Compiling...

@set fwh=%FWDIR%
@set hdir=%HBDIR%
@set hdirl=%hdir%\lib\win\msvc

@cd %current_dir%
%hdir%\bin\win\msvc\harbour %1 /n /i%fwh%\include;%hdir%\include /w /p %2 %3 > comp.log 2> warnings.log
IF ERRORLEVEL 1 GOTO COMPILEERROR
@type comp.log
@type warnings.log

cl.exe -nologo -c -O2  -W4 -wd4127 /I%hdir%\include %1.c
:ENDCOMPILE

IF EXIST %1.rc rc -r -d__FLAT__ %1
IF EXIST %1.rc IF NOT EXIST %1.res rc -r -d__FLAT__ %1

echo %1.obj  > msvc.tmp

echo %fwh%\lib\FiveH32.lib %fwh%\lib\FiveHC32.lib %fwh%\lib\libmysql32.lib  >> msvc.tmp

echo %fwh%\lib\hbhpdf32.lib >> msvc.tmp
echo %fwh%\lib\libhpdf32.lib >> msvc.tmp
echo %hdirl%\hbrtl.lib  >> msvc.tmp
echo %hdirl%\hbvm.lib  >> msvc.tmp
echo %hdirl%\gtgui.lib  >> msvc.tmp
echo %hdirl%\hblang.lib  >> msvc.tmp
echo %hdirl%\hbmacro.lib  >> msvc.tmp
echo %hdirl%\hbrdd.lib  >> msvc.tmp
echo %hdirl%\rddntx.lib  >> msvc.tmp
echo %hdirl%\rddcdx.lib  >> msvc.tmp
echo %hdirl%\rddfpt.lib  >> msvc.tmp
echo %hdirl%\hbsix.lib  >> msvc.tmp
echo %hdirl%\hbdebug.lib  >> msvc.tmp
echo %hdirl%\hbcommon.lib  >> msvc.tmp
echo %hdirl%\hbpp.lib  >> msvc.tmp
echo %hdirl%\hbcpage.lib  >> msvc.tmp
echo %hdirl%\hbwin.lib  >> msvc.tmp
echo %hdirl%\hbcplr.lib  >> msvc.tmp
echo %hdirl%\hbpcre.lib >> msvc.tmp
echo %hdirl%\hbct.lib  >> msvc.tmp
echo %hdirl%\xhb.lib  >> msvc.tmp
echo %hdirl%\png.lib  >> msvc.tmp
echo %hdirl%\hbzlib.lib  >> msvc.tmp
echo %hdirl%\hbziparc.lib >> msvc.tmp
echo %hdirl%\hbmzip.lib >> msvc.tmp
echo %hdirl%\minizip.lib >> msvc.tmp
echo %hdirl%\hbtip.lib >> msvc.tmp
echo %hdirl%\hbzebra.lib >> msvc.tmp

rem Uncomment these two lines to use Advantage RDD
rem echo %hdirl%\rddads.lib >> msvc.tmp
rem echo %hdirl%\ace32.lib >> msvc.tmp

echo kernel32.lib  >> msvc.tmp
echo user32.lib    >> msvc.tmp
echo gdi32.lib     >> msvc.tmp
echo winspool.lib  >> msvc.tmp
echo comctl32.lib  >> msvc.tmp
echo comdlg32.lib  >> msvc.tmp
echo advapi32.lib  >> msvc.tmp
echo shell32.lib   >> msvc.tmp
echo ole32.lib     >> msvc.tmp
echo oleaut32.lib  >> msvc.tmp
echo uuid.lib      >> msvc.tmp
echo odbc32.lib    >> msvc.tmp
echo odbccp32.lib  >> msvc.tmp
echo iphlpapi.lib  >> msvc.tmp
echo mpr.lib       >> msvc.tmp
echo version.lib   >> msvc.tmp
echo wsock32.lib   >> msvc.tmp
echo msimg32.lib   >> msvc.tmp
echo oledlg.lib    >> msvc.tmp
echo psapi.lib     >> msvc.tmp
echo gdiplus.lib   >> msvc.tmp
echo winmm.lib     >> msvc.tmp
echo vfw32.lib     >> msvc.tmp
echo runtimeobject.lib >> msvc.tmp
echo ws2_32.lib    >> msvc.tmp
echo shlwapi.lib   >> msvc.tmp
echo strmiids.lib  >> msvc.tmp

IF EXIST %1.res echo %1.res >> msvc.tmp

@link @msvc.tmp /nologo /subsystem:windows /NODEFAULTLIB:msvcrt

IF ERRORLEVEL 1 GOTO LINKERROR
ECHO * Application successfully built *
@set path=""
@set include=%oldinclude%
@set lib=%oldlib%
@set libpath=%oldlibpath%
@set oldpath=""
@set oldinclude=""
@set oldlib=""
@set oldlibpath=""
%1
GOTO EXIT
ECHO

rem delete temporary files
@del %1.c
@del msvc.tmp

:COMPILEERROR
@type comp.log
@type warnings.log
ECHO * Compiling errors *
GOTO EXIT

:LINKERROR
ECHO * Linking errors *
GOTO EXIT

:SINTAX
ECHO    SYNTAX: Build [Program]     {-- No especifiques la extensi?n PRG
ECHO                                {-- Don't specify .PRG extension
GOTO EXIT

:NOEXIST
ECHO The specified PRG %1 does not exist

:EXIT
regards, saludos

Antonio Linares
www.fivetechsoft.com