recvfrom

Jump to: navigation, search

Table of Contents >> Programmer's Reference


#include <trsocket.h>


int recvfrom (
int socketDescriptor,
char * bufferPtr,
int bufferLength,
int flags,
struct sockaddr * fromPtr,
int * fromLengthPtr
);


Function Description

recvfrom() is used to receive messages from another socket. recvfrom() may be used to receive data on a socket whether it is in a connected state or not but not on a TCP socket. socketDescriptor is a socket created with socket(). If fromPtr is not a NULL pointer, the source address of the message is filled in. fromLengthPtr is a value-result parameter, initialized to the size of the buffer associated with fromPtr, and modified on return to indicate the actual size of the address stored there. The length of the message is returned. If a message is too long to fit in the supplied buffer, excess bytes may be discarded depending on the type of socket the message is received from (see socket()). If no messages are available at the socket, the receive call waits for a message to arrive, unless the socket is non-blocking, or the MSG_DONTWAIT flag is set in the flags parameter, in which case TM_SOCKET_ERROR is returned with socket error being set to EWOULDBLOCK. select() may be used to determine when more data arrives, or/and when out-of-band data arrives. tfRegisterSocketCB() may be used to asynchronously determine when more data arrives, or/and when out-of-band data arrives.


Parameters

  • socketDescriptor
    The socket descriptor from which to receive the data.
  • bufferPtr
    The buffer into which the received data is put.
  • bufferLength
    The length.
  • flags
    See below for possible flags parameters that can be OR'ed together.
  • fromPtr
    The socket from which the data is (or to be) received.
  • fromLengthPtr
    A pointer to the length of the data area that fromPtr points to. Upon return a pointer to the length of the from data.


Flags

Value Meaning
MSG_DONTWAIT Don't wait for data, but rather return immediately.
MSG_PEEK "Peek" at the data present on the socket; the data is returned, but not consumed, so that a subsequent recv() operation will see the same data.


Return Values

  • > 0
    Number of bytes actually received from the socket.
  • TM_ENOERROR
    EOF.
  • TM_SOCKET_ERROR
    Failure.


Note Note: TM_SOCKET_ERROR means that this socket call has failed and the errorCode has been set on the socket itself.

To retrieve the socket error the user must call tfGetSocketError(socketDescriptor).


Possible socket errors

  • TM_EBADF
    The socket descriptor is invalid.
  • TM_EINVAL
    One of the parameters is invalid.
  • TM_EMSGSIZE
    The socket requires the message to be received atomically, and bufferLength was too small.
  • TM_EPROTOTYPE
    The TCP protocol requires usage of recv(), not recvfrom().
  • TM_EWOULDBLOCK
    The socket is marked as non-blocking and no data is available to be read.


Table of Contents >> Programmer's Reference