ACCEPT

Use the ACCEPT command to accept new connections from a client.

This command is valid only for stream sockets. Connection requests are processed in the order in which they are received. When a new connection is accepted, a new socket ID is created with the same properties as the listening socket ID. The new socket ID cannot be used to accept new connections. The original socket remains available to accept new connection requests.

This command supports both blocking and nonblocking sockets.

Rule: When the listening socket is in blocking mode, the ACCEPT command blocks the caller until a new connection request is received. When the listening socket is in nonblocking mode, the ACCEPT command immediately returns the 35 EWOULDBLOCK return code.
Tip: When you use blocking or nonblocking socket calls, use the SELECT command to check for new connection requests before you call the ACCEPT command. The availability of a new connection is reported as a READ event on the listening socket. The new socket should not be accepted until after the READ event is received.

Format

Read syntax diagramSkip visual syntax diagramSOCKET( "ACCEPT",socketid )

Parameters

socketid
The socket descriptor of the listening socket where new connection requests are queued

Returned value

The command returns a string that contains the return code, the new scopeid value for the accepted connection, and the NAME string of the connecting client. 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.

Consider the following IPv4 and IPv6 examples:
IPv4
The string, 0 6 AF_INET 50000 10.1.2.3, is an example of what this command returns. 0 is the function return code, 6 is the new socket ID of the accepted connection, AF_INET is the address family to which the socket belongs, 50000 is the remote port from which the client is connecting, and 10.1.2.3 is the remote address from which the client is connecting.
IPv6
The string, 0 6 AF_INET6 5462 0 2001:10:11:103::1 0, is an example of what this command returns. 0 is the function return code, 6 is the new socket ID of the accepted connection, AF_INET6 is the address family to which the socket belongs, 5462 is the remote port from which the client is connecting, 0 is the flowinfo value, 2001:10:11:103::1 is the remote address from which the client is connecting, and 0 is the scopeid value. For IPv6 connections, the flowinfo and scopeid fields are set to 0.

See Socket call error return codes for additional information about the numeric error codes that are returned by this command.

The following REXX TCP/IP error numbers can be returned:
  • 1 EPERM
  • 9 EBADF
  • 22 EINVAL
  • 35 EWOULDBLOCK
The following REXX socket API error numbers can be returned:
  • 2001 EINVALIDRXSOCKETCALL
  • 2006 ESOCKETNOTDEFINED
  • 2007 EMAXSOCKETSREACHED
  • 2009 ESOCKETNOTDEFINED
  • 2012 EINVALIDNAME

LE C/C++ equivalent

int accept(int socket, struct sockaddr *address, int *address_len);

Code example

Figure 1. ACCEPT command example


/* REXX EZARXR01 */
/*
 * This sample demonstrates the use of the INITIALIZE, SOCKET, BIND
 * LISTEN, ACCEPT, RECV, CLOSE, and TERMINATE socket commands.
 *
 * 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 one receive command and then close
 * the connection. If the data received is the string "DONE", then the

 * program will close the listening socket and terminate. Otherwise the
 * program will wait for the next connection.
 *
 * RESTRICTION: This program is designed to read 1 packet with a max
 *              size of 512 bytes.
 *
 * GUIDELINE: It is generally recommended that a program loop around
 *            the RECV command to ensure that all data is read off
 *            the socket. This sample does not follow the guideline.
 */
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
            l_Done = "FALSE";
            do until l_Done = "TRUE";
               say "Listening on socket "l_sockid;
               src = socket("ACCEPT", l_sockid );
               if perror(src,"ACCEPT") = 0 then do
                  parse var src . l_newsockid . ;
                  src = socket("RECV",l_newsockid,512);
                  parse var src l_retcode l_datalen l_data
                  if l_data = "DONE" | perror(src,"RECV") \= 0 then
                     l_Done = "TRUE";
                  src = socket("CLOSE",l_newsockid);
                  src = perror(src,"Accepted socket close");
               end;
               else do
                  src = socket("CLOSE",l_sockid);
                  src = perror(src,"Listen close");
                  l_done = "TRUE";
               end; /* ACCEPT */
            end; /* DO */
         end; /* LISTEN */
      end; /* BIND */
   end; /* SOCKET */
end; /* INITIALIZE */
src = socket("TERMINATE","MYSET01");
src = perror(src,"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;