10

I have a problem with socket programming in Python 3. I get an exception that is not making the program crash, but is just shown in terminal.

Here is my code:

from PyQt4 import QtCore, QtGui
from imigui import Ui_MainWindow

class imiserv(QtGui.QMainWindow):

    send_msg = pyqtSignal('QString', 'QString')

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.Sport_lineEdit.setMaxLength(5)
        self.ui.Sconnect_pushButton.clicked.connect(self.serv)

        self.send_msg.connect(self.write_msg)

    def write_msg(self, lbl_msg= None, txt_msg= None):
        if lbl_msg:
            self.ui.C_label.setText(lbl_msg)
        if txt_msg:
            self.ui.Clog_textEdit.setText(txt_msg)

    def serv(self):
        MY_LOCK = threading.Lock()
        class CountT(threading.Thread):
            def __init__(self, parent):
                threading.Thread.__init__(self)
                self._parent= parent

            def run(self):
                MY_LOCK.acquire()
                self._parent.send_msg.emit("Waiting connections","")
                while True:
                    cliconn, (addr, remoport)= self._parent.clis.accept()
                    clirecmsg= str(cliconn.recv(1024)
                    self._parent.send_msg.emit("{0}:{1} is connected.".format(addr, remoport), "{0}:{1}".format(addr, remoport)
                    cliconn.close()

                MY_LOCK.release()

        try:
            self.clis= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.clis.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            clierhost= str(self.ui.Sip_lineEdit.text())
            clierport= int(self.ui.Sport_lineEdit.text())
            self.clis.bind((clierhost, clierport))
            self.clis.listen(5)
            a= CountT(self)
            a.daemon= True
            a.start()
        except socket.error as err:
            err= str(err)
            print(err)

And here are the errors that happened decussate (this error show only in linux os):

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.3/threading.py", line 637, in _bootstrap_inner
    self.run()
  File "imiclilap.py", line 34, in run
    cliconn, (addr, remoport)= self._parent.clis.accept()
  File "/usr/lib/python3.3/socket.py", line 135, in accept
    fd, addr = self._accept()
OSError: [Errno 22] Invalid argument

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.3/threading.py", line 637, in _bootstrap_inner
    self.run()
  File "imiclilap.py", line 34, in run
    cliconn, (addr, remoport)= self._parent.clis.accept()
  File "/usr/lib/python3.3/socket.py", line 135, in accept
    fd, addr = self._accept()
OSError: [Errno 22] Invalid argument
alireza
  • 1,173
  • 4
  • 19
  • 40
  • Take a look at http://stackoverflow.com/questions/14862742/invalid-argument-exception-in-socket-accept-if-i-restart-immediately-after-a-p – pfc Jan 17 '14 at 13:55
  • 8
    The code you posted can't be the code you have run: 1. it contains a syntax error; 2. it only starts one thread, not two; 3. `accept()` may raise "invalid argument" only if you have not called `listen()`. – Andrea Corbellini Sep 03 '15 at 15:56
  • 1
    You are calling `accept()` on a socket that hasn't been put into LISTEN state, but not with this code. – user207421 Oct 25 '19 at 05:49
  • Does this answer your question? [UDP socket sendto() functions](https://stackoverflow.com/questions/30268008/udp-socket-sendto-functions) – Brian Tompsett - 汤莱恩 Jul 11 '20 at 10:36

2 Answers2

0

here is a detailed answer https://stackoverflow.com/a/30268744/8447510

in short: use 192.168.1.x instead of 127.0.0.1

Khaled Dallah
  • 111
  • 1
  • 4
-3

Errorno 22 is a linux error defined as "Invalid Argument" so its possible your address or port are not the correct types. Check that

addr, remoport

are string, int respectively.

Its also possible you have already bound to the socket in a different thread.

You could run your program in strace 1 that would allow to you to see what sockets are getting used and if there is some double binding going on.

Also just a fyi:

Your program is not crashing because the error happens in a separate thread so that thread is crashing but your main thread is still going.

Ada
  • 97
  • 1
  • 11