I have some code I'm testing. Program A listens on a predefined socket for program B to connect to and for my testing I'm running 32 instances of the programs A and B. I've written my script to tell program A to listen to port 9001-9032 and put in the configuration files for each instance of program B the corresponding port to connect to.
When I run the script which starts up program A and B and run
netstat -tnap I find that of all the ports 9001-9032 the only port actually used is 9001 which is used by another program (my mistake!). When I grep for program A, I find each instance is listening on a seemingly completely random ports that have no relationship to the ports 90** they should be listening on. However, my program B, which should be connecting to ports 9001-9032, is able to find and connect to each instance of program A despite A apparently not listening on the ports it's B should be trying to connect to. I'm rather confused as to how that's possible.
If that isn't confusing enough my 32 program and only the 32 one (listening on 9032) dies at startup with an error that the address was refused when it tries to bind to the port. I tried changing the programs to listen on ports 15001-15032 just to see what would happen and I still got the exact same behavior; all programs seem to be listening on random ports and the 32 program can't establish a connection.
Can anyone explain why my ports are behaving the way the are? Am I misunderstanding some part of netstat or Linux ports?