【问题标题】:conn.send('Hi'.encode()) BrokenPipeError: [Errno 32] Broken pipe (SOCKET)conn.send('Hi'.encode()) BrokenPipeError: [Errno 32] Broken pipe (SOCKET)
【发布时间】: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)

【问题讨论】:

    标签: python sockets


    【解决方案1】:

    你的问题很简单。

    1. 您的客户端连接到服务器
    2. 服务器正在创建一个无限循环的新线程
    3. 服务器发送一条简单的消息
    4. 客户端收到消息
    5. 客户端默认关闭连接(!!!),因为您从它的方法返回(不再引用)
    6. 服务器尝试接收消息,然后继续(此处出错)

    由于客户端已关闭连接,服务器无法在循环内发送或接收下一条消息,因为它是无限的。这就是错误的原因!在关闭连接的情况下也没有错误处理,也没有关闭每一端的协议。


    如果您需要一个检查服务器是否在线的函数,您应该创建一个函数(但我确信一个简单的连接就足够了),它的工作原理类似于 ping。 例子:


    客户端功能:

    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
    

    服务器功能:

    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
    

    根据您之前的问题,我可以告诉您在理解 TCP socket communication 的工作原理方面存在一些问题。请花点时间阅读几篇关于如何通过套接字进行通信的文章。如果您不需要实时通信(连续数据流,如视频、游戏服务器等),仅需要登录表单,请坚持使用众所周知的协议,如 HTTP。如果您刚接触套接字编程,创建自己的可靠协议可能会有点复杂。

    您可以使用 flask 作为 HTTP 后端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      相关资源
      最近更新 更多