Page 1 of 1

Escuchar comunicaciones por puerto IP.

Posted: Thu Apr 10, 2014 10:22 am
by fernandomoralesdr
Hola a todos.

Tengo una pesadora que se comunica con un programa a través de una IP.
Es un programa de terceros y quiero sustituirlo por uno de desarrollo propio.
Sé como establecer comunicación a través del puerto serie pero en este caso me interesaría saber cómo puedo capturar la información que envía. Si fuera posible mediante programación, y si no, con algún software que dicho tráfico me lo enviara o un fichero de texto que periódicamente mi programa leería.
Gracias por adelantado.

Re: Escuchar comunicaciones por puerto IP.

Posted: Thu Apr 10, 2014 2:04 pm
by Adolfo
Fernando..
Puedes enviar la marca y modelo de la pesa.

Yo tengo algunas con un cliente, pero la comunicacion IP es solo para programarlas y enviarles detalle de precios, codigos y productos, y la puerta serial para enviar en vivo el pesaje.

A ver si te puedo ayudar.

Desde Chile
Adolfo

Re: Escuchar comunicaciones por puerto IP.

Posted: Mon Apr 14, 2014 9:36 pm
by JmGarcia
Mira a ver si te vale este código:

Code: Select all

public cIPhost:="192.168.1.170  ",nPuerto:=10001,pSocket:=0,cTrama:="",cResponse:="",;
       nBytes:=0,cBuffer:=""
INetInit()
pSocket:=INetConnectIP(cIPhost,nPuerto)
if INetErrorCode(pSocket)<>0
   MsgAlert("Socket error:"+;
            INetErrorDesc(pSocket)+;
            " ("+alltrim(str(INetErrorCode(pSocket)))+")","¡ AVISO !")
   INetCleanUp()
   quit
endif
INetSetTimeout(pSocket,5000)
cTrama:=""
cResponse:=""
nSegundos:=seconds()
do while .T. // De este bucle hay que controlar cuando se quiere salir y como.
   nBytes :=1500
   cBuffer:=space(nBytes)
   nBytes :=INetDGramRecv(pSocket,@cBuffer,nBytes)
   cBuffer:=left(cBuffer,nBytes)
   cTrama:=cTrama+cBuffer // En la variable "cTrama" se acumula los contenidos de las tramas recibidas...
   .../...
enddo
INetClose(pSocket)
INetCleanUp()
Tienes que linkar la librería \xHarbour\lib\tip.lib

