Use the RECVFROM command to receive data on the specified socket.
If the number of bytes is less than the number of bytes requested, the command returns the number of bytes that are available. If the socket is in blocking mode and data is not available on the socket, the command blocks until data arrives. When the socket is in nonblocking mode and data is not available, the command returns the 35 EWOULDBLOCK return code .
.-,--10 000----. >>-SOCKET--(--"RECVFROM"--,--socketid--+--------------+---------> '-,--maxlength-' >--+--------------+--)----------------------------------------->< '-,--recvflags-'
The command returns a string that contains the return code, a NAME string, the maximum length of the data returned, and the data. The return code can be 0, a REXX socket API error number, or the REXX TCP/IP error number that is set by the socket command. The return code 0 indicates that the requested socket command was completed successfully.
For information about the format of the NAME string, see How structures are represented. See Socket call error return codes for additional information about the numeric error codes that are returned by this command.
int recvfrom(int socket, char *buffer, int length, int flags,
struct sockaddr *address, int *address_length);
/* REXX EZARXR21 */
/*
* This sample demonstrates the use of the READ and RECV
* socket commands.
*
* To use the READ command, set the variable g_RECVCMD equal to "READ"
* to use the RECV command, set the variable g_RECVCMD equal to "RECV"
*
* The program creates a listening socket and then goes into a
* loop and blocks on the accept command. When a new connection is
* ACCEPTED the program will issue the READ or RECV command until
* the connection is terminated.
*
* If the data received is the string "DONE", then the
* program will close the accepted socket and wait for a new
* connection request.
*/
g_RECVCMD = "READ"
src = socket("INITIALIZE","MYSET01",10);
if perror(src,"INITIALIZE") = 0 then do
src = socket("SOCKET","AF_INET6","STREAM");
if perror(src,"SOCKET") = 0 then do
parse var src . l_sockid
l_name6 = "AF_INET6 54004 0 ::0 0";
src = socket("BIND", l_sockid, l_name6);
if perror(src,"BIND") = 0 then do
src = socket("LISTEN", l_sockid);
if perror(src,"LISTEN") = 0 then do
say "Listening on socket "l_sockid;
do forever
src = socket("ACCEPT", l_sockid );
if perror(src,"ACCEPT") = 0 then do
parse var src . l_newsockid . ;
l_datalen = -1;
l_Done = "FALSE";
l_totallen = 0;
l_packet = "";
/* **********************************************
* Loop around RECV|READ command until all data has
* has been received and the client closes the
* connection.
* **********************************************/
do until l_datalen = 0 | l_done = "TRUE"
src = socket(g_RECVCMD,l_newsockid,512);
if perror(src,g_RECVCMD) = 0 then do
parse var src l_retcode l_datalen l_data
if l_datalen > 0 then do
l_totallen = l_totallen + l_datalen;
if l_packet = "" then do
l_packet = l_data;
if l_packet = "DONE" then
l_done = "TRUE";
end;
else l_packet = l_packet||l_data;
end;
else do
/* ***************************************
* A data length of zero indicates the
* connection has been closed by the
* remote side
* ***************************************/
Say "Connection has been closed",
"received "l_totallen" bytes";
l_done = "TRUE";
end;
end;
else do
l_done = "TRUE";
end;
end; /* DO READ */
src = socket("CLOSE",l_newsockid);
src = perror(src,"CLOSE");
end; /* ACCEPT */
end; /* DO FOREVER */
end;
end;
end;
end; /* INITIALIZE */
src = perror(socket("TERMINATE","MYSET01"),"TERMINATE");
exit 0;
/* This routine returns -1 if the first word if arg 1 is not zero */
perror: if word(arg(1),1) = 0 then return 0; else
Say arg(2) "Error : "arg(1);
return -1;