TLDR; I try to send data from C++ program to Python program and reversed, i managed to send data from C++ and received on python, but not send from python to C++
Okay before going into code, i will explain what iam trying to do (Hope it will make understanding my code easier): I wanted to create a server which will listen and read an image from C++ Client and return another image
(Server Side-Python) First i created a socket and bind it with localhost at port 5001
(Client Side-C++) I created a socket, read an image (Here i use OpenCV) send image size first then send the image itself, and wait for server to respond
(Server Side-Python) Read the size and read the image using the size it received
-- At this point everything work as expected --
(Server Side-Python) Server read and image and send it back
(Client Side-C++) Client is freeze since it dont receive anything?
It took me 4 hours with no result :( this is my code:
Server Python
import socket
import cv2
import numpy
import time
UDP_IP = '127.0.0.1'
UDP_PORT = 5001
#Create socet here
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((UDP_IP, UDP_PORT))
#Get image size here
length, addr = s.recvfrom(16)
#read image data here
stringData,addr = s.recvfrom(int(length))
#Convert to opencv image format
data = numpy.fromstring(stringData, dtype='uint8')
decimg=cv2.imdecode(data,0)
############################################################################
#Read an image
sendBackImg = cv2.imread("cat.jpeg",0)
#Encode file
encode_param=[int(cv2.IMWRITE_JPEG_QUALITY),90]
result, imgencode = cv2.imencode('.jpg', sendBackImg, encode_param)
data = numpy.array(imgencode)
stringData = data.tostring()
#Send image size
s.sendto(str(len(stringData)).zfill(16), (UDP_IP, UDP_PORT))
# s.sendto(stringData, (UDP_IP, UDP_PORT))
#Show image to check if it work correctly
cv2.imshow('SERVER-SEND',sendBackImg)
cv2.imshow('SERVER-SEND',decimg)
cv2.waitKey(3000)
s.close()
cv2.destroyAllWindows()
Client C++
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define PORT 5001
using namespace std;
using namespace cv;
int main(void)
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
// read an image and show it so later and can compare with result on server side
Mat frame = imread("dog.jpg",0);
imshow("CLIENT",frame);
// Encode image
vector<uchar> buf;
imencode(".jpg",frame,buf);
const char *data = reinterpret_cast<char*>(buf.data());
int strLength = buf.size();
std::string strSizeTmp = std::to_string(strLength);
strSizeTmp = string(16-strSizeTmp.length(),'0')+strSizeTmp ;
char strSize[strSizeTmp.length()+1];
strcpy(strSize, strSizeTmp.c_str());
// Create socket
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
//Send image size and image data
sendto(sock, (const char *)strSize, 16,
MSG_CONFIRM, (const struct sockaddr *) &serv_addr,
sizeof(serv_addr));
sendto(sock, (const char *)data, strLength,
MSG_CONFIRM, (const struct sockaddr *) &serv_addr,
sizeof(serv_addr));
//////////////////////////////////////////////////////////
//Wait for respond
char bufferTmp[16] ;
cout << "Wait" << endl;
recvfrom(sock, (char *)bufferTmp, 16,
MSG_WAITALL, (struct sockaddr *) NULL,
NULL);
// The program just nerver reach here
cout << bufferTmp << endl;
close(sock);
waitKey(3000);
}
There is one thing i noticed that c++ recvfrom doesnt required ip and port, how does it know which port to listen to?
Aucun commentaire:
Enregistrer un commentaire