GLBasic User Manual

Main sections

SOCK_RECV

rv% = SOCK_RECV(sock%, BYREF msg$, length%)


With this function you read data from a TCP stream previously created with SOCK_TCPACCEPT() or SOCK_TCPCONNECT().
You can also use this function to receive UDP messages sent to a port previously bound with SOCK_UDPOPEN().

msg$ is the transmitted message, or empty in the case of an error.
length% specifies the maximum number of characters to be received.
The message is read until either the maximum length is reached or the first '\0' character is read. If you read binary data with INLINE, you have to check the return value to find the real length.

Since GLBasic uses non blocking sockets by default, there may be an error if the socket is not ready to receive data yet.

The return value could mean:
-2 : a non blocking socket is not ready to be read and would block. (The socket has no data to read available right now). Try reading from that socket a bit later.
-1 : there was an error.
0 : the connected host has disconnected (only for TCP). As a result this socket is useless and must be closed with SOCK_CLOSE().
>0 : number of successfully read bytes.

You can retrieve any error messages with NETGETLASTERROR$().

See also...