Consultor Eletrônico



Kbase P127630: 4GL/ABL: How to test if specific port of a remote host machine is available and listening?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   12/14/2007
Status: Verified

GOAL:

4GL/ABL: How to test if specific port of a remote host machine is available and listening?

GOAL:

How to determine if the port on which a database server starts is alive and listening for clients.

GOAL:

How to avoid the long delays caused by failing to connect to a database on an unavailable port due to network or other causes?

FACT(s) (Environment):

All Supported Operating Systems
OpenEdge 10.x
Progress 9.1x

FIX:

The following procedure checks the availability of a specific port on a remote host by attempting to create a server socket object on the target port. If it succeeds, it disconnects from the server and deletes the created handle and proceeds to connects to the database running at that port. Please remember that the port being alive and well does not guarantee the existence of a database running on that port. The code assumes that whenever the database is running, it is running on the specified host and port number:
DEFINE VARIABLE hSocket AS HANDLE NO-UNDO.
DEFINE VARIABLE iPortNumber AS INTEGER NO-UNDO.
DEFINE VARIABLE cIPAddress AS CHARACTER NO-UNDO.
DEFINE VARIABLE cDBName AS CHARACTER NO-UNDO.
DEFINE VARIABLE lResult AS LOGICAL NO-UNDO.
ASSIGN
iPortNumber = 12345
cIPAddress = "192.168.0.100"
cDBName = "Sports2000".
CREATE SOCKET hSocket.
lResult = hSocket:CONNECT('-H ' + cIPAddress + ' -S ' + STRING( iPortNumber)) NO-ERROR.
IF hSocket:CONNECTED() THEN DO:
hSocket:DISCONNECT() NO-ERROR.
CONNECT VALUE(cDBName) -H VALUE(cIPAddress) -N tcp -S VALUE(iPortNumber) NO-ERROR.
END.
ELSE
MESSAGE "The specified port is not listening!"
VIEW-AS ALERT-BOX INFO BUTTONS OK.
MESSAGE "Connected to database:~t" CONNECTED("Sports2000")
VIEW-AS ALERT-BOX INFO BUTTONS OK.
DELETE OBJECT hSocket.