gpt4 book ai didi

python-3.x - WinError 10022 : An invalid argument was supplied, 我不知道为什么

转载 作者:行者123 更新时间:2023-12-03 11:52:46 28 4
gpt4 key购买 nike

我仅使用套接字库和线程制作了一个聊天应用程序,只是为了好玩,但现在我正在尝试制作它的 GUI 版本。我试图有一个选择你的名字的第一个窗口和你发送消息等的第二个窗口。我的名字窗口可以用于接收消息,但我的文本窗口不能用于接收消息,我得到:

[WinError 10022] An invalid argument was supplied. 

我的代码: https://github.com/BenTensMexicans/pyqt5thing

编辑:这是我最小的例子,
#SIMPLE SERVER
import socket
import threading

connections = []


def handler(conn, addr):
while True:
try:
msg = conn.recv(1024).decode("utf-8")
except:
break
for x in connections:
x.send(f"{addr[0]}: {msg}".encode("utf-8"))


def start():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostbyname(), 8888))

s.listen(5)

while True:
conn, addr = s.accept()
t = threading.Thread(target=handler, args=(conn, addr))
t.start()


start()


#PYQT5 BASIC CLIENT
import socket

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QThreadPool, QThread

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#removed setup so it does not look overwelming

self.c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.c.connect((socket.gethostname(), 8888))

self.lineEdit.returnPressed.connect(self.sendmsg)



self.thread = QThread()
self.thread.start(self.recieve())

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

def sendmsg(self):
self.c.send(self.lineEdit.text().encode("utf-8"))
self.lineEdit.clear()
def recieve(self):
while True:
msg = self.c.recv(1024).decode("utf-8")
if msg:
txt = txt + msg + "\n"
self.plainTextEdit.setPlainText(txt)


if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

它甚至没有启动,但是当 Windows 提示终止它而我终止它时,它给了我前面描述的错误。

最佳答案

您的代码在处理线程时有几个错误:

  • self.thread.start(self.recieve())没有意义,看起来您将 QThread 与 python 线程混淆了。
  • 即使上述方法有效,它仍然是一个错误,因为您不应该从另一个线程更新 GUI。

  • 您可以使用 QtNetwork 中的 QTcpSocket,而不是搞乱线程:
    import socket

    from PyQt5 import QtCore, QtGui, QtWidgets, QtNetwork


    class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
    super().__init__(parent)

    self._socket = QtNetwork.QTcpSocket(self)

    self.lineEdit = QtWidgets.QLineEdit()
    self.plainTextEdit = QtWidgets.QPlainTextEdit(readOnly=True)

    central_widget = QtWidgets.QWidget()
    self.setCentralWidget(central_widget)
    lay = QtWidgets.QVBoxLayout(central_widget)
    lay.addWidget(self.lineEdit)
    lay.addWidget(self.plainTextEdit)

    self.socket.readyRead.connect(self.receive_message)
    self.lineEdit.returnPressed.connect(self.send_message)
    self.socket.connected.connect(self.on_connected)
    self.socket.disconnected.connect(self.on_disconnected)

    try:
    server_name = socket.gethostbyname()
    except:
    server_name = socket.gethostbyname(socket.gethostname())
    self.socket.connectToHost(QtNetwork.QHostAddress(server_name), 8888)

    self.lineEdit.setEnabled(False)

    @property
    def socket(self):
    return self._socket

    def send_message(self):
    msg = self.lineEdit.text()
    self.socket.write(msg.encode("utf-8"))

    def receive_message(self):
    data = self.socket.readAll()
    msg = data.data().decode("utf-8")
    self.plainTextEdit.appendPlainText(msg)

    def on_connected(self):
    self.lineEdit.setEnabled(True)

    def on_disconnected(self):
    self.lineEdit.setEnabled(False)


    if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

    注: for x in connections:它不会做任何事情,因为 connections是空的,如果你想有回声那么你必须添加 connections.append(conn)之后 conn, addr = s.accept()

    关于python-3.x - WinError 10022 : An invalid argument was supplied, 我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62335330/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com