[英]conn.send('Hi'.encode()) BrokenPipeError: [Errno 32] Broken pipe (SOCKET)
hi i make model server client which works fine and i also create separate GUI which need to two input server IP and port
it only check whether server
is up or not.嗨,我制作了工作正常的模型服务器客户端,我还创建了单独的 GUI,它需要两个输入server IP and port
,它只检查server
是否启动。 But when i run server and then run my GUI and enter server IP and port it display connected
on GUI but on server side it throw this error.但是当我运行服务器然后运行我的 GUI 并输入服务器 IP 和端口时,它在 GUI 上显示已connected
,但在服务器端它会抛出此错误。 The Server Client working fine but integration of GUI with server throw below error on server side.服务器客户端工作正常,但 GUI 与服务器的集成在服务器端引发以下错误。
conn.send('Hi'.encode()) # send only takes string BrokenPipeError: [Errno 32] Broken pip
This is server Code:这是服务器代码:
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()
This is part of Gui Code which cause problem:这是导致问题的 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)
Your problem is simple.你的问题很简单。
Since the connection has been closed by the client, the server cannot send nor receive the next message inside the loop, since it is infinite.由于连接已被客户端关闭,服务器无法在循环内发送或接收下一条消息,因为它是无限的。 That is the cause of the error!这就是错误的原因! Also there is no error handling in case of closing the connection, nor a protocol for closing on each side.在关闭连接的情况下也没有错误处理,也没有在每一端关闭的协议。
If you need a function that checks whether the server is online or not, you should create a function, (but I'm sure a simple connect is enough), that works like a ping.如果您需要一个函数来检查服务器是否在线,您应该创建一个函数,(但我确信一个简单的连接就足够了),它的工作方式类似于 ping。 Example:例子:
Client function:客户端功能:
def isOpen(self, ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((str(ip), int(port)))
s.send("ping".encode('utf-8'))
return s.recv(1024).decode('utf-8') == "pong" # return whether the response match or not
except:
return False # cant connect
Server function:服务器功能:
def clientthread(conn):
while True:
msg = conn.recv(1024).decode('utf-8') #receiving a message
if msg == "ping":
conn.send("pong".encode('utf-8')) # sending the response
conn.close() # closing the connection on both sides
break # since we only need to check whether the server is online, we break
From your previous questions I can tell you have some problems understanding how TCP socket communication works.从您之前的问题中,我可以告诉您在理解 TCP套接字通信的工作原理方面存在一些问题。 Please take a moment and read a few articles about how to communicate through sockets.请花点时间阅读一些关于如何通过套接字进行通信的文章。 If you don't need live communications (continous data stream, like a video, game server, etc), only login forms for example, please stick with well-known protocols, like HTTP.如果您不需要实时通信(连续数据流,如视频、游戏服务器等),只需登录表单,请坚持使用众所周知的协议,如 HTTP。 Creating your own reliable protocol might be a little complicated if you just got into socket programming.如果您刚开始接触套接字编程,创建自己的可靠协议可能会有点复杂。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.