Srecv
Previous Topic  Next Topic 

Synopsis: 

       Srecv (socket, no_bytes [,timeout_10ths[ ,&pData_buf]]);        


Arguments:

       socket - The socket handle

       no_bytes - The number of bytes to read from the socket

       [timeout_10ths] - Optional timeout in 10ths seconds

       [pData_buf] - Optional pointer to a variable to hold the returned data..

       

Description:   This function is used to receive data on the given socket.     The no_bytes argument specifies the maximum number of bytes to read from the socket.   If there is less than no_bytes received waiting to be received on the socket then the function will return these bytes.


If the optional timeout_10ths argument is specified then the function will block until the speicifed number of bytes has been read or the timeout has expired (in which case the function will return -3).       A timeout value of 0 will cause the function to return immediately (which is the default value for this optional argument).    A negative timeout value will cause the function to


The data received on the socket is usually returned by the function,  however this can sometime cause confusion if the data received is a string like "-3", which would be confused with the return value "-3" (EWOULDBLOCK).       Therefore it is better to pass a pointer to a variable that will receive the data using the pData_buf argument.      If this argument is given then all data received on the socket will be placed in the variable pointed to by this argument and the function itself will return 0 when some data has been received or a negative error value.


Below is an example where one character at a time is received from the socket and added to a string until a newline character is received.


       // Keep reading from socket until we receive a newline character

       while(1)

               var char:1;

               var msg:255;

               int x;


               x=Srecv(socket,1,0,&char);

               // -3 indicates no character is waiting

               if(x eq -3)

                       sleep(1);

                       continue;

               // Any other negative value is an error

               else if(x < 0)

                       errlog("Error reading socket: err=",x);

                       stop;

               endif endif

               

               // if we get here then we have received a character

               

               // append it to message variable (unless it is newline)

               if(char streq "`n")

                       // newline indicates end of message so break loop

                       break;

               endif

               // append character to message string

               msg=msg&char;                

       endwhile



Returns:    If pData_buf is given then the function will return 0 if successful or a negative error value (and the received data is place in the variable pointed to by pData_buf).          If pData_buf is not given then the function will return the received data on the socket or a negative error code.   In particular -3 (EWOULDBLOCK) is returned if there is less than the number of bytes specified by no_bytes waiting on the socket.

.