我希望我在正确的地方。 如果我不在我的问题的正确部分,我道歉。
我正在尝试将用python编写的客户端的图像发送到用c ++编写的服务器。 当我执行它们时,服务器端似乎已连接但客户端方面给了我这个错误(IndentationError:unindent与任何外部缩进级别不匹配)
我将发布服务器端:
#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"
编辑: 图像从客户端发送到服务器端,服务器将回复收到的消息....
答案 0 :(得分:2)
这可能是一个简单的缩进错误吗?在打印之前看起来有任何额外的空间
while chunk:
print("Sending Picture")
client_socket.send(chunk)
chunk = pic.read(1024)