hello,
i have designed a code for a tcp ip communication between raspberry pi and windows. the pi acts as a server and the pc acts as a client.
the server listens on the port for any incoming requests form the client side. as soon as it receives a request, it sends an image to the client.
the issue i have is,
the image on the server side is 56.5 kb
on the client i receive the image but it does not open. i get a "decode error". also the size of the image that the client recevies is 15kb.
What I have tried:
#include <stdio.h>
#include <string.h>
#include
#include
#include <unistd.h>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <errno.h>
using namespace std;
int send_image(int socket)
{
FILE *picture;
int size, read_size, stat, packet_index;
char send_buffer[10240], read_buffer[256];
packet_index = 1;
picture = fopen("a.jpg", "r");
printf("\nGetting Picture Size\n");
if(picture == NULL)
{
printf("Error Opening Image File");
}
fseek(picture, 0, SEEK_END);
size = ftell(picture);
fseek(picture, 0, SEEK_SET);
printf("Total Picture size: %i\n",size);
write(socket, (void *)&size, sizeof(int));
printf("Sending Picture with Size %d\n", size);
printf("Sending Picture as Byte Array\n");
do {
stat=read(socket, &read_buffer , 255);
printf("Bytes read: %i\n",stat);
} while (stat < 0);
printf("Received data in socket\n");
printf("Socket data: %s\n", read_buffer);
while(!feof(picture)) {
read_size = fread(send_buffer, 1, sizeof(send_buffer)-1, picture);
do{
stat = write(socket, send_buffer, read_size);
}while (stat < 0);
printf("Packet Number: %i\n",packet_index);
printf("Packet Size Sent: %i\n",read_size);
printf(" \n");
printf(" \n");
packet_index++;
bzero(send_buffer, sizeof(send_buffer));
}
return 0;
}
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int value=0;
if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
else
printf("socket created with %d %d",AF_INET, SOCK_STREAM);
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
send_image(newsockfd);
bzero(buffer,256);
close(newsockfd);
close(sockfd);
return 0;
}
client side
#define _CRT_SECURE_NO_DEPRECATE
#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
#include
using namespace std;
int receive_image(int socket)
{
int buffersize = 0, recv_size = 0, read_size, write_size, packet_index =1,stat;
char size[10241];
char imagearray[10241],verify = '1';
FILE *image;
do
{
stat = recv(socket, *&size, sizeof(int),0);
}while(stat<0);
cout<<"Packet received.\n";
cout<<"Image size: "<<sizeof(size)<<endl;
char buffer[] = "Got it";
do
{
stat = send(socket, buffer, sizeof(int),0);
}while(stat<0);
cout<<"Reply sent"<<endl<<endl;
image = fopen("a.jpeg", "w");
if( image == NULL)
{
cout<<"Error has occurred. Image file could not be opened"<<endl;
return -1;
}
int need_exit = 0;
struct timeval timeout = {10,0};
fd_set fds;
int buffer_fd, buffer_out;
while( recv_size < sizeof(size))
{
FD_ZERO(&fds);
FD_SET(socket,&fds);
buffer_fd = select(FD_SETSIZE,&fds,NULL,NULL,&timeout);
if (buffer_fd < 0)
cout<<"error: bad file descriptor set"<<endl;
if (buffer_fd == 0)
cout<<"error: buffer read timeout expired."<<endl;
if (buffer_fd > 0)
{
do
{
read_size = recv(socket,imagearray, 10241,0);
}while(read_size <0);
cout<<"Packet number received: "<<packet_index<<endl;
cout<<"Packet size: "<<read_size<<endl;
write_size = fwrite(imagearray,1,read_size, image);
cout<<"Written image size: "<<write_size<<endl;
if(read_size !=write_size)
{
cout<<"error in read write"<<endl;
}
recv_size += read_size;
packet_index++;
cout<<"Total received image size: "<<recv_size<<endl<<endl<<endl;
}
}
fclose(image);
cout<<"Image successfully Received!"<<endl;
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 )
{
cout<<"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;
}