简体   繁体   中英

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. 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. The Server Client working fine but integration of GUI with server throw below error on server side.

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:

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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM