0

I have a socket that I use and then closed it. I f I tried to re use it, I get the following error:

  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

Here is my code:

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

When I call the VotingSessionOnOff() the first time it runs just good, calling it a further time gives the previous error.

A_Matar
  • 2,210
  • 3
  • 31
  • 53
  • Is `votingSocket` still opened or not `None`? – handle May 07 '17 at 20:24
  • it should be closed after the 1st call, I am not sure if it is set t None or not – A_Matar May 07 '17 at 20:25
  • You need to provide a calling sequence that produces the error, including code that sets/clears `on` between calls. – chepner May 07 '17 at 20:26
  • Possible duplicate of [Python socket (Socket Error Bad File Descriptor)](http://stackoverflow.com/questions/16382899/python-socket-socket-error-bad-file-descriptor) – Peter Wood May 07 '17 at 20:34
  • See also [How to use socket in Python as a context manager?](http://stackoverflow.com/questions/16772465/how-to-use-socket-in-python-as-a-context-manager) – Peter Wood May 07 '17 at 20:36

0 Answers0