Socket: 7.2 Sending and Receiving Data: SocketSend() and SocketRecv()

Up: GEOS SDK TechDocs| Up | Prev: 7.1 Urgent Data | Next: 7.3 Options

The SocketSend() routine sends data over a socket. It takes several arguments:

Socket
The socket through which to send data
Buffer
A buffer containing the data to send
Buffer Size
The size of the data buffer.
Optional Address
Address to which to send data. If you pass an address here, make sure that you set the Address flag in the Flags field. If you don't set the Address flag, then this Optional Address argument may be a null pointer.
For sequenced packet or stream delivery type sockets, optional address is ignored.
For datagram delivery type sockets, specify the destination address of the packet in this parameter unless the socket has a default destination address as described below.
Datagram sockets can use the SocketConnect() routine to specify a default destination address. If they have no default address, you must specify the destination address for the packet. If there is a default destination address and you pass this optional address, then the optional address overrides the default for this send.
The following flags are available:
SSF_ADDRESS
To pass an optional address.
SSF_URGENT
To mark the data packet as urgent.
SSF_OPEN_LINK
To open the link if it is closed or to close the current link if it is idle and open it to a different location. If you do not set this flag, the datagram will only be sent if the link is opened to the desired remote link address.

Note that in some domains, the driver may delay before sending small packets of data because it is waiting for more packets to combine into one large packet. In many cases, this leads to more efficient communication. You may turn off this behavior by setting the "no delay" option. See SO_NODELAY: No Delay for small packets To force a driver to send small packets without waiting for other packets to bundle together. Drivers often delay sending small packets, waiting for other packets to bundle together in one larger packet. Th for information about setting socket options.

The SocketRecv() routine receives data from the socket. After filling the passed buffer with data, the socket discards its copy of the received data to make room for the next piece of incoming data.

Sequenced packet or datagram delivery sockets can only receive whole packets. If you grab data from the socket but don't grab all the data in the packet, the remaining data is lost. Thus, it's a good idea to establish a maximum packet size for sockets using these delivery types. If you're not sure how much room you'll need to receive a packet, call SocketRecv() with the SRF_PEEK flag. This allows you to "peek" at the incoming data without causing the socket to discard its copy.

The SocketRecv() routine takes the following arguments:

Socket
The socket from which to grab the data.
Buffer
Buffer to fill with data.
Buffer Size
Size of the data buffer. A packet-based socket will not retain data that does not fit in the buffer. A stream socket, however, will.
Time-out
The number of 1/60 second ticks to wait for incoming data. Pass SOCKET_NO_TIMEOUT to wait forever.
Optional Address
If you pass the Address flag of the Flags argument, then pass an empty SocketAddress buffer. (The buffer's SA_domain , SA_domainSize , and SA_addressSize fields must be initialized.) Make sure the buffer has room for the address data after the SocketAddress structure. Note that if either the domain name buffer or the address buffer isn't long enough to hold its string, the resulting truncated string isn't null-terminated.
If you do not set the Address flag, this Optional Address argument may be a null pointer.
The following flags are available:
SRF_ADDRESS
Return the source address in the Optional Address buffer.
SRF_URGENT
Return the first packet which has been marked urgent. (See Urgent Data.)
SRF_PEEK
Force the socket to not discard the data received.

The SocketRecv() routine returns the size of the received buffer. If this size is zero, there may be an error in the connection. Use ThreadGetError() to check for an error. If ThreadGetError() returns SE_NORMAL, then there was no error and you received a zero-length packet. For a datagram connection, an SE_EXCEPTION error indicates that the network is having problems and some datagrams may not be getting through.


Up: GEOS SDK TechDocs| Up | Prev: 7.1 Urgent Data | Next: 7.3 Options