Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad file descriptor when using Pyside

Tags:

python

pyside

My code:

class Receiver(QWidget):
def __init__(self):
    self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #Create button
    QToolTip.setFont(QFont('Time New Roman',10))
    super(Example, self).__init__()
...
self.btnConnect.clicked.connect(self.connectserver)
    self.btnConnect.clicked.connect(self.disconnect)
    self.btnLeft.clicked.connect(self.turnleft)
    self.btnRight.clicked.connect(self.turnright)
def connectserver(self):
    self.s.connect((TCP_IP, TCP_PORT))
    length = recvall(conn,16)
    stringData = recvall(s, int(length))
    while True:
        data = numpy.fromstring(stringData, dtype='uint8')
        decimg=cv2.imdecode(data,1)
        cv2.imshow('Client',decimg)
        cv2.waitKey(10)
def disconnect(self):
    self.s.close()
def turnleft(self):
    self.s.send("left")
def turnright(self):
    self.s.send("right")

First, I click Connect button. It's worked, but when I click Turn left or Turn right, I got an error:

Traceback (most recent call last):
  File "D:\NEW\GUI.py", line 69, in turnright
    self.s.send("right")
  File "C:\Python27\lib\socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor
like image 773
Trần Duy Linh Avatar asked Nov 10 '22 21:11

Trần Duy Linh


1 Answers

You connected btnConnect to both connectserver and disconnect, so when you click it, it will connect and then immediately disconnect the socket.

If you try to send data to a closed socket, it will raise a bad file descriptor error.

like image 88
ekhumoro Avatar answered Nov 14 '22 23:11

ekhumoro