May 22, 2011 at 5:09am UTC
hi friends..
I want to sent an image from client to server in c++ on windows plate form.
can any one tell me how to read an image and convert it into byte array? And on server side how to create the image again from the received data?
Is sending byte array is same as sending characters??
I m very thankful for your kind help.
May 23, 2011 at 1:54pm UTC
Use sockets to do the sending and receiving. Open your files in binary mode.
May 23, 2011 at 6:00pm UTC
I do that but not working. Actually i save the image data in buffer but when i send it to server it does not do any thing. image file is created but "no preview is available" error is shown.
May 23, 2011 at 8:36pm UTC
What error are you seeing? Run cksum on the file before you transmit it and again when you receive it. Are they the same?
May 24, 2011 at 3:17am UTC
[b]No. when i receive the data on server side it is not same as i send from client...
This is the Code which i am trying to use. but not working.
Client Side code[/b]
long len;
char* buf = NULL;
FILE* fp = NULL;
fp = fopen( "image.jpg", "rb" );
if (fseek( fp, 0, SEEK_END ) != 0)
{
fclose( fp );
}
len = ftell( fp );
rewind( fp );
buf = (char*)malloc( len );
if (!buf)
{
fclose( fp );
}
if (!fread(buf,len,1,fp))
{
fclose( fp );
}
fclose( fp );
//******************
if( (bytecount=send(hsock, buf, strlen(buf),0))==SOCKET_ERROR){
fprintf(stderr, "Error sending data %d\n", WSAGetLastError());
goto FINISH;
}
printf("Sent bytes %d\n", len);
Sever Side Code:
int *csock = (int*)lp;
char buffer[102400];
long buffer_len = 102400;
long bytecount;
memset(buffer, 0, buffer_len);
if((bytecount = recv(*csock, buffer, buffer_len, 0))==SOCKET_ERROR){
fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
goto FINISH;
}
printf("Receive bytes %d\n", buffer_len);
//******************
FILE* fp = NULL;
fp = fopen( "image.jpg", "wb" );
if (!fwrite (buffer, 1, buffer_len, fp))
{
fclose( fp );
}
fclose( fp );
//**********
May 24, 2011 at 4:42am UTC
Thanks Bros. I got the solution. Thanks alot for your kind help