Python Forum
BrokenPipeError: [Errno 32] Broken pipe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BrokenPipeError: [Errno 32] Broken pipe
#1
I have a client-server system set up on two Raspberry Pi's on the same network. When I run the echo program below (client sends the server a message, server sends the message back, client prints received message) I get no errors and everything works great. When I run the "GPS" program below (just sends a message to client) the client recieves and prints the messaage no problem but the server gives me the error
BrokenPipeError: [Errno 32] Broken pipe
and exits the program. The only thing I can think of causing this would be the buffer size on the client side message-receiving code since everything else is pretty much the same but I'm not sure how to approach this. Anyone have any solutions?

Echo Server:
# Echo server program
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('ip address', 50007)
print ('starting up on %s port %s' % server_address)
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print('waiting for a connection')
    connection, client_address = sock.accept()

    try:
        print ('connection from', client_address)

        # Receive the data in small chunks and retransmit it
        while True:
            chunk = 1024
            data = connection.recv(chunk)
            dec_data = data.decode('utf-8')
            print('received "%s"' % dec_data)
            if dec_data:
                print('sending data back to the client')
                connection.sendall(data)
            else:
                print('no more data from', client_address)
                break

    finally:
        # Clean up the connection
        connection.close()
Echo Client:
# Echo client program
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('ip address', 50007)
print('connecting to %s port %s' % server_address)
sock.connect(server_address)

try:
    # Send data
    message = ('This is the message.  It will be repeated.')
    enc_mes = message.encode('utf-8')
    print('sending "%s"' % message)
    sock.sendall(enc_mes)

    # Look for the response
    amount_received = 0
    amount_expected = len(message)

    while amount_received < amount_expected:
        chunk = 1024
        data = sock.recv(chunk)
        amount_received += len(data)
        dec_data = data.decode('utf-8')
        print('received "%s"' % dec_data)

finally:
    print('closing socket')
    sock.close()
GPS Server:
# GPS server program
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('ip address', 50007)
print ('starting up on %s port %s' % server_address)
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print('waiting for a connection')
    connection, client_address = sock.accept()

    try:
        print ('connection from', client_address)

        # Message transmission
        while True:
            # Send GPS data
            gps_data = ('Pretend GPS Data').encode('utf-8')
            connection.sendall(gps_data)

    finally:
        # Clean up the connection
        connection.close()
GPS Client:
# GPS client program
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('ip address', 50007)
print('connecting to %s port %s' % server_address)
sock.connect(server_address)

try:
    # Look for the response
    bufsize = 2048
    data = sock.recv(bufsize)
    dec_data = data.decode('utf-8')
    print('received "%s"' % dec_data)

finally:
    print('closing socket')
    sock.close()
Reply
#2
(Nov-15-2018, 09:22 PM)Pflaumboy Wrote:
        while True:
            # Send GPS data
            gps_data = ('Pretend GPS Data').encode('utf-8')
            connection.sendall(gps_data)
I don't know a lot about sockets, but sending an infinite amount of data sounds like something that should be throwing errors.
Reply
#3
maybe giving a time delay will fix the issue
Reply
#4
this happens when the receiving end of the pipe closes the pipe then the sending end writes a buffer to the pipe. handle or ignore that exception. i usually just ignore it (pass). this can happen on stdout or stderr if the scripts are run in a pipeline.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  All pipe instances are busy GrahamL 0 3,006 Jan-07-2021, 12:05 PM
Last Post: GrahamL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020