本文介绍了套接字编程 - 发送和接收图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我在正确的地方。
如果我不在我的问题的正确部分,我道歉。

I hope that I am in the right place.I apologize if I am not in the right section for my question.

我试图将图像从客户端写在python的服务器用c ++写的。
当我执行它们两者时,服务器端似乎连接,但客户端给我这个错误(IndentationError:unindent不匹配任何外部缩进级别)

I am trying to send images from client written in python to server written in c++.When I execute both of them, the server side seems connected but client side gave me this error (IndentationError: unindent does not match any outer indentation level)

我要在服务器端发布:

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

#define SERVER_PORT htons(50007)

int main() {

    char buffer[1024];
    int n;

    int serverSock=socket(AF_INET, SOCK_STREAM, 0);

    sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = SERVER_PORT;
    serverAddr.sin_addr.s_addr = INADDR_ANY;

    /* bind (this socket, local address, address length)
       bind server socket (serverSock) to server address (serverAddr).
       Necessary so that server can use a specific port */
    bind(serverSock, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr));


    // wait for a client
    /* listen (this socket, request queue length) */
    listen(serverSock,1);

    sockaddr_in clientAddr;
    socklen_t sin_size=sizeof(struct sockaddr_in);

    int clientSock=accept(serverSock,(struct sockaddr*)&clientAddr, &sin_size);

    while (1 == 1) {
            bzero(buffer, 1024);

        //char *buff = (char*)malloc(sizeof(char) * (240*360));
        FILE *output;
        output = fopen("test.jpg", "wb");
        unsigned int readBytes = 0;
        while(true)
        {
            int ret = recv(clientSock, buffer+readBytes, (240*360)-readBytes, 0);
            if (ret <= 0)
            {
                break;
            }
                readBytes += ret;
        }
        fwrite(buffer, sizeof(char), readBytes, output);
        fclose( output );

        //Mat img_2 = imread( "test.jpg");

            //receive a message from a client
            n = read(clientSock, buffer, 500);
            cout << "Confirmation code  " << n << endl;
            cout << "Server received:  " << buffer << endl;

            strcpy(buffer, "test");
            n = write(clientSock, buffer, strlen(buffer));
            cout << "Confirmation code  " << n << endl;
    }
    return 0;
    close(serverSock);
}

这是客户端:

我希望有人帮助我。

import socket
import sys,os
import cv2
import numpy as np
import time

HOST, PORT = "localhost", 50007

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

client_socket.connect((HOST, PORT))


fpath ='/Users/salemalqahtani/Desktop/NetworkingPart/sss.png'
pic = open(fpath, 'rb')
chunk = pic.read(1024)
client_socket.send(str.encode("STORE " + filePath))
t = time()
while chunk:
    print("Sending Picture")
   client_socket.send(chunk)
   chunk = pic.read(1024)
pic.close()
print("Done sending")
print("Elapsed time = " + str(time() - t) + 's')
client_socket.close()
return "Done sending"

编辑:
图像正从客户端发送到服务器

The image is sending from client side to server side and server will reply a message received....

推荐答案

这可能是一个简单的缩进错误吗?打印之前看起来有任何额外的空间

Could it be a simple indentation error? It looks like there is any extra space before print

while chunk:
   print("Sending Picture")
   client_socket.send(chunk)
   chunk = pic.read(1024)

这篇关于套接字编程 - 发送和接收图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:37