【发布时间】:2021-01-22 00:52:45
【问题描述】:
嗨,我制作了运行良好的模型服务器客户端,我还创建了单独的 GUI,需要两个输入 server IP and port 它只检查 server 是否启动。但是当我运行服务器然后运行我的 GUI 并输入服务器 IP 和端口时,它会在 GUI 上显示 connected 但在服务器端它会抛出此错误。服务器客户端工作正常,但 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)
【问题讨论】: