Use the GETPEERNAME command to return the name of the remote peer that is connected to the socket.
This command returns a string that contains the return code and the name of the remote peer, for example, 0 NAME. 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 getpeername(int socket, struct sockaddr *name, int *namelen);
/* REXX EZARXR14 */
/*
* This sample demonstrates the use of the GETPEERNAME
* socket command.
*/
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("GETPEERNAME",l_socketid);
if perror(src,"GETPEERNAME") = 0 then do
l_PeerName = Subword(src,2);
say "The remote peer is: "l_PeerName;
end;
end;
end;
src = perror(socket("CLOSE",l_socketid),"CLOSE");
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;