1

I writing a simple application that reads each word from a text file and reports various statistics from about the words. One file reads through every word in the file and sends each word to the server using Python's socket library. This works fine for the first few words before lagging and halting to a stop. When I kill the both the server and the client, I receive this broken pipe error message for the client:

Traceback (most recent call last):
  File "client.py", line 27, in <module>
    read_file()
  File "client.py", line 19, in read_file
    s.sendall(message_byte)
BrokenPipeError: [Errno 32] Broken pipe

Here is the code for my client.py:

import socket
import time
host = socket.gethostname()
port = 12345                   # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
def read_file():
    with open('../DataSet/ToTheLighthouse.txt','r') as file:
        index = 0
        for line in file:
            for word in line.split():

                word_lower = word.lower()
                word_stripped = ''.join([i for i in word_lower if i.isalpha()])

                index_str = str(index)
                message_byte = word_stripped.encode('utf-8')
                s.sendall(message_byte)
                time.sleep(1)
                data = s.recv(1024)
                index+=1
        print('Received', repr(data))

read_file()
s.close()

And for the server.py:

import socket

vowel = "aeiou"

host = ''        
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))

print(f"Host:{host}\tPort:{port}")
s.listen(1)
conn, addr = s.accept()
print(f'Connected by{addr}')
while True:
    try:
        data = conn.recv(1024)

        if not data:
            print("broke")
            break

        is_boat  = lambda string: "boat" in string
        starts_with_vowel = lambda char: "char" in vowel
        data = str(data)
        data = data[1:]
        data = ''.join([i for i in data if i.isalpha()])
        print(data)
        word_len = len(data)



        print(f"Word Length:{word_len}\n")
        print(f"Has Boat: {is_boat(data)}")
        print(f"Vowel Start: {starts_with_vowel(data[0])}")
        conn.sendall(b"Server Says:hi")

    except socket.error:
        print("Error Occured.")
        break

conn.close()

I am struggling to see what is causing this problem and if anyone could offer any guidance I'd be very appreciative. Thank you.

Fiach ONeill
  • 155
  • 13
  • If the client is gone the socket does not exist anymore and you can't send to it. You have to add exception handling for this case! – Klaus D. Aug 02 '21 at 13:06
  • You haven't provided the input file that produces the problem. The code works (although it is slow) with the simple input I gave it. You should also realize that TCP is a byte streaming protocol. You should frame your messages and make sure you extract them one at a time. The 1-second sleep just provides transmission gaps, but is not a good way to solve the problem. You can wrap the server socket as a file-like object (`reader = s.makefile('r')`) and then use `reader.readline()` to extract a message, if you terminate each message in a newline. It will then process much faster. – Mark Tolonen Aug 02 '21 at 20:38

0 Answers0