你好,我制作了模型服务器客户端,它工作得很好,我还创建了单独的 GUI,它需要两个输入,server IP and port它只检查server是否启动。但是,当我运行服务器,然后运行 GUI 并输入服务器 IP 和端口时,它会显示connected在 GUI 上,但在服务器端会抛出此错误。服务器客户端工作正常,但 GUI 与服务器的集成在服务器端抛出以下错误。
conn.send('Hi'.encode()) # send only takes string BrokenPipeError: [Errno 32] Broken pip
这是服务器代码:
from socket import *
# Importing all from thread
import threading
# Defining server address and port
host = 'localhost'
port = 52000
data = " "
# Creating socket object
sock = socket()
# Binding socket to a address. bind() takes tuple of host and port.
sock.bind((host, port))
# Listening at the address
sock.listen(5) # 5 denotes the number of clients can queue
def clientthread(conn):
# infinite loop so that function do not terminate and thread do not end.
while True:
# Sending message to connected client
conn.send('Hi'.encode('utf-8')) # send only takes string
data =conn.recv(1024)
print (data.decode())
while True:
# Accepting incoming connections
conn, addr = sock.accept()
# Creating new thread. Calling clientthread function for this function and passing conn as argument.
thread = threading.Thread(target=clientthread, args=(conn,))
thread.start()
conn.close()
sock.close()
这是导致问题的 Gui 代码的一部分:
def isOpen(self, ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, int(port)))
data=s.recv(1024)
if data== b'Hi':
print("connected")
return True
except:
print("not connected")
return False
def check_password(self):
self.isOpen('localhost', 52000)
红糖糍粑
相关分类