I have a program that listens on a port waiting for a small amount of data to tell it what to do. I run 5 instances of that program, one on each port from 5000 to 5004 inclusively.
I have a second program written in Python that creates a socket "s", writes the data to port 5000, then closes. It then increments the port number and creates a new socket connected to 5001. It does this until it reaches 5004.
The first three socket connections work just fine. The programs on ports 5000 to 5002 receive the data just fine and off they go! However, the fourth socket connection results in a "Connection Refused" error message. Meanwhile, the fourth listening program still reports that it is waiting on port 5003 -- I know that it's listening properly because I am able to connect to it using Telnet.
I've tried changing the port numbers, and each time it's the fourth connection that fails.
Is there some limit in the system I should check on? I only have one socket open at any time on the sending side, and the fact that I can get 3 of the 5 through eliminates a lot of the basic troubleshooting steps I can think of.
Any thoughts? Thanks!
--- EDIT ---
I'm on CentOS Linux with Python version 2.6. Here's some example code:
try:
portno = 5000
for index, data in enumerate(data_list):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('myhostname', portno))
s.sendall(data)
s.close()
portno = portno + 1
except socket.error, (value,message):
print 'Error: Send could not be performed.\nPort: ' + str(portno) + '\nMessage: ' + message
sys.exit(2)