简体   繁体   English

conn.send('Hi'.encode()) BrokenPipeError: [Errno 32] Broken pipe (SOCKET)

[英]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.你的问题很简单。

  1. Your client connects to the server您的客户端连接到服务器
  2. The server is creating a new thread with an infinite loop服务器正在创建一个无限循环的新线程
  3. The server sends a simple message服务器发送一条简单的消息
  4. The client receives the message客户端收到消息
  5. The client closes the connection by default (!!!) , since you returned from its method (no more references)客户端默认关闭连接(!!!) ,因为您从其方法返回(不再引用)
  6. The server tries to receive a message, then proceeds (Error lies here)服务器尝试接收消息,然后继续(错误在此)

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.如果您刚开始接触套接字编程,创建自己的可靠协议可能会有点复杂。

You could use flask for an HTTP back-end.您可以将烧瓶用于 HTTP 后端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BrokenPipeError:[Errno 32] makefile 插座的 pipe 损坏? - BrokenPipeError: [Errno 32] Broken pipe for makefile socket? 获取 BrokenPipeError:[Errno 32] 发送第二个套接字 MSG 时损坏 pipe - Getting BrokenPipeError: [Errno 32] Broken pipe When Sending Second Socket MSG BrokenPipeError: [Errno 32] 运行 GAN 时管道损坏错误 - BrokenPipeError: [Errno 32] Broken pipe error when running GANs 已解决:Python 多处理 imap BrokenPipeError: [Errno 32] Broken pipe pdftoppm - Solved: Python multiprocessing imap BrokenPipeError: [Errno 32] Broken pipe pdftoppm 我得到 BrokenPipeError: [Errno 32] Broken pipe 错误 python - I get BrokenPipeError: [Errno 32] Broken pipe error in python socket.error:[Errno 32]管道破裂 - socket.error: [Errno 32] Broken pipe Python socket errno 32 断管 - Python socket errno 32 broken pipe Python socket.send() 只能发送一次,然后 socket.error: [Errno 32] Broken pipe 发生 - Python socket.send() can only send once, then socket.error: [Errno 32] Broken pipe occurred Matlab 服务器与树莓派上的 python 客户端 BrokenPipeError: [Errno 32] Broken pipe - Matlab Server with python client on raspberry pi BrokenPipeError: [Errno 32] Broken pipe Errno 32:管道损坏 - Errno 32: Broken pipe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM