断开与服务器的连接时出错

时间:2012-05-08 00:29:02

标签: python python-2.7

我一直这样:

    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时,我都会在我的客户端上收到先前发布的错误。

1 个答案:

答案 0 :(得分:1)

关闭套接字时,请勿重复使用。创建一个新的:

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

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

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

server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)