Página 1 de 1

Socket com dois EXE

Enviado: 12 Mai 2011 07:52
por sygecom
Olá Pessoal,
Eu posso me conectar em um EXE usando o comando abaixo e guardando em um DBF o ID da conexão e enviar comandos por outro EXE usando esse ID, alguém sabe se isso é permitido ?

pSocket := INetConnect( cIP, nPORTA )

Socket com dois EXE

Enviado: 22 Mai 2011 16:40
por rochinha
Amiguinho,

Não entendi o que voce quiz dizer.

Se voce deseja ter dois aplicativos que se comuniquem usando TCP-IP acho que deveria olhar os exemplos SOC2SERV.PRG e SOC2CLI.PRG

SOC2CLI.PRG

Código: Selecionar todos


// Socket server connection sample

#include "FiveWin.ch"

#define ST_COMMAND  1
#define ST_SENDFILE 2
#define FILE_BLOCK 8000

static oWnd, oSocket

static hLib, hDib
static xdll

//------------------------------------------------------------------------//
function Main(cHostIP)
   local oBar
   // **********************************************************************
   cPath     := cFilePath( GetModuleFileName( GetInstance() ) )
   // Pega nome da maquina
   cNetName  := NETNAME()
   // Pega IP da maquina
   WsaStartUp() ; cClientIP := getHostByName( cNetName ) ; WsaCleanUp()
   // Salva dados
   cClientIP   := VerifyINI( cNetName  , "IP"  , cClientIP , cPath+"soc2cli.ini" )
   cClientName := VerifyINI( cNetName  , "NAME", cNetName  , cPath+"soc2cli.ini" )
   //
   cSite       := VerifyINI( "SERVICE", "SITE"   , "http://localhost/5volution", cPath+"soc2cli.ini" )
   //cTXTIP      := VerifyINI( "SERVICE", "TXTIP", cSite + "/netsrv.txt" , cPath+"soc2cli.ini" )
            //
   cHostIP     := getIPExtern( cSite + "/netsrv.txt" )
   //
   cHostIP     := VerifyINI( "SERVIDOR", "IP"  , cHostIP   , cPath+"soc2cli.ini" )
   cHostName   := VerifyINI( "SERVIDOR", "NAME", "5VSERVER", cPath+"soc2cli.ini" )
   //
   // **********************************************************************
   DEFINE WINDOW oWnd TITLE "Cliente: "+alltrim(cNetName)+" IP: "+cClientName+"("+cHostIP+")"
          DEFINE BUTTONBAR oBar OF oWnd _3D
          DEFINE BUTTON OF oBar ACTION Client( cHostIP ) TOOLTIP "Conectar"
          //DEFINE BUTTON OF oBar ACTION SendCMD( "QUIT" ) TOOLTIP "Derrubar servidor"
          DEFINE BUTTON OF oBar ;
             ACTION oSocket:SendData( "MSG Mensagem de teste!" ) ;
             TOOLTIP "Send data"
          DEFINE BUTTON OF oBar ;
             ACTION SendData() TOOLTIP "Send data"
          DEFINE BUTTON OF oBar ;
             ACTION SendSearch() TOOLTIP "Send search"
   ACTIVATE WINDOW oWnd ON INIT Client( cHostIP )
   return nil

//------------------------------------------------------------------------//
function Client( cIP )
   oSocket = TSocket():New( 2000 )  
   //oSocket:bRead    = { | oSocket | OnRead( oSocket ) }
   oSocket:bConnect = { || oWnd:SetText( "Conectado a "+cHostName+"("+cIP+")" ) }
   oSocket:bClose   = { || MsgInfo( "Servidor inativado!" ) }
   //syswait(3)
   oSocket:Connect( cIP )
            //
   //oSocket:bAccept = { | oSocket | oServer := TSocket():Accept( oSocket:nSocket ),;
   //                                oServer:Cargo  := ST_COMMAND,;
   //                                oServer:bRead  := { | oSocket | OnRead( oSocket ) } }
   //oSocket:Listen()
   return nil

//------------------------------------------------------------------------//
function OnRead( oSocket )
   local cData := oSocket:GetData()
   local cToken
   do case
      case oSocket:Cargo == ST_COMMAND
           cToken = StrToken( cData, 1 )
           do case
              case cToken == "SENDCMD"
                   if "NOFOUND" $ SubStr( cData, 8 )
                      MsgWait( "Nao encontrado " + SubStr( cData, 8 ) )
                   //   oWnd:End()
                   endif
              case cToken == "MSG"
                   MsgInfo( SubStr( cData, 5 ) )
           endcase
      case oSocket:Cargo == ST_SENDFILE
           fwrite( oSocket:hFile, cData, Len( cData ) )
           LogFile( "sockserv.txt", { "writting..." } )
           if Len( cData ) < FILE_BLOCK
              // fclose( oSocket:hFile )
              // MsgInfo( Len( cData ) )
              // oSocket:Cargo = ST_COMMAND
           endif
   endcase
   return nil

