Passa al contenuto principale

SysGetPeerInfos, returns peer infos

Questo blocco funzione da eseguire in task Back, ritorna le informazioni della connessione. Passando in File al blocco funzione un file di tipo TCP o UDP è possibile avere in uscita le informazioni relative.

Sostituisce SysGetIpInfos

Sostituisce il blocco funzione SysGetIpInfos modificando il tipo di parametri in ingresso da stringa a puntatore a stringa. La precedente dichiarazione:

VAR
PeerInfos : SysGetIpInfos; (* Peer infos *)
END_VAR

PeerInfos(File:=Fp);
PeerIP:=PeerInfos.PeerIP;
PeerPort:=PeerInfos.PeerPort;

Diventa:

VAR
PeerInfos : SysGetPeerInfos; (* Peer infos *)
END_VAR

PeerInfos(File:=Fp);
i:=Sysmemmove(ADR(PeerIP), PeerInfos.PeerIP, SIZEOF(PeerIP));
PeerPort:=PeerInfos.PeerPort;

Descrizione​

Immagine FB SysGetPeerInfos

ParametroTipoDescrizione
FileFILEPFile pointer, deve essere di tipo TCP o UDP.
PeerIP@S_TRINGStringa di definizione indirizzo IP del peer connesso al file. PeerPort UINT) Porta del peer connesso al file

Esempi​

Come utilizzare gli esempi.
Nell'esempio è attivato un server TCP in ascolto sulla porta 4000, è accettata un'unica connessione. Quando un client si connette viene ritornato l'indirizzo IP e la porta del client che si è connesso.

LogicLab (Ptp116, ST_SysGetPeerInfos)
PROGRAM ST_SysGetPeerInfos
VAR
Pulse : BOOL; (* One shot *)
Fp : eFILEP; (* File pointer array *)
PeerPort : UINT; (* Peer port *)
PeerIP : STRING[ 16 ]; (* Peer IP *)
TCPServer : SysTCPServer; (* TCPServer management *)
PeerInfos : SysGetPeerInfos; (* Peer infos *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_SysGetPeerInfos"
// *****************************************************************************
// A TCP server is instantiated on defined port 4000. When a client connects,
// the client's information is returned.
// -----------------------------------------------------------------------------

// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// First program execution loop initializations.

IF (SysFirstLoop) THEN
TCPServer.FilesArr:=ADR(Fp); //Files array
TCPServer.LocalAdd:=ADR('0.0.0.0'); //Local address
TCPServer.LocalPort:=1010; //Local port
TCPServer.MaxConn:=1; //Accepted connections
TCPServer.FlushTm:=50; //Flush time (mS)
TCPServer.LifeTm:=30; //Life time (S)
TCPServer.RxSize:=128; //Rx buffer size
TCPServer.TxSize:=128; //Tx buffer size
END_IF;

// Manage the TCP server.

TCPServer(Enable:=TRUE); //TCPServer management

// -------------------------------------------------------------------------
// READ PEER INFOS
// -------------------------------------------------------------------------
// On a new connection, it reads the peer information.

IF (Pulse <> SysFIsOpen(Fp)) THEN
Pulse:=SysFIsOpen(Fp); //One shot

IF (Pulse) THEN
PeerInfos(File:=Fp); //Get peer infos
eTO_JUNK(Sysmemmove(ADR(PeerIP), PeerInfos.PeerIP, SIZEOF(PeerIP))); //Peer IP
PeerPort:=PeerInfos.PeerPort; //Peer port
END_IF;
END_IF;

// [End of file]