Python:重用套接字时的错误文件描述符

时间:2017-05-07 20:15:55

标签: python sockets

我有一个我使用的套接字然后关闭它。我试图重新使用它,我收到以下错误:

  File "myCode.py", line 83, 
    votingSocket.bind((TCP_IP, TCP_VOTING_PORT))
  File "...\Anaconda3\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
  File "..\Anaconda3\lib\socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor

这是我的代码:

votingSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

def VotingSessionOnOff():
    global votingSocket
    if (on)
        if votingSocket is None:
            votingSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        votingSocket.bind((TCP_IP, TCP_VOTING_PORT))
        votingSocket.listen(NUMBER_OF_CLIENTS) 

        print 'Server opened voting socket!'
    else:
        votingSocket.close()

当我第一次调用VotingSessionOnOff()时它运行良好,再调用它会产生上一个错误。

0 个答案:

没有答案