soRecvFrom (Function)

Top  Previous  Next

Architecture:

NX32L

Firmware version:

1.08.00


The soRecvFrom function is identical to the soRecv function, except it also returns the local address and the address the data originated from.

The soRecvFrom function is typically used with connectionless sockets, where this is the only way to get the source address (and local address).

 

 

Input:

socket : SYSHANDLE

Handle to the socket.

 

data : PTR

Address of the buffer that contains the received data.

 

maxsize : DINT

Maximum number of bytes that can be received (size of "data").

 

 

Output:

size : DINT

The number of bytes received.

 

local : STRING

The socket address of the network interface where the data was received.

 

remote : STRING

The socket address of the source of the data.

 

 

Returns: INT

1

- Success.

0

- The function is not supported.

-1

- The handle is not a valid socket.

-2

- One or more parameters are illegal.

-3

- The socket is closed.

-4

- The socket is marked non-blocking and the operation would block.

-6

- The socket require a connection and is not connected.

-11

- Memory error.

-17

- Generic error.

-110

- The secure transfer failed.

-111

- The secure connection is closed.

 

 

Declaration:

FUNCTION soRecvFrom : INT;
VAR_INPUT
   socket  : SYSHANDLE;
   data    : PTR;
   maxsize : DINT;
   size    : ACCESS DINT;
   local   : ACCESS STRING;
   remote  : ACCESS STRING;
END_VAR;

 

 


Example:

 

INCLUDE rtcu.inc
 
PROGRAM udp_example;
VAR
  handle  : SYSHANDLE;
  address : STRING;
 
  buf     : ARRAY [1..230OF SINT;
  size    : DINT;
  local   : STRING;
  remote  : STRING;
  rc      : INT;
 
  port    : DINT;
  host    : STRING;
  str     : STRING;
END_VAR;
 
// Open network
netOpen(iface := _NET_IFACE_LAN1);
 
// Open UDP socket
rc := soCreate(type := _SO_TYPE_DGRAMprotocol := _SO_PROT_UDPsocket := handle);
IF rc < 1 THEN
  // Error handling
  DebugFmt(message := "soCreate=\1", v1 := rc);
END_IF;
 
// Listen to port 5022 on any network interface
soAddrInetSet(address := addressport := 5022);
rc := soBind(socket := handleaddress := address);
IF rc < 1 THEN
  // Error handling
  DebugFmt(message := "soBind=\1", v1 := rc);
END_IF;
 
BEGIN
  // Read data (blocks until data is available)
  rc  := soRecvFrom(
                    socket  := handle,
                    data    := ADDR(buf),
                    maxsize := SIZEOF(buf),
                    size    := size,
                    local   := local,
                    remote  := remote
                   );
  IF rc < 1 THEN
    // Error handling
  END_IF;
 
  // Get information
  soAddrInetGet(address := remotehost := hostport := port);
  str := strFromMemory(src := ADDR(buf), len := INT(size));
 
  // Show information
  DebugFmt(message := " soRecvFrom    = \1", v1 := rc);
  DebugFmt(message := "    interface  = \1", v1 := soAddrToInterface(address := local));
  DebugMsg(message := "    IP address = " + host);
  DebugFmt(message := "    IP port    = \4", v4 := port);
  DebugMsg(message := "    data       = [" + str + "]");
END;
END_PROGRAM;