0

I'm currently using python to write a simple socket program. However, I have noticed that I'm getting this error sometimes:

Traceback (most recent call last):
  File "/Users/user/Desktop/socketpython/client.py", line 41, in <module>
    s.send(str(f"{choice}").encode('utf8'))
BrokenPipeError: [Errno 32] Broken pipe

Here is my client code:

#client

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 9999))

while True:
  choice = input("Select option: ")

     if choice == str(7):
        s.send(str(f"{choice}").encode('utf8'))
        report = s.recv(2048)
        message = report.decode('utf8')
        print("** Python DB contents **\n" + message)

And here is my corresponding server code:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 9999))

s.listen()

while True:
    conn, addr = s.accept()
    with conn:
        print(f"Connection established from address {addr}")
        request = conn.recv(2048).decode("utf-8")
        serverdata = request.split(";")
        choice = serverdata[0]

          if choice == str(7):
            sortedList = ""
            data.sort()
            print(data)
            for i in range(len(data)):
                sortedList += data[i][0] + "|" + data[i][1] + "|" + data[i][2] + "|" + data[i][3] + "\n"
            conn.send(sortedList.encode('utf-8'))

If anyone has any idea on why this problem is happening and what code modifications are necessary to resolve this issue, I would greatly appreciate it.

giotto1
  • 49
  • 5
  • The source code do not match with the stack trace and they seem to be scrapped. You should provide a [mre] and debugging details including the test scenario, outputs(logs), inputs, etc. Mostly you will find the cause by yourself while you prepare these. Anyway, for the broken pipe error, see [this question](https://stackoverflow.com/questions/34448088/socket-send-function-returned-broken-pipe-error). – relent95 Nov 19 '22 at 01:01

0 Answers0