简体   繁体   English

Python 套接字将多次发送的数据放入一个接收缓冲区

[英]Python socket putting data of multiple send into one receive buffer

I am trying to make a simple image-sharing app in Python such that images can be shared from clients to a server.我正在尝试在 Python 中制作一个简单的图像共享应用程序,以便可以将图像从客户端共享到服务器。 I am doing that using socket and sending images in form of numpy arrays.我正在使用套接字并以 numpy arrays 的形式发送图像。 My problem is that when the app loads, I have made such that 6 images or less(if less than 6 present for an account) are sent from server to the client.我的问题是,当应用程序加载时,我已经将 6 张或更少的图像(如果帐户少于 6 张)从服务器发送到客户端。 Each image is stored in the server with a unique name identifying it.每个图像都存储在服务器中,并具有唯一的名称来标识它。 This name is of variable length and is decided by the user and is sent before the image array is sent.该名称长度可变,由用户决定,在图像数组发送之前发送。 Also, the shape of the image is sent to reproduce it from the bytes at the client.此外,图像的形状被发送以从客户端的字节中复制它。 But as the name length is not fixed, I am reading 10 buffer size.但由于名称长度不固定,我正在读取 10 个缓冲区大小。 But if the name is smaller, it is reading the shape of the are that I am sending later as well.但如果名称更小,它也会读取我稍后发送的区域的形状。 How do I fix this?我该如何解决? Here is the sending image code:这是发送图像代码:

def send_multiple_imgs(send_socket, imgs):
    num = len(imgs)
    send_socket.send(str(num).encode())
    for img in imgs:
        print(img)
        send_socket.send(img.encode())
        send_img(send_socket,imgs[img])

part of send_img function: send_img function 的一部分:

def send_img(send_socket,img):
    send_socket.send(str(img.shape).encode())
    .....

The later part is not important, I think.我认为后面的部分并不重要。 Here is the receiving part:这是接收部分:

def receive_multiple_img(recv_socket):
    num = int(recv_socket.recv(1).decode())
    imgs = {}
    for i in range(num):
        img_name = recv_socket.recv(10).decode()
        print(img_name)
        imgs[img_name] = recieve_image(recv_socket)

    return imgs

What is happening is, I have an image named 'ds' of shape (200,200,4), but the img_name reads: 'ds(200, 20' and then it messes up the further sending and receiving as well. How do I fix this? I am using TCP protocol:发生的事情是,我有一个名为“ds”的图像,形状为 (200,200,4),但 img_name 显示为:“ds(200, 20”,然后它也搞乱了进一步的发送和接收。我该如何解决这个?我使用的是 TCP 协议:

s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)

I am new to networking in python.我是 python 网络的新手。 So, please consider if this is a silly question所以,请考虑一下这是否是一个愚蠢的问题

TCP, unlike UDP, is a stream protocol . TCP,不同于UDP,是一个stream协议 That means that a single send() doesn't correspond to a single read() .这意味着单个send()不对应于单个read() A call to send() simply places the data to be sent into the TCP send buffer, and a call to read() simply returns the bytes from the TCP receive buffer.send()的调用只是将要发送的数据放入 TCP 发送缓冲区,对read()的调用只是从 TCP 接收缓冲区返回字节。 The bytes in the buffer could have come from a single send() on the other side or a hundered.缓冲区中的字节可能来自另一侧的单个send()或数百个。

This is a fairy common misunderstanding that leads to many bugs.这是一个导致许多错误的童话常见误解。 Here's me explaining this again , and again , and again , and again .这是我一次又一次地解释这个,一次又一次一次又一次

If you want to send() several separate messages or pieces of data, the reader must be able to tell the messages apart.如果你想send()几个单独的消息或数据片段,读者必须能够区分这些消息。 There are many ways to do that, such as using fixed-length messages, prefixing the message's length before each message, or using a delimiter.有很多方法可以做到这一点,例如使用固定长度的消息、在每条消息之前添加消息长度的前缀或使用分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM