Nov-15-2018, 09:22 PM
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
Echo Server:
BrokenPipeError: [Errno 32] Broken pipeand 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()