Olá!
Você usa SOCKET igual a toda linguagem.
Quem será o servidor(daemon) na porta TCP ? Em JAVA ou Harbour ?
Vc pode usar HBTIP ou HBCurl. para puxar os dados.
Ou montar seu DAEMON (Serviço) escutando pelo TCP
Na pasta test do harbour tem esse: iotcp.prg o server.prg e o client.prg para estudos.
iotcp.prg
Código: Selecionar todos
/*
* Harbour FILE IO redirector: IOTCP
* example of IOUSR usage
*
* Copyright 2014 Przemyslaw Czerpak <druzus / at / priv.onet.pl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file LICENSE.txt. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA (or visit https://www.gnu.org/licenses/).
*
* As a special exception, the Harbour Project gives permission for
* additional uses of the text contained in its release of Harbour.
*
* The exception is that, if you link the Harbour libraries with other
* files to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the Harbour library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the Harbour
* Project under the name Harbour. If you copy code from other
* Harbour Project or Free Software Foundation releases into a copy of
* Harbour, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for Harbour, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice.
*
*/
#include "hbiousr.ch"
#include "hbsocket.ch"
#include "fileio.ch"
#include "error.ch"
ANNOUNCE HB_IOTCP
#define IOTCP_SOCKET 1
#define IOTCP_EOF 2
#define IOTCP_TIMEOUT 3
#define IOTCP_ERRORBASE 10000
#define IOTCP_NEW( sd, tout ) { sd, .F., tout }
/*
* NOTE: cDefExt, cPaths and oError can be NIL
*/
STATIC FUNCTION IOTCP_Open( cFile, cDefExt, nFlags, cPaths, oError )
LOCAL nTimeout := 10000, nError, hSock := NIL, aFile := NIL
LOCAL cAddr, cRest, nPort := 0, aAddr
LOCAL nAt
HB_SYMBOL_UNUSED( cDefExt )
HB_SYMBOL_UNUSED( cPaths )
/* strip "tcp:" prefix */
cAddr := substr( cFile, 5 )
/* take host, port and timeout from "<host>:<port>[:<timeout>]" string */
IF ( nAt := At( ":", cAddr ) ) > 1
cRest := SubStr( cAddr, nAt + 1 )
cAddr := Left( cAddr, nAt - 1 )
IF ( nPort := Val( cRest ) ) > 0
WHILE IsDigit( cRest )
cRest := SubStr( cRest, 2 )
ENDDO
IF cRest = ":"
cRest := SubStr( cRest, 2 )
IF IsDigit( cRest ) .AND. ( nTimeout := Val( cRest ) ) > 0
WHILE IsDigit( cRest )
cRest := SubStr( cRest, 2 )
ENDDO
IF cRest = ":"
cRest := ""
ENDIF
ENDIF
ENDIF
IF ! cRest == ""
nPort := 0
ENDIF
ENDIF
ENDIF
IF nPort != 0
IF !Empty( aAddr := hb_socketResolveInetAddr( cAddr, nPort ) ) .AND. ;
!Empty( hSock := hb_socketOpen() )
hb_socketSetKeepAlive( hSock, .T. )
IF hb_socketConnect( hSock, aAddr, nTimeout )
IF !Empty( hSock )
SWITCH hb_bitAnd( nFlags, hb_bitOr( FO_READ, FO_WRITE, FO_READWRITE ) )
CASE FO_READ
hb_socketShutdown( hSock, HB_SOCKET_SHUT_WR )
EXIT
CASE FO_WRITE
hb_socketShutdown( hSock, HB_SOCKET_SHUT_RD )
EXIT
ENDSWITCH
aFile := IOTCP_NEW( hSock, nTimeout )
ENDIF
ENDIF
IF aFile == NIL
nError := hb_socketGetError()
hb_socketClose( hSock )
ENDIF
ENDIF
IF nError == 0 .AND. aFile == NIL
nError := hb_socketGetError()
ENDIF
ELSE
nError := HB_SOCKET_ERR_WRONGADDR
ENDIF
IOUSR_SetError( nError, IOTCP_ERRORBASE )
IF oError != NIL
oError:filename := cFile
IF aFile == NIL
oError:osCode := nError
oError:genCode := EG_OPEN
ENDIF
ENDIF
RETURN aFile /* if aFile == NIL indicates error */
STATIC FUNCTION IOTCP_Close( aFile )
hb_socketClose( aFile[ IOTCP_SOCKET ] )
IOUSR_SetError( hb_socketGetError(), IOTCP_ERRORBASE )
RETURN NIL
STATIC FUNCTION IOTCP_Read( aFile, /*@*/ cData, nLen, nTimeout )
LOCAL nRead := 0, nError
IF !aFile[ IOTCP_EOF ]
IF nTimeout == -1
nTimeout := aFile[ IOTCP_TIMEOUT ]
ENDIF
nRead := hb_socketRecv( aFile[ IOTCP_SOCKET ], @cData, nLen, 0, nTimeout )
nError := hb_socketGetError()
IF nRead <= 0
SWITCH nError
CASE HB_SOCKET_ERR_TIMEOUT
CASE HB_SOCKET_ERR_AGAIN
CASE HB_SOCKET_ERR_TRYAGAIN
EXIT
OTHERWISE
aFile[ IOTCP_EOF ] := .F.
ENDSWITCH
nRead := 0
ENDIF
IOUSR_SetError( nError, IOTCP_ERRORBASE )
ENDIF
RETURN nRead
STATIC FUNCTION IOTCP_Write( aFile, cData, nLen, nTimeout )
IF nTimeout == -1
nTimeout := aFile[ IOTCP_TIMEOUT ]
ENDIF
nLen := hb_socketSend( aFile[ IOTCP_SOCKET ], cData, nLen, 0, nTimeout )
IOUSR_SetError( hb_socketGetError(), IOTCP_ERRORBASE )
RETURN iif( nLen < 0, 0, nLen )
STATIC FUNCTION IOTCP_Eof( aFile )
RETURN aFile[ IOTCP_EOF ]
STATIC FUNCTION IOTCP_Configure( aFile, nIndex, xValue )
HB_SYMBOL_UNUSED( aFile )
HB_SYMBOL_UNUSED( nIndex )
HB_SYMBOL_UNUSED( xValue )
RETURN .F.
STATIC FUNCTION IOTCP_Handle( aFile )
IOUSR_SetError( 0, IOTCP_ERRORBASE )
RETURN hb_socketGetFD( aFile[ IOTCP_SOCKET ] )
INIT PROCEDURE CLIPINIT
LOCAL aMethods[ IOUSR_METHODCOUNT ]
aMethods[ IOUSR_OPEN ] := @IOTCP_Open()
aMethods[ IOUSR_CLOSE ] := @IOTCP_Close()
aMethods[ IOUSR_READ ] := @IOTCP_Read()
aMethods[ IOUSR_WRITE ] := @IOTCP_Write()
aMethods[ IOUSR_EOF ] := @IOTCP_Eof()
aMethods[ IOUSR_CONFIGURE ] := @IOTCP_Configure()
aMethods[ IOUSR_HANDLE ] := @IOTCP_Handle()
IOUSR_Register( aMethods, "tcp:" )
RETURN
/* test code */
REQUEST HB_IOTCP
PROCEDURE Main( cAddr )
LOCAL hFile, cData, cSend, cEOL, nLen
IF Empty( cAddr )
cAddr := "tcp:smtp.gmail.com:25:10000"
ENDIF
? "open:", cAddr
hFile := hb_vfOpen( cAddr, FO_READWRITE )
IF Empty( hFile )
? "Open error:", FError()
ELSE
cData := Space( 1024 )
cEOL := e"\r\n"
IF ( nLen := hb_vfRead( hFile, @cData,, 10000 ) ) > 0
? "<< " + StrTran( HB_BLeft( cData, nLen ), cEOL, cEOL + "<< " )
ENDIF
cSend := "EHLO" + cEOL
nLen := hb_vfWrite( hFile, cSend,, 1000 )
? ">> " + StrTran( HB_BLeft( cSend, nLen ), cEOL, cEOL + ">> " )
IF nLen != hb_BLen( cSend )
? "WRITE ERROR:", FError()
ENDIF
IF ( nLen := hb_vfRead( hFile, @cData,, 10000 ) ) > 0
? "<< " + StrTran( HB_BLeft( cData, nLen ), cEOL, cEOL + "<< " )
ENDIF
cSend := "QUIT" + cEOL
nLen := hb_vfWrite( hFile, cSend,, 1000 )
? ">> " + StrTran( HB_BLeft( cSend, nLen ), cEOL, cEOL + ">> " )
IF nLen != hb_BLen( cSend )
? "WRITE ERROR:", FError()
ENDIF
IF ( nLen := hb_vfRead( hFile, @cData,, 10000 ) ) > 0
? "<< " + StrTran( HB_BLeft( cData, nLen ), cEOL, cEOL + "<< " )
ENDIF
hb_vfClose( hFile )
? "closed, error:", FError()
ENDIF
?
WAIT
RETURN
server.prg
Código: Selecionar todos
/**
* Harbour Inet demo server program
*
* Giancarlo Niccolai
*
* In this program, the server uses just one thread
* to demonstrate how to use timeout sockets.
*/
PROCEDURE Main( cPort )
LOCAL Socket, s
LOCAL nResponse, cResponse
LOCAL nTurn := 0, nTurn1 := 0
LOCAL bCont := .T.
CLS
IF Empty( cPort )
cPort := "2000"
ENDIF
hb_inetInit()
@ 1, 15 SAY "H A R B O U R - Inet Api Server Demo"
@ 2, 5 SAY "Contact this server using telnet or the Harbour Inet Client demo"
@ 3, 5 SAY "Press a [KEY] to terminate the program"
@ 5, 5 SAY "Server listening on port " + cPort + "..."
Socket := hb_inetServer( Val( cPort ) )
hb_inetTimeout( Socket, 500 )
DO WHILE bCont
@ 6, 5 SAY Space( 70 )
@ 7, 5 SAY Space( 70 )
@ 8, 5 SAY Space( 70 )
@ 9, 5 SAY Space( 70 )
@ 6, 5
// Accepting a connection
DO WHILE bCont
Progress( @nTurn, 5, 39 )
s := hb_inetAccept( Socket )
IF hb_inetErrorCode( Socket ) == 0
EXIT
ENDIF
IF Inkey() != 0
bCont := .F.
ENDIF
ENDDO
IF ! bCont
EXIT
ENDIF
hb_inetTimeout( s, 500 )
@ 6, 5 SAY "Connection from: " + hb_inetAddress( s ) + ":" + Str( hb_inetPort( s ), 5 )
@ 7, 5 SAY "Receiving: "
@ 8, 5
nResponse := hb_inetSend( s, "Welcome to my server!" + hb_eol() )
DO WHILE bCont
// This timeout ...
hb_inetTimeout( s, 250 )
// ... will trigger this periodic callback,
hb_inetPeriodCallback( s, { @Progress(), @nTurn, 6, 39 } )
// that will be called each TIMEOUT Milliseconds.
cResponse := hb_inetRecvLine( s, @nResponse )
// hb_inetRecvLine won't return until the periodic callback returns .F.,
// or the Timelimit has been reached. Timelimit is currently -1, so
// hb_inetRecvLine will wait forever.
DO CASE
CASE hb_inetErrorCode( s ) == 0
IF Lower( cResponse ) == "quit"
bCont := .F.
ENDIF
@ 8, 5 SAY Space( 70 )
@ 8, 5 SAY cResponse
cResponse := "Count: " + Str( nResponse ) + " characters" + hb_eol()
hb_inetSend( s, cResponse )
CASE hb_inetErrorCode( s ) == -1
// idle (timed out)
Progress( @nTurn1, 7, 17 )
OTHERWISE
@ 7, 5 SAY "Received Error " + Str( hb_inetErrorCode( s ) ) + ": " + hb_inetErrorDesc( s )
@ 8, 5 SAY Space( 70 )
@ 9, 5 SAY Space( 70 )
@ 9, 5 SAY "Press a key to continue"
Inkey( 0 )
EXIT
ENDCASE
IF Inkey() != 0
bCont := .F.
ENDIF
ENDDO
ENDDO
hb_inetCleanup()
RETURN
PROCEDURE Progress( nProgress, nDrow, nDcol )
LOCAL nRow := Row(), nCol := Col()
@ nDrow, nDcol SAY "[ ]"
DO CASE
CASE nProgress == 0
@ nDrow, nDcol + 1 SAY "-"
CASE nProgress == 1
@ nDrow, nDcol + 1 SAY "\"
CASE nProgress == 2
@ nDrow, nDcol + 1 SAY "|"
CASE nProgress == 3
@ nDrow, nDcol + 1 SAY "/"
ENDCASE
nProgress++
IF nProgress == 4
nProgress := 0
ENDIF
@ nRow, nCol
RETURN
Saudações,
Itamar M. Lins Jr.