Get real date from the Internet

Re: Get real date from the Internet

Postby Antonio Linares » Thu Sep 29, 2022 7:43 pm

This version works fine with Borland and MSVC 2022. many thanks to Lailton help!!!
Code: Select all  Expand view  RUN
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

Postby Antonio Linares » Thu Sep 29, 2022 7:55 pm

This version removes all the warnings with MSVC 2022 and also works with Borland !!!
Code: Select all  Expand view  RUN
#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
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

Postby Horizon » Thu Sep 29, 2022 8:15 pm

Thank you very much Antonio.
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: Get real date from the Internet

Postby Lailton » Fri Sep 30, 2022 1:07 am

Excellent Antonio, Good job!
Regards,
Lailton Fernando Mariano
User avatar
Lailton
 
Posts: 156
Joined: Fri Jul 20, 2012 1:49 am
Location: Brazil

Re: Get real date from the Internet

Postby Enrico Maria Giordano » Fri Sep 30, 2022 10:17 am

Please add this to get it working with BCC:

Code: Select all  Expand view  RUN
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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

#include <ws2tcpip.h>
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Get real date from the Internet

Postby Antonio Linares » 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
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

Postby Enrico Maria Giordano » Fri Sep 30, 2022 10:39 am

Works fine here with BCC and MSC.
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Get real date from the Internet

Postby Enrico Maria Giordano » Fri Sep 30, 2022 10:39 am

Have you add all this?

Code: Select all  Expand view  RUN
#ifdef __BORLANDC__
   #include <winsock2.h>
#endif
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Get real date from the Internet

Postby Antonio Linares » Fri Sep 30, 2022 10:40 am

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

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

Postby Antonio Linares » 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
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

Postby Enrico Maria Giordano » Fri Sep 30, 2022 10:41 am

No. If you add this it won't make any difference with MSC.

Code: Select all  Expand view  RUN
#ifdef __BORLANDC__
   #include <winsock2.h>
#endif
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Get real date from the Internet

Postby Enrico Maria Giordano » Fri Sep 30, 2022 10:43 am

Please double check. There is something wrong in your code.
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Get real date from the Internet

Postby Antonio Linares » Fri Sep 30, 2022 10:51 am

please provide us a correct buildh32.bat :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Get real date from the Internet

Postby Enrico Maria Giordano » 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.
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Get real date from the Internet

Postby Antonio Linares » Fri Sep 30, 2022 2:26 pm

Dear Enrico,

Code: Select all  Expand view  RUN
┌────────────────────────────────────────────────────────────────────────────┐
│ 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: Select all  Expand view  RUN
@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
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

PreviousNext

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 76 guests

cron