95 pts.
 Determine TCP port used by a connection.
We have an application that has our iSeries(AS/400) connect to a remote IP address and port. If we have to end the application and restart it (IPL, error, end/start subsystem) the iSeries keeps the connection open. AFter the application is restarted, the TCP connections show two connections to the remote IP, and the application does not transfer data on the new(second) connection. I have looked at the ENDTCPCNN command, and it requires the local IP address and port. Is there any way to capture this information in a program? We would like to automate the ENDTCPCNN command so the operators do not need to look up the local port each time.

Software/Hardware used:
ASKED: August 25, 2008  7:29 PM
UPDATED: September 2, 2008  2:42 PM

Answer Wiki:
Interesting. Most TCP stacks should time out and close a connection after a period of non-activity. You don't say what the device is at the other end of the connection and if you have control of it. I can't answer AS/400 specifics but if the remote device is a Windows host then you can go to a command prompt and type <b>netstat -an</b>. This command will give you a list of the local addresses/ports and remote addresses/ports and the current state of those connections. There is a similar command on AS/400 and some details can be found <a href="http://search400.techtarget.com/tip/0,289483,sid3_gci1132847,00.html">here</a>. Maybe <a href="http://home.columbus.rr.com/jbmmdietz/nstat.html">this utility</a> would help.
Last Wiki Answer Submitted:  August 25, 2008  7:46 pm  by  Labnuke99   32,645 pts.
All Answer Wiki Contributors:  Labnuke99   32,645 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Hi,

If you’re IPL’ing the AS400, then it’s not the AS400 that’s keeping the connection open. If you IPL, then everthing is shutdown and restarted, including all communications. When the AS400 is restarted, then maybe it creates a new connection, but the old connection should be gone, unless the machine at the other end re-opens it. I suggest you check whatever is on the other end of your connection.

Regards,

Martin Gilbert.

 23,625 pts.

 

The device os a small TCP/IP-> Com port converter box which listens for a connection. The iSeries connects to this converter using TCP/IP. If we end the program that connects, or the subsystem that it runs in, and then restart the process the iSeries does not drop the connection. It will if we let it time out, but most of the time the stop/start happens within a few seconds. I would like to add an ENDTCPCNN command to make sure that the connection is closed, but the command requires the local port number. The connection is setup to connect to port yyyy on the converter. The iSeries will use any available port. I am looking for a way to get the local port number in a program. I know I can use netstat/wrktcpsts to find the port, but I am trying to automate the process so the operators do not have to find the local port number and manually enter the endtcpcnn command.

 95 pts.

 

Unfortunately, the ENDTCPCNN command exists so that you can limit authority to ending connections through NETSTAT.

The connections should time out and drop, but other than that I think you can only end the connection using NETSTAT. Take option #3, scroll down through the list and end with option #4.

Regards
Mike

 2,725 pts.

 

Thanks for all the answers. What I am looking to do is to not have the operators, or anyone, have to lookup the port number and end the connection (using ENDTCPCNN ro NETSTAT/Opt#3/Opt#4). I would like to have a program retrieve the local port number and end the connection. No manual intervention needed.

 95 pts.

 

You have a few options here.

If you have the source for the application program you could:

1. Use the bind() API (long name of Set Local Address for Socket) to turn off the port selection by the system. Just use a fixed port number and that’s what you’ll cancel on.

2. After you have established the connection use the getsockname() API (long name of Get Local Address of Socket). Given the socket desriptor the API will return to you the local address. This local address includes the port number. You could write the port number to a *DTAARA and use this stored value later, if needed, to end the connection. Using this approach I would lean towards clearing the value upon a successful close of the connection.

If you don’t have the source for the application program then you can get at the data that NetStat drives off of with the QtocLstNetCnn API (long name of List Network Connections). This API also returns port information.

I hope this helps,
Bruce Vining
http://www.brucevining.com/

 6,055 pts.

 

Thanks bvining,

After adding the bind call to my program, I was able to control the local port number. Adding the ENDTCPCNN command was just as easy. I have tested the ENDSBS/STRSBS a few times, and it works great.

Thanks to all who responded,

Len

 95 pts.