Hello everyone I need to convert std::vector<uchar> to const char* or send std::vector<uchar> over socket using winsock2.h in windows system.
How can I do this?
I'm compress one image:
std::vector<uchar> buff;//buffer for coding
std::vector<int> param(2);
param[0] = cv::IMWRITE_JPEG_QUALITY;
param[1] = 40;//default(95) 0-100
cv::imencode(".jpeg", frame, buff, param);
Then:
I need to send std::vector<uchar> buff over socket but the the send() just accept const char*.
I have two programs, client and server and I want to send an image through a socket using a JPG buffer to be received by the server and converted back into OpenCV mat.
I already did it that way, but it didn't work:
int bytes = 0;
if (!imgMat.empty())
{
if ((bytes = send(server, (const char*)buff.data() , buff.size(), 0)) < 0) //if ((bytes = send(server, (char*)imgMat.data, imageSize, 0)) < 0)
{
::cout << "Error while sending..";
break;
}
::cout << "Frame sent sucessfuly" << endl;
::cout << bytes << " bytes sent." << endl;
}
else
{
::cout << "Frame was empty" << endl;
break;
}
The purpose is to improve sending speed with an image that takes up less space.
In a comment, you say that you are passing
sizeof(buff.data())torecv(). This is wrong, you are specifying the size of the pointer itself, not the size of the data that it is pointing at. You need to usebuff.size()instead.As such, you need to pre-size
buffbefore you can use it withrecv(). Which means you need to know how many bytes to expect from the sender. So the sender should send its buffer size along with its actual buffer data.Try something more like this:
sender:
Receiver:
Alternatively, if your code runs on a platform that doesn't support
MSG_WAITALL: