Hello,
I have configured my raspberry pi as a server and my Windows pc as a client.
The raspberry pi sends an image to the pc when connection is established.
on the PC side, i am using Visual studio. I have included the dependencies of openCV2
i used the following command: image = fopen_s(fn, "wb"); but it doesnt work.
I am receving the data from the pi,meaning that the image is being recevied. But i need a mode to display the same image on my windows pc.
the code below is the image function of my client side. I have simply called the above function when the connection (bind, listen, accept) is established in my main program
What I have tried:
#pragma once
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <iostream>
#include <fstream>
#include <errno.h>
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <conio.h>
#include
#include
using namespace cv;
using namespace std;
int receive_image(int socket)
{
int imgc = 0 ;
while(imgc < 2000)
{
int buffersize = 0, recv_size = 0,size = 0, read_size, write_size, packet_index =1;
char imagearray[10240],verify = '1';
FILE *image=0;
int value = 0 ;
value = value + 1;
char wzRec[10240];
int nLeft= 512, nLefts = 512;
int iPos= 0, iPos2 = 0;
int nData = 0;
do
{
nData = recv( socket, &wzRec[iPos], nLeft, 0 );
if( nData == SOCKET_ERROR )
{
cout << "Error receiving data" << endl;
break;
}
}while( nLeft > 0);
printf("size = %d" , size) ;
if(size>1000)
{
imgc = imgc + 1 ;
char buffer[] = "Got it";
do
{
nData = send( socket, &buffer[iPos2], nLeft, 0 );
if( nData == SOCKET_ERROR )
{
cout<< "Error sending data" << endl;
break;
}
nLefts -= nData;
iPos2 += nData;
}while( nLefts > 0);
printf("Reply sent\n");
printf(" \n");
if( image == NULL)
{
printf("Error has occurred. Image file could not be opened\n");
return -1;
}
int need_exit = 0;
struct timeval timeout = {10,0};
fd_set fds;
int buffer_fd, buffer_out;
while(recv_size < size)
{
FD_ZERO(&fds);
FD_SET(socket,&fds);
buffer_fd = select(FD_SETSIZE,&fds,NULL,NULL,&timeout);
if (buffer_fd < 0)
printf("error: bad file descriptor set.\n");
if (buffer_fd == 0)
printf("error: buffer read timeout expired.\n");
if (buffer_fd > 0)
{
do
{
read_size = recv(socket,imagearray, 10240,0);
}while(read_size <0);
printf("Packet number received: %i\n",packet_index);
printf("Packet size: %i\n",read_size);
write_size = fwrite(imagearray,1,read_size, image);
printf("Written image size: %i\n",write_size);
if(read_size !=write_size)
{
printf("error in read write\n");
}
recv_size += read_size;
packet_index++;
printf("Total received image size: %i\n",recv_size);
printf(" \n");
printf(" \n");
}
}
fclose(image);
printf("Image successfully Received!\n");
}
}
return 1;
}
int main(int argc, char* argv[])
{
WSADATA wsaData = {0};
WORD wVer = MAKEWORD(2,2);
int nRet = WSAStartup( wVer, &wsaData );
if( nRet == SOCKET_ERROR )
{
printf("Failed to init Winsock library");
return -1;
}
cout << "Opening connection to server" << endl;
WORD WSAEvent = 0;
WORD WSAErr = 0;
SOCKET hServer = {0};
hServer = socket( AF_INET, SOCK_STREAM, IPPROTO_IP );
if( hServer == INVALID_SOCKET )
{
cout << "Invalid socket, failed to create socket" << endl;
return -1;
}
sockaddr_in saServer = {0};
saServer.sin_family = PF_INET;
saServer.sin_port = htons( 5001 );
saServer.sin_addr.s_addr = inet_addr( "172.16.26.100" );
nRet = connect( hServer, (sockaddr*)&saServer, sizeof( sockaddr ) );
if( nRet == SOCKET_ERROR )
{
cout << "Connection to server failed" << endl;
closesocket( hServer );
return -1;
}
cout << "Connected to server" << endl;
cout << "Sending data to server" << endl;
receive_image(hServer);
cout << "Closing connection" << endl;
nRet = shutdown( hServer, SD_BOTH );
if( nRet == SOCKET_ERROR )
{
cout << "Error trying to perform shutdown on socket" << endl;
return -1;
}
nRet = closesocket( hServer );
hServer = 0;
if( nRet == SOCKET_ERROR )
{
cout << "Error failed to close socket" << endl;
}
nRet = WSACleanup();
if( nRet == SOCKET_ERROR )
{
cout << "Error cleaning up Winsock Library" << endl;
return -1;
}
cout << "Data sent successfully" << endl;
getchar();
return 0;
}