繁体   English   中英

如何使用 python 客户端和 C 服务器通过套接字正确发送图像

[英]How to correctly send image through socket using python client and C server

我正在编写类似聊天的应用程序,其中用户除了发送消息之外还可以发送图片。 唯一的问题是我不确定我应该如何处理图像发送。 主要思想是用户在控制台中选择图像写入/send image.jpg ,然后通过socket.send(image.jpg)发送图像。 发送图像对我来说很好,但是我的 C 服务器由于缓冲区而从单独的“包”中从客户端获取数据输入,但是当我添加它们时,它们与我的图像大小不匹配(例如我的图像是 73kb 但服务器从两个单独的“包”20kb + 8.5kb 中获取 29kb)。 第二个问题是如何进一步发送这些数据,我认为也许有某种方法可以通过实际图像二进制数据传递缓冲区并将其发送到 Python 中我的客户的 rest。 我的缓冲区是 20480 字节。

从 client.py 发送:

elif "/send " in buffer:
            fileArr = buffer.split(' ') #filearr = ["/send", "image.jpg"]
            file = open(fileArr[1], "rb").read()
            s.send(fileArr[0].encode()) #metadata that im sending image file
            time.sleep(0.2)
            s.send(file) #sending my file
            time.sleep(1)
            print("Image sent!")

我的 server.c 接收和发送:

void send_message(char *s, int uid){
  pthread_mutex_lock(&clients_mutex);
  for (size_t i = 0; i < MAX_CLIENTS; i++) {
    if(clients[i]){
      if(clients[i]-> uid != uid){
        if(write(clients[i]->sockfd, s, strlen(s)) < 0){
          perror("");
          printf("Write to descriptor failed \n");
          break;
        }
        }
      }
    }
  pthread_mutex_unlock(&clients_mutex);
}


if(strstr(buffer, "/send") != NULL || receive > 128){
        if(strstr(buffer, "/send")){
          buffer[0] = '\0';
        } 
        recv(cli->sockfd, buffer, sizeof(buffer), 0); // Receiving my image and saving it to buffer
        printf("receiveing img");
        send_message(buffer, cli->uid); // Sending my buffer further to client
        printf("sending image\n" );

而我的 client.py 接收:

def recvDataLoop():
    while True:
        reply = s.recv(buffer_sz)
        if reply:
            print("Got reply");
        flag = 0
        try:
            reply = reply.decode()
            if flag != 1 and "joined the chat" in reply or "has left" in reply:
                print(reply)
            elif flag != 1 and "|" in reply:
                replyArr = reply.split("|")
                time.sleep(0.1)
                print("{}: {}".format(replyArr[0],replyArr[1]))
        except:
            print("Encoded") #If data received and it cannot be decoded, its <bytes>
            try:
                f = open("cat4.jpg", "ab").write(reply) #If image exists, write data to existing image
            except:
                f = open("cat4.jpg", "wb").write(reply)

我需要制作这个 try-except 块,因为当接收到数据时,它是在包中接收的,所以当我得到它时,我需要将它写入 cat4.jpg 文件的末尾。

套接字 rx/tx 函数通常返回实际传输的字节数。 因此,执行 rx/tx 的正确方法是在循环中调用 function,同时累积返回值并移动缓冲区起始指针。 此外,在处理二进制数据时 - 例如图像数据 - null 终止假设是错误的,并且会截断错误长度的数据; 它还可能触发数组索引越界相关异常。 您需要明确指定 rx/tx 字节数( strlen是错误的)。

暂无
暂无

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

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