//------------------------------------------------------------------------//
function getIPExtern( _site_ )
   local _IPExtern_
   ws:=TdWebService():new()
   _IPExtern_ := ws:OpenWS( _site_ )
   ws:end()
   return _IPExtern_

//------------------------------------------------------------------------//
function SendSEARCH()
   //
   cCMDString := space(40)
   MsgGet( "Pesquisar", "Nome", @cCMDString )
   oSocket:SendData( "SENDSEARCH " + cCMDString )
   //
   return nil

//------------------------------------------------------------------------//
function SendCMD( cCMDString )
   //
   oSocket:SendData( "SENDCMD " + cCMDString )
   //
   return nil

//------------------------------------------------------------------------//
function SendData()
   cREGString := "00246|JOSE CARLOS DA ROCHA|(011)3534-3099|ROCHINHA"
   //
   oSocket:SendData( "SENDDATA " + cREGString )
   //
   return nil

//------------------------------------------------------------------------//
function SendFile()
   local cFileName := cGetFile( "*.*", "Select a file to send by Internet" )
   if ! Empty( cFileName ) .and. File( cFileName )
      oSocket:SendData( "SENDFILE " + cFileName( cFileName ) )
      oSocket:SendFile( cFileName )
      oSocket:End()
      MsgInfo( "File sent" )
   endif
   return nil

//------------------------------------------------------------------------//
function VerifyINI( _section_, _entry_, _var_, _inifile_, _grava_ )
   oIni := TIni():New( _inifile_ )
   if _grava_ = .t.
      oIni:Set( _section_, _entry_, _var_ )
   endif
   return oIni:Get( _section_, _entry_, _var_, _var_ )

/*
 ******
 * Classe de acesso a webservice
 ******
 */
CLASS TdWebService
   DATA hOpen
   DATA sbuffer HIDDEN
   DATA xDLL HIDDEN
   METHOD New(buffersize) CONSTRUCTOR
   METHOD OpenWS(url)
   METHOD End()
ENDCLASS

METHOD New(conexion,buffersize) CLASS TdWebService
   DEFAULT buffersize:=64000
   ::sbuffer:=buffersize
   xDll:=LoadLib32("wininet.dll")
   ::hOpen = InternetOpen("TdWebService", 1,,, 0)
   RETURN Self

METHOD OpenWS(url) CLASS TdWebService
   local hFile,ret,xml
   hFile = InternetOpenUrl(::hOpen, url,"",0,,0)
   xml:=space(::sbuffer)
   InternetReadFile(hFile, @xml, ::sbuffer, @Ret)
   return subst(alltrim(xml),1,len(alltrim(xml)))

METHOD End() CLASS TdWebService
   FreeLib32(xDll)
   return nil

/*
 ******
 * Funcoes de socket
 ******
 */
DLL32 FUNCTION InternetOpen( cApp as LPSTR, n1 AS DWORD, n2 AS LPSTR, n3 AS LPSTR, n4 AS DWORD ) AS LONG PASCAL FROM "InternetOpenA" LIB xDll
Dll32 FUNCTION InternetReadFile(hFile As 7, @sBuffer As 8, lNumBytesToRead As 7, @lNumberOfBytesRead As 7) As 7 PASCAL Lib xDll
Dll32 FUNCTION InternetOpenUrl(hInternetSession As 7, lpszUrl As 8, lpszHeaders As 8, dwHeadersLength As 7, dwFlags As 7, dwContext As 7) As 7 FROM "InternetOpenUrlA" PASCAL Lib xDll
DLL32 FUNCTION InternetCloseHandle( hSession AS LONG ) AS BOOL PASCAL LIB xDll
DLL32 FUNCTION InternetConnect( hInternet AS LONG, cServerName AS LPSTR, nServerPort AS LONG, cUserName AS LPSTR, cPassword AS LPSTR, nService AS DWORD, nFlags AS DWORD, @nContext AS PTR ) AS LONG PASCAL FROM "InternetConnectA" LIB xDll
DLL32 FUNCTION FTPGETFILE( hConnect AS LONG, cRemoteFile AS LPSTR, cNewFile AS LPSTR, nFailIfExists AS LONG, nFlagsAndAttribs AS DWORD, nFlags AS DWORD, @nContext AS PTR ) AS BOOL PASCAL FROM "FtpGetFileA" LIB xDll
DLL32 FUNCTION FTPPUTFILE( hConnect AS LONG, cLocalFile AS LPSTR, cNewRemoteFile AS LPSTR, nFlags AS DWORD, @nContext AS PTR ) AS BOOL PASCAL FROM "FtpPutFileA" LIB xDll
DLL32 FUNCTION InternetWriteFile( hFile AS LONG, cBuffer AS LPSTR, lSize AS LONG, @nSize AS PTR ) AS BOOL PASCAL LIB xDll
DLL32 FUNCTION FtpOpenFile( hFTP AS LONG, cRemoteFile AS LPSTR, n1 AS LONG, n2 AS LONG, n3 AS LONG ) AS LONG PASCAL FROM "FtpOpenFileA" LIB xDll
DLL32 FUNCTION InternetSetFilePointer( hFile AS LONG, nDistanceToMove AS LONG, nReserved AS LPSTR, nSeekMethod AS LONG, @nContext AS PTR ) AS BOOL PASCAL LIB xDll
DLL32 FUNCTION FtpFindFirstFile( hFTP AS LONG, cMask AS LPSTR, @cWin32DataInfo AS LPSTR, n1 AS LONG, n2 AS LONG ) AS LONG PASCAL FROM "FtpFindFirstFileA" LIB xDll
DLL32 FUNCTION InternetFindNextFile( hFTPDir AS LONG, @cWin32DataInfo AS LPSTR ) AS BOOL PASCAL FROM "InternetFindNextFileA" LIB xDll
SOC2SERV.PRG

Código: Selecionar todos


// Socket server connection sample

#include "FiveWin.ch"

#define ST_COMMAND  1
#define ST_SENDFILE 2
#define FILE_BLOCK 8000

static oWnd, oSocket, oClient

//------------------------------------------------------------------------//
function Main()
   local oBar
   // **********************************************************************
   cPath     := cFilePath( GetModuleFileName( GetInstance() ) )
   // Pega nome da maquina
   cNetName  := NETNAME()
   // Pega IP da maquina
   WsaStartUp(); cHostName := getHostByName( cNetName ); WsaCleanUp()
   // Salva dados
   VerifyINI( "SOCKSERV", "NAME", cNetName , cPath+"sockserv.ini", .T. )
   VerifyINI( "SOCKSERV", "IP"  , cHostName, cPath+"sockserv.ini", .T. )
   // **********************************************************************
   if ! file( "SOCKSERV.DBF" )
      ESTRU_DBF := { { "IDCODIGO"  , "N", 5, 0 } , ;
                     { "NOME"      , "C",40, 0 } , ;
                     { "TELEFONE"  , "C",14, 4 } , ;
                     { "CONTATO"   , "C",20, 0 } }
                     DBCREATE( "SOCKSERV", ESTRU_DBF )
   endif
   // **********************************************************************
   DEFINE WINDOW oWnd TITLE "Servidor: " + cNetName
          DEFINE BUTTONBAR oBar OF oWnd _3D
          DEFINE BUTTON OF oBar ACTION Server() TOOLTIP "Listen"
   ACTIVATE WINDOW oWnd ON INIT Server()
   return nil

//------------------------------------------------------------------------//
function Server()
   oSocket = TSocket():New( 2000 )
   oSocket:bAccept = { | oSocket | oClient := TSocket():Accept( oSocket:nSocket ),;
                       oClient:Cargo  := ST_COMMAND,;
                       oClient:bRead  := { | oSocket | OnRead( oSocket ) },;
                       oClient:bClose := { | oSocket | OnClose( oSocket ) } }
   oSocket:Listen()
   return nil

//------------------------------------------------------------------------//
function OnRead( oSocket )
   local cData := oSocket:GetData()
   local cToken
   LogFile( "sockserv.txt", { Len( cData ) } )
   do case
      case oSocket:Cargo == ST_COMMAND
           cToken = StrToken( cData, 1 )
           do case
              case cToken == "SENDCMD"
                   if "QUIT" $ SubStr( cData, 8 )
                      MsgWait( "Fechando Aplicativo" )
                      oWnd:End()
                   endif
              case cToken == "MSG"
                   MsgRun( SubStr( cData, 5 ) )
              case cToken == "SENDSEARCH"
                   cNome := upper( alltrim( substr( cData, 12 ) ) )
                   USE clientes SHARED
                   locate for alltrim(clientes->nome) = alltrim(cNome)
                   if found()
                      ? "Encontrei:",clientes->nome,"Registro:",recno(),"Telefone:",clientes->telefone
                   else
                                                                               ? "nao encontrei "+cNome   
                   endif  
                                                                            BROWSE() 
                   USE
                   MsgRun( "Inserido "+cData )
              case cToken == "SENDDATA"
                   aSockServ := StringToArray( SubStr( cData, 10 ), "|" )
                   USE sockserv SHARED
                   dbAppend()
                   sockserv->IDCODIGO := val( aSockServ[1] )
                   sockserv->NOME     := aSockServ[2]
                   sockserv->TELEFONE := aSockServ[3]
                   sockserv->CONTATO  := aSockServ[4]
                   dbCommit()
                   USE
                   MsgRun( "Inserido "+cData )
           endcase
      case oSocket:Cargo == ST_SENDFILE
           fwrite( oSocket:hFile, cData, Len( cData ) )
           LogFile( "sockserv.txt", { "writting..." } )
           if Len( cData ) < FILE_BLOCK
              // fclose( oSocket:hFile )
              // MsgInfo( Len( cData ) )
              // oSocket:Cargo = ST_COMMAND
           endif
   endcase
   return nil

