亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

conn.send('Hi'.encode()) BrokenPipeError:

conn.send('Hi'.encode()) BrokenPipeError:

MYYA 2023-10-18 15:58:59
你好,我制作了模型服務器客戶端,它工作得很好,我還創建了單獨的 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 threadimport threading# Defining server address and porthost = 'localhost'port = 52000data = " "# Creating socket objectsock = socket()# Binding socket to a address. bind() takes tuple of host and port.sock.bind((host, port))# Listening at the addresssock.listen(5)  # 5 denotes the number of clients can queuedef 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 Falsedef check_password(self):    self.isOpen('localhost', 52000)
查看完整描述

1 回答

?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

你的問題很簡單。

  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套接字通信的工作原理方面存在一些問題。請花點時間閱讀一些有關如何通過套接字進行通信的文章。如果您不需要實時通信(連續數據流,如視頻、游戲服務器等),僅需要登錄表單,請堅持使用眾所周知的協議,如 HTTP。如果您剛剛開始套接字編程,創建自己的可靠協議可能會有點復雜。

您可以使用Flask作為 HTTP 后端。


查看完整回答
反對 回復 2023-10-18
  • 1 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號