views:

846

answers:

3

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)
A: 

This sounds a lot like the anti-portscan measure of your firewall kicking in.

+1  A: 

I don't know that much about sockets, so this may be really bad style... use at own risk. This code:

#!/usr/bin/python
import threading, time
from socket import *
portrange = range(10000,10005)

class Sock(threading.Thread):
    def __init__(self, port):
        self.port = port
        threading.Thread.__init__ ( self )

    def start(self):
        self.s = socket(AF_INET, SOCK_STREAM)
        self.s.bind(("localhost", self.port))
        self.s.listen(1)
        print "listening on port %i"%self.port
        threading.Thread.start(self)
    def run(self):
        # wait for client to connect
        connection, address = self.s.accept()
        data = True
        while data:
            data = connection.recv(1024)
            if data:
                connection.send('echo %s'%(data))
        connection.close()

socketHandles = [Sock(port) for port in portrange]

for sock in socketHandles:
    sock.start()

# time.sleep(0.5)

for port in portrange:
    print 'sending "ping" to port %i'%port
    s = socket(AF_INET, SOCK_STREAM) 
    s.connect(("localhost", port))
    s.send('ping')
    data = s.recv(1024)
    print 'reply was: %s'%data
    s.close()

should output:

listening on port 10000
listening on port 10001
listening on port 10002
listening on port 10003
listening on port 10004
sending "ping" to port 10000
reply was: echo ping
sending "ping" to port 10001
reply was: echo ping
sending "ping" to port 10002
reply was: echo ping
sending "ping" to port 10003
reply was: echo ping
sending "ping" to port 10004
reply was: echo ping

perhaps this will help you see if it's your firewall causing troubles. I suppose you could try splitting this into client and server as well.

Markus
In the socket examples in the Python library docs, they don't send without receiving. Sockets can be fussy things so perhaps all the acks are being buffered until socket.recv() is called and after his third port, the buffer is full and gets stuck in some kind of wait state.
Michael Dillon
A: 

Are you running the 2nd Python program from within Idle? If so - try it outside of Idle and see if the results are any different.

Anon