0

In the code below I've tried to send an image using the python socket module in one machine to another machine. So I have 2 files: client.py and Server.py

as I figured it out the problem is when I read the image(as bytes) at the client machine and then the server tries to receive the file, at that moment when sending process is done before the receiving process then the error below occurs at line 13 of the client code:

BrokenPipeError: [Errno 32] Broken pipe

I want to find out what this error is and why does it occur in my code.

Server.py

import socket

host = '192.168.1.35'
port = 5000

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

while True:
    conn , addr = s.accept()
    data = conn.recv(1024)

    with open(r"C:\Users\master\Desktop\music.jpg",'wb') as f:
        f.write(data)
        # conn.send(b'done')
        data = conn.recv(1024)
        if not data:
            break
            conn.send(b'done')
    conn.send(b'done')
    conn.close()
s.close()

Client.py

import socket

def main():

    HOST = '192.168.1.35'  
    PORT = 5000

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    f = open('/home/taha/Desktop/f.jpg','rb')
    data = f.read()
    s.sendfile(f)
    if s.recv(1024) == b'done':
        f.close()

    s.close()

if __name__ == '__main__':
    main()

1 Answer 1

2

You are closing the server connection before the client read the “done”

Sign up to request clarification or add additional context in comments.

2 Comments

so what should I do to prevent it??
Wait with sleep or let the client send you confirmation that he got your done and then close the connection (but then the client will have to wait)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.