2

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)
4

3 回答 3

1

这听起来很像防火墙启动的反端口扫描措施。

于 2009-07-18T03:40:15.737 回答
1

我不太了解套接字,所以这可能是一种非常糟糕的风格......使用风险自负。这段代码:

#!/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()

应该输出:

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

也许这将帮助您查看是否是您的防火墙引起了麻烦。我想您也可以尝试将其拆分为客户端和服务器。

于 2009-07-18T14:38:43.620 回答
0

您是否在 Idle 中运行第二个 Python 程序?如果是这样 - 在 Idle 之外尝试一下,看看结果是否有任何不同。

于 2009-07-18T14:53:34.487 回答