I'd like to ask you a simple question about Python socket programming (about 50 lines of code including server cla). )

Asked 6 days ago, Updated 6 days ago, 1 views

Server

import socket

HOST = ""
PORT = 8081
def receive(socket):
    data = socket.recv(1024)
    with open("D:/caps/test.jpg", 'wb') as f:
        try:
            while data:
                f.write(data)
                data = socket.recv(1024)
        except Exception as e:
            print(e)



server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen(10)

while True:
    client_socket, addr = server_socket.accept()
    receive(client_socket)

    socket.send ("Received".encode())

Client

import socket

HOST = '127.0.0.1'
PORT = 8081


def send():
    data_transferred = 0
    print ("Start File Transfer")
    with open("E:/caps/test.jpg", 'rb') as f:
        try:
            data = f.read(1024)
            while data:
                data_transferred = client_socket.send(data)
                data = f.read(1024)
                if data_transferred == 0:
                    break
        except Exception as e:
            print(e)


def receive():
    message = client_socket.recv(1024)
    print(message.decode())



client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))

send()
receive()

client_socket.close()


A simple code that sends an image called test.jpg from the client, receives the image from the server, and sends a message back to the client that it has received.

The problem is that the client sends data normally, but it stops while receiving data from the server.

I couldn't find the cause and solution alone because I didn't learn much about socket programming. I ask for your help.

python socket

2022-09-20 15:36

1 Answers

recv() does not end when transferring files and can continue to wait (no, wait)

The solution is

There is.

The code of this question is to send a received message from SERVER to CLIENT after the file transmission and reception are completed (because the socket connection should not be closed), so solution number 3 has been applied.

There may be other options, but this is the best I can do ^ For socket.shutdown(socket.SHUT_WR), see stackoverflow.

^

SERVER SIDE

import socket

HOST = ""
PORT = 8081

def receive(receive_socket):
    with open("D:/caps/test.jpg", 'wb') as f:
        try:
            while True:
                data = receive_socket.recv(1024)
                if data:
                    f.write(data)
                else:
                    break
        except Exception as e:
            print(e)
        finally:
            f.close()


if __name__ == '__main__':
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((HOST, PORT))
    server_socket.listen(10)

    while True:
        client_socket, addr = server_socket.accept()
        receive(client_socket)

        # socket.send ("Received".code()) : typo? Haha
        client_socket.send ("Received".encode())

CLIENT SIDE

import socket

HOST = '127.0.0.1'
PORT = 8081


def send():
    with open("E:/caps/test.jpg", 'rb') as f:
        try:
            while True:
                data = f.read(1024)
                if data:
                    client_socket.send(data)
                else:
                    break
            # ------------------------------------ #
            client_socket.shutdown(socket.SHUT_WR)
            # ------------------------------------ #
        except Exception as e:
            print(e)


def receive():
    message = client_socket.recv(1024)
    print(message.decode())


if __name__ == '__main__':
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((HOST, PORT))
    send()
    receive()


2022-09-20 15:36

If you have any answers or tips


© 2022 pinfo. All rights reserved.