GETSOCKNAME

Use the GETSOCKNAME command to retrieve the name of a bound socket.

Stream sockets are not assigned a name until after a successful BIND, CONNECT, or ACCEPT command is completed.

Tip: Use this command to discover the port number that is assigned to a socket after the socket has been implicitly bound, for example, after a CONNECT command has been completed.

Format

Read syntax diagramSkip visual syntax diagram
>>-SOCKET--(--"GETSOCKNAME"--,--socketid--)--------------------><

Parameters

socketid
The socket descriptor

Returned value

This command returns a string that contains the return code and the NAME string of the bound socket, for example, 0 AF_INET6 7 0 2001:197:11:103::1 0. 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.

The following REXX TCP/IP error numbers can be returned:
  • 9 EBADF
  • 38 ENOTSOCK
  • 45 EOPNOTSUPP
  • 57 ENOTCONN
The following REXX socket API error numbers can be returned:
  • 2001 EINVALIDRXSOCKETCALL
  • 2005 ESUBTASKNOTACTIVE
  • 2009 ESOCKETNOTDEFINED
  • 2012 EINVALIDNAME

LE C/C++ equivalent

int getsockname(int socket, struct sockaddr *name, int *namelen);

Code example

Figure 1. GETSOCKNAME command example
/* REXX EZARXR19 */
/*
 * This sample demonstrates the use of the GETSOCKNAME
 * socket command. After the remote peer is obtained
 * send is echoed to the remote server.
 *
 * 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");
if perror(src,"INITIALIZE") = 0 then do
   src = socket("SOCKET","AF_INET","STREAM");
   if perror(src,"SOCKET") = 0 then do
      l_socketid = WORD(src,2);
      l_RMTname = "AF_INET 7 127.0.0.1";
      src = socket("CONNECT",l_socketid,l_RMTname);
      if perror(src,"CONNECT") = 0 then do
         src = socket("GETSOCKNAME",l_socketid);
         if perror(src,"GETSOCKNAME") = 0 then do
            l_LOCname = SUBWORD(src,2);
            Say "The local socket name is: "l_LOCName;
         end;
         src = socket("SEND",l_socketid,"*******");
         if perror(src,"SEND") = 0 then do
            src = socket("RECV",l_socketid);
            if perror(src,"RECV") = 0 then
               Say "Echoed data: " word(src,3);
         end;
      end;
   end;
end;
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;