I made a chat application just for fun using just the socket library and threading, but now I am trying to make a GUI version of it. I am trying to have a first window where you choose your name and a second window where you send messages etc. My name window works with receiving messages but my text window does not work with receiving messages and I get:
[WinError 10022] An invalid argument was supplied.
MY CODE: https://github.com/BenTensMexicans/pyqt5thing
EDIT: Here is my minimal example,
#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_())
It does not even start but when windows prompts to terminate this and I terminate it it gives me the error previously described.