Y las funciones usadas son:
INetInit() : Initializes the sockets subsystem.
Syntax
INetInit() --> NIL
Arguments
no arguments available
Return value
The return value is always NIL.
Description
Function inetInit() must be called to initialize the sockets subsystem and allocate all required memory resources. It is recommended to place one call to inetInit() at the begin of a program that uses sockets. After the sockets subsystem is initialized, function inetConnect() or inetAccept() can be called to establish a sockets connection.
Note:
call function inetCleanup() to release memory resources for the sockets subsystem, when sockets are no longer needed.
INetConnectIP() : Establishes a sockets connection to a server using the IP address.
Syntax
INetConnectIP( <cIPAddress>, <nPort> ) --> pSocket
or
INetConnectIP( <cIPAddress>, <nPort>, <pSocket> ) --> NIL
Arguments
<cIPAddress>
This is a character string holding IP address of the server in dotted notation (e.g. "192.168.1.145").
<nPort>
This is the numeric port number on the local computer to use for the socket connection. The default internet port is 80.
<pSocket>
Optionally, a socket created with inetCreate() or previously closed with inetClose() can be passed to establish a connection.
Return value
If called with two parameters, the return value is a pointer to a socket. If a socket is passed as third parameter, the return value is NIL.
Description
inetConnectIP() does the same as inetConnect() except for not resolving the IP address from the server's URL, or DNS name. inetConnectIP() accepts only the IP address of the server in dottet notation. Therefore, the function is thread safe and can be called simultaneously in multiple threads.
If the connection is successfully established, the socket can be used for sending and receiving data to/from the server. If the connection fails, the cause of failure can be detected with inetErrorCode() .
INetErrorCode() : Returns the last sockets error code.
Syntax
INetErrorCode( <pSocket> ) --> nErrorCode
Arguments
<pSocket>
This is a pointer to a socket.
Return value
The function returnes the error code of the last sockets operation as a numeric value.
Description
When a sockets function fails, the error code of the sockets operation is stored in <pSocket> and can be retrieved later with function inetErrorCode(). This error code remains with <pSocket> until a new sockets function is called with this socket or inetClearError() is executed.
To obtain a human readable description of a sockets error, call inetErrorDesc() with <pSocket>.
INetErrorDesc() : Returns a descriptive error message
Syntax
INetErrorDesc( <pSocket> ) --> cErrorMessage
Arguments
<pSocket>
This is a pointer to a socket.
Return value
The function retunrs a character string holding an error message. If no error occurred, the return value is a null string ("").
Description
Function inetErrorDesc() is used to obtain the description of an error when a socket function fails. It translates the numeric error code into a human readable message.
INetCleanup() : Releases memory resources for sockets.
Syntax
INetCleanup() --> NIL
Arguments
no arguments available
Return value
The return value is always NIL.
Description
inetCleanup() should be called at the end of any program using sockets functions, or when sockets are no longer required. The function releases all memory resources allocated with inetInit() .
INetSetTimeout() : Sets a timout value in milliseconds for a socket.
Syntax
INetSetTimeout( <pSocket>, <nMilliSeconds> ) --> NIL
Arguments
<pSocket>
This is a pointer to a socket created from any socket creation function.
<nMilliSeconds>
A numeric value specifies the timeout period in milliseconds.
Return value
The return value is always NIL.
Description
inetSetTimeout() sets the timeout value for a socket, after which a blocking sockets function returns. If the blocking sockets function is not completed withing the timeout period, it returns, and a sockets error of -1 is set.
Refer to function inetSetPeriodCallback() for an example of taking advantage of a timeout value.
Note:
high-level sockets functions, like inetRecvAll() , internally call their low-level counterpart inetRecv() multiple times until all data is retrieved. The timeout value is applied to all individual inetRecv() calls, not for the entire inetRecvAll() call.
INetDGramRecv() : Reads data from a datagram socket.
Syntax
INetDGramRecv( <pSocket>, ;
@<cBuffer>, ;
[<nBytes>] ) --> nReceivedBytes
Arguments
<pSocket>
This is a pointer to a socket as returned by inetDGram() or inetDGramBind()
<cBuffer>
A memory variable holding a character string must be passed by reference to inetDGramRecv(). It must have at least <nBytes> characters.
<nBytes>
This is a numeric value specifying the number of bytes to transfer from the socket into the memory variable <cBuffer>. It defaults to Len(<cBuffer>).
Return value
The function returns a numeric value indicating the number of bytes read from the socket. If the return value is smaller than Len(<cBuffer>), either no more bytes are available, or a network error ocurred. This can be identified using function !LINK INetErrorCode() !ELINK.
Description
inetDGramRecv() blocks the current thread until <nBytes> bytes are read from a datagram socket <pSocket>. The received bytes are copied into the memory variable <cBuffer>, which must be passed by reference.
To avoid blocking, function inetDataReady() can be used to detect if incoming data is available, or a timeout value can be set with inetSetTimeout() .
Note:
it is not guaranteed that all the data required to be read is sent from the kernel to the application. The kernel just returns the last complete datagram that has been received, up to <nBytes> bytes.
Refer to function inetDGramSend() for an example of datagram exchange between two processes.
INetClose() : Closes a connection on a socket.
Syntax
INetClose( <pSocket> ) --> nError
Arguments
<pSocket>
This is a pointer to a socket.
Return value
The function returns zero on success, or -1 on failure.
Description
inetClose() closes a socket and notifies both ends of the connection. If threads are executing a blocking sockets function with <pSocket>, they will terminate their wait state and a socket error is set ("socket closed"). This causes all threads waiting on a blocking sockets function to resume so they can report a sockets error.
If the return value is not zero, use function inetErrorCode() to obtain information about failure.