Astenoth
Posts: 19
Joined: Thu Jan 30, 2014 12:25 pm

error: [errno 9] bad file descriptor

Tue Mar 25, 2014 9:56 am

Hi,
I have the below code for a socket:
Server side:

Code: Select all

def server():
   s = socket.socket()
   s.bind(("localhost", 9999))  
   s.listen(1)
   s.setblocking(0)
   inputs = [s]
   outputs = [ ]
   while True:
      readable, writable, exceptional=select.select(inputs,outputs,[],0.6)
      for sock in readable:
               if sock is s:
                   client,address = sock.accept()
                   inputs.append(client)    
               else:
                   data = sock.recv(1024)
                   if data=="true":
                       global MinutoCtr, TCanal1, Canal1
                       Canal1,TCanal1,Luna,TLuna = readatos()
                       MinutoCtr = 77
                   elif data=="status":
                       outputs.append(client)                    
                   else:
                       sock.close()
                       if sock in outputs:
                          outputs.remove(sock)
                       inputs.remove(sock)       
      for sock in writable:
           sock.send (json.dumps({"IntDia": "%d" % (x),"PWDia":"%d" % (y),"IntNoche": "%d" % (x2)}))
           outputs.remove(sock)
           
   s.close()
Cliente side:

Code: Select all

s = socket.socket()
try:
    s.connect(("localhost", 9999))
    s.send("status")
    bufsize = 1024
    estados=s.recv(bufsize)
    print "Content-type:text/html\r\n\r\n"
    print estados
except:
    print "Content-type:text/html\r\n\r\n"
    print (json.dumps({"IntDia": "Sin respuesta"}))
	#print 
s.close()  
The code go fine, but after sometime I get the error... error: [errno 9] bad file descriptor
I read about this error and I understand that it is because the I am trying connect with the socket but the socket is closed already. But really I don't close the socket, the "s.close()" is out the loop.
Could somebody help me?
Regards
Raul

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: error: [errno 9] bad file descriptor

Tue Mar 25, 2014 5:49 pm

Where in the script do you get the error? Could you post it in full?

Dave.
Apple say... Monkey do !!

Astenoth
Posts: 19
Joined: Thu Jan 30, 2014 12:25 pm

Re: error: [errno 9] bad file descriptor

Tue Mar 25, 2014 6:51 pm

The complete error:

Code: Select all

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 505, in run
    self.__target(*self.__args, **self.__kwargs)
  File "aquansrV2.py", line 44, in server
    sock.send (json.dumps({"IntDia": "%d" % (x),"PWDia":"%d" % (y),"IntNoche": "                %d" % (x2)}))
  File "/usr/lib/python2.7/socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: error: [errno 9] bad file descriptor

Tue Mar 25, 2014 7:00 pm

Not a clue but this may skirt the problem...

Code: Select all

try:
    for sock in writable:
        sock.send (json.dumps({"IntDia": "%d" % (x),"PWDia":"%d" % (y),"IntNoche": "%d" % (x2)}))
        outputs.remove(sock)
except:
    pass
Dave
Apple say... Monkey do !!

Return to “Python”