//------------------------------------------------------------------------//
function OnClose( oSocket )
   MsgRun( "Client has closed!" )
   do case
      case oSocket:Cargo == ST_SENDFILE
           fclose( oSocket:hFile )
   endcase
   oSocket:End()
   return nil

//------------------------------------------------------------------------//
function VerifyINI( _section_, _entry_, _var_, _inifile_, _grava_ )
   oIni := TIni():New( _inifile_ )
   if _grava_ = .t.
      oIni:Set( _section_, _entry_, _var_ )
   endif
   return oIni:Get( _section_, _entry_, _var_, _var_ )

//------------------------------------------------------------------------//
function StringToArray( cString, cSeparator ) 
   LOCAL nPos 
   LOCAL aString := {} 
   DEFAULT cSeparator := ";" 
   cString := ALLTRIM( cString ) + cSeparator 
   DO WHILE .T. 
      nPos := AT( cSeparator, cString ) 
      IF nPos = 0 
         EXIT 
      ENDIF 
      AADD( aString, SUBSTR( cString, 1, nPos-1 ) ) 
      cString := SUBSTR( cString, nPos+1 ) 
   ENDDO 
   RETURN ( aString ) 

//------------------------------------------------------------------------//
function ArrayToString( aArray, cSeparator ) 
   LOCAL nPos
   DEFAULT cSeparator := ";" 
   cString := ""
   FOR nPos = 1 TO LEN(aArray)
       cString := cString + aArray[nPos] + cSeparator
   NEXT
   RETURN ( cString ) 
Neste meus exemplos estou enviando comandos de uma maquina para outra onde a receptora interpreta o comando e salva num DBF o trecho que mandei. É o mesmo exemplo que tem nos samples da Fivewin com incrementos.

Voce configura os IPs nos INIs mas o interessante é prepara-los para capturar os IPs sem as mãos do usuário.

Entenda:

O módulo servidor deve entrar em ação e transferir via FTP ou HTTP o seu IP para o seu website.

Os clientes ao serem executados irão neste website buscar esta informação e conduzir a conexão.

Esta idéia é a mesma usada em aplicativos com TeamViewer e serve muito ao propósisto de transferencia de informações.

Deve ser também melhorada a operação de envio de arquivos pois por enquanto assim funciona somente com strings.

Socket com dois EXE

Enviado: 23 Mai 2011 11:56
por sygecom
Olá Rochinha,
Meu problema é o seguinte. Para iniciar a comunicação com o SOCKET leva 5 segundos para abrir essa conexão, e depois é só comando de ida e volta, acontece que tenho que abrir 10 conexão por vez e isso durante o dia vou ter que abrir mais de 200 vez, então isso vai consumir um tempo tremendo. Então pensei em ter um sistema no TRAY para apenas abrir e testar se as conexões estão abertas, e deixar o meu sistema normal fazer o resto de envio e recebimento de comandos.
Fiz um teste e não consegui fazer isso, porem pensei que alguém poderia já ter passado por isso e tenha alguma solução.

Socket com dois EXE

Enviado: 02 Jun 2012 20:02
por rochinha
Amiguinho,

SygeCOM

Depois de exatamente um ano, voce conseguiu criar algo relacionado ao tópico ou encontrou alguma forma de resolver o problema?

Socket com dois EXE

Enviado: 02 Jun 2012 21:00
por sygecom
Olá Rochinha,
Fiz no jeitinho brasileiro. Apenas um EXE se comunica com o Socket e esse único fica monitorando os comandos que são armazenado em um DBF e sai executando os comandos e dando retorno para os outros EXE que solicitaram o comando. Depois que fiz isso que fiquei sabendo que o aparelho que eu estava conectando só aceita uma conexão por vez, então não ia dar certo mesmo...