1

我不断得到这个:

    Traceback (most recent call last):
  File "C:\Users\T_Mac\Desktop\Rex's Stuff\PyNet\Client.py", line 14, in <module
>
    server.connect(ADDRESS)
  File "C:\Python27\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
  File "C:\Python27\lib\socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor

当我使用此代码作为服务器运行“changeclient”时:

#  Server

from socket import *

PORT = 5000
BUFSIZE = 1024
ADDRESS = ('', PORT)        # '' = all addresses.
server = socket(AF_INET, SOCK_STREAM)

server.bind(ADDRESS)
server.listen(5)
# print stuff the user needs to know
print ''
print '  ____              _____     ___    _______    '
print ' /    \  |      |  /     \   /____\     |       '
print '|      | |      | |       | |           |       '
print ' \____/   \____/| |       |  \____/     |   v0.1'
print ' |              |                               '
print ' |              |                               '
print ' |              |                               '
print ' |        _____/                                '
print 'Contact Rex for any bug reports at rexploits@gmail.com'
print '\n'
print 'Please input the command when prompted with \'>\''
print 'The stdout stuff will be in this format: '
print '     (<stdout>, <stderr>)\n'

while True:
    END_SCRIPT = 0                                          #Setting command to something other than '1'
    print '\nWaiting for connections...'
    client, address = server.accept()
    print '...client connected from ', address[0], '\n'
    while True: 
        command = raw_input('> ')
        if command == 'quit':
            server.close()
            END_SCRIPT = 1
            break
        elif command == 'exit':
            server.close()
            END_SCRIPT = 1
            break
        elif command == 'changeclient':
            print 'Changing clients.....\n'
            client.send(command)
            break
        else:
            client.send(command)
            commandJazz = client.recv(BUFSIZE)
            print commandJazz
    if END_SCRIPT == 1:
        print 'Closing server......'
        print 'Goodbye!'
        break
server.close()

这是我的客户:

#  client

from subprocess import *
from socket import *
import time
test = 0
PORT = 5000
IP = 'localhost'        #To change it to your ip, delete 'raw_input('> ')' and put your IP in its place.
BUFSIZE = 1024
ADDRESS = (IP, PORT)
server = socket(AF_INET, SOCK_STREAM)

while True:
    server.connect(ADDRESS)
    while True:
        command = server.recv(BUFSIZE)
        if command == 'changeclient':
            server.close()
            test = 1
            break
        else:
            executeIt = Popen(command, shell = True, stdin = PIPE, stdout = PIPE, stderr = STDOUT)
            commandJazz = executeIt.communicate()
            strCommandJazz = str(commandJazz)
            server.send(strCommandJazz)

我运行我的服务器,然后运行我的客户端的两个实例。它连接良好,一切正常。我已经内置了一个名为 changeclient 的命令来断开当前客户端并连接到另一个客户端。每当我执行 changeclient 时,我都会在客户端上收到之前发布的错误。

4

1 回答 1

1

当你关闭你的套接字时,不要重复使用它。创建一个新的:

server = socket(AF_INET, SOCK_STREAM)
server.connect(ADDRESS)

现在您正尝试在同一个套接字实例上重新连接。

您还可以尝试使用一个标志来告诉套接字在您重新打开它而不是创建一个新端口时重用它。

server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
于 2012-05-08T02:34:46.200 回答