The following ABL procedure, attached to this KB article for convenience, demonstrates how to obtain the current ABL process PID and how to get the port number used by this process to connect to a given server.
The code works with any server regardless of whether it is a Web Services server, a Database server or an AppServer provided that the name of that server is known.
If the server name is not known but its IP address is known, then change the following two lines of code:
cCommandLine = "netstat -o > " + cFileName
cHostname = "myServerHostName".
to:
cCommandLine = "netstat -n -o > " + cFileName
cHostname = "nnn.nnn.nnn.nnn". /* This is the server's IP address for example "112.118.197.151". */
If the current ABL client has multiple connections to the given server, then all the ports will be displayed in the message statement. If there is only one connection to that server, then the code may be modified to LEAVE the REPEAT block after that port is found.
/* getThisClientPIDAndPortNumbers.p */
USING System.Diagnostics.*.
DEFINE VARIABLE iThisClientPID AS INTEGER NO-UNDO.
DEFINE VARIABLE cCommandLine AS CHARACTER NO-UNDO.
DEFINE VARIABLE iThisClientPort AS INTEGER NO-UNDO.
DEFINE VARIABLE cHostname AS CHARACTER NO-UNDO.
DEFINE VARIABLE cFileName AS CHARACTER NO-UNDO.
DEFINE VARIABLE cLIne AS CHARACTER NO-UNDO.
ASSIGN
cFileName = "netstat.txt"
cCommandLine = "netstat -o > " + cFileName
cHostname = "pcbedyshanshi2".
RUN GetThisClientProcessId (OUTPUT iThisClientPID).
OS-COMMAND SILENT VALUE(cCommandLine).
RUN GetThisClientPort (OUTPUT iThisClientPort).
PROCEDURE GetThisClientProcessId:
DEFINE OUTPUT PARAMETER opiThisClientPID AS INTEGER.
opiThisClientPID = Process:GetCurrentProcess():Id.
END PROCEDURE.
PROCEDURE GetThisClientPort:
DEFINE OUTPUT PARAMETER opiThisClientPort AS INTEGER.
DEFINE VARIABLE iStartPortNumber AS INTEGER NO-UNDO.
DEFINE VARIABLE iEndPortNumber AS INTEGER NO-UNDO.
DEFINE VARIABLE iLengthPortNumber AS INTEGER NO-UNDO.
INPUT FROM VALUE(cFileName).
REPEAT:
IMPORT UNFORMATTED cLine.
IF (INDEX(cLine, STRING(iThisClientPID)) > 0 ) AND INDEX (cLine, cHostname) > 0 THEN DO:
iStartPortNumber = INDEX (cline, ":", 1) + 1.
iEndPortNumber = INDEX (cLine, CHR(32), iStartPortNumber).
iLengthPortNumber = iEndPortNumber - iStartPortNumber.
MESSAGE
"The full line from netstat report:~t" cline "~n"
" The port number of interest:~t~t" SUBSTRING(cLine, iStartPortNumber, iLengthPortNumber)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
END.
END.
INPUT CLOSE.
END PROCEDURE.