简体   繁体   中英

Python3 - [Errno 32] Broken Pipe while using sockets

I have the following code for the server:

import socket
import threading

def handle_client(client_socket):
    request = client_socket.recv(1024)
    print ("[*] Received: %s" % request)
    client_socket.send("ACK!".encode("utf-8"))
    client_socket.close()

bind_ip = "0.0.0.0"
bind_port = 9998
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5)
print ("[*] Listening on %s:%d" % (bind_ip, bind_port))

while True:
    client, addr = server.accept()
    print ("[*] Accepted connection from: %s:%d" % (addr[0], addr[1]))
    client_handler = threading.Thread(target = handle_client, args = (client))
    client_handler.start()

And the following on the client side:

def client_sender(buffer):
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((target, port))
    if len(buffer) > 0:
        client.send(buffer.encode("utf-8"))
        data = client.recv(4096).decode("utf-8")
        print(" - " + data)
    while True:
        buffer = input("Input:")
        buffer += "\n"
        client.send(buffer.encode("utf-8"))
        data = client.recv(4096).decode("utf-8")
        print (" - " + data)

The function client_sender is called by another function where I can choose if listen incoming connection or send data to someone. I will call it in this way:

python3 filename.py -ip 0.0.0.0 -port 9998

I can then write some data. The first time I do it I press CTRL + D to send it and the server gets and the client gets the response.But when I try to send some data from inside the "While True" loop the server never gets it and I receive this:

client.send(buffer.encode("utf-8"))
BrokenPipeError: [Errno 32] Broken pipe

How do I solve it? The only solution I found that works is to move these two lines inside the "While True" loop:

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target, port))

However it seems inefficient to me to re-connect every time.

EDIT As suggested by Sam Mason, changing the handle_client in the following way solves the problem:

def handle_client(client_socket):
    while True:
        request = client_socket.recv(1024).decode("utf-8")
        if len(request):
            print ("[*] Received: %s" % request)
            client_socket.send("ACK!".encode("utf-8"))
        else:
            client_socket.close()
            break

your server is closing the port immediately after a single recv . I'd suggest changing your handle_client code to have some sort of while loop that ends when recv returns an empty string (this indicates the client has shutdown their end of the connection, probably by close ing their connection)

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