Sending Image to Server using C++

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.
You read it like any other file, send it like any other type of data and receive/write it back to a file like any other data.
If you have problems with one of these steps, you should be more specific.

Is sending byte array is same as sending characters??

Yes, although you might have to use reinterpret_cast to get a byte (=unsigned char) array to conform to interfaces that expect char arrays.
Thanks for reply

But it is not working. please can you give me any other suggestion.
You're kidding, right?
I'll quote myself:

If you have problems with one of these steps, you should be more specific.
can you send "hello world" from client to server?

if you can do that, you should be able to send just about anything from client to server.
if not, you'd better get that simple case working first.
yes i can send Hello Word. Now the problem is that when i send data from client to server it does not receive the data correctly and image is created but this image is corrupted.
then it's obviously a bug in the way you are encoding and decoding the stream

the computer cannot tell the difference between a stream of characters and a stream of bytes representing a picture - however, it is up to you to program in the consistency on both the client and the server side

please express in pseudocode, what you are doing to encode on the server-side and to decode on the client-side - in particular, how does the client know that the stream has ended?

something else you may want to try - send a very small picture and then run a cmp (binary compare) on the server vs client files to see how much of your file came through and where it got corrupted - there may be a clue there

lastly, you may want to run both the server and the client in a debugger with breakpoints at relevant places, just to monitor exactly what is happening in your loops
I'm guessing you're using a certain character to end transmission. Instead send a pre-file packet, that describes the size of the file, before sending any of the binary data. Have that trigger another function or something on the other side that will receive the actual bin data.

The basic idea is quite simple, if you need I can provide some sample code from my networking engine.

This is the Code which i am trying to use. but not working.

Client Side code
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 );
Last edited on
sir avlagrath. if your code transferring image from client to server then i m very thankful to u for providing that code. Kindly give me that code.
aviagrath's guess is 100% correct

@OP - do you understand what you are doing when you write this code on the client side?

send(hsock, buf, strlen(buf),0)

you are calculating strlen( buf ) - what does that actually mean, if buf is a buffer that contains a jpg image? do you know what strlen() does inside?

on the server-side,

if((bytecount = recv(*csock, buffer, buffer_len, 0))==SOCKET_ERROR)

is not a reliable way of terminating your recv() - the way you have it coded, you are expecting the client to send you 102400 bytes every time and relying on a SOCKET_ERROR to kick you out.
Last edited on

Then what is the Solution?????
Please help me out..
aviagrath already told you the solution - you should learn to figure out how to do it yourself (especially since your comments so far seems to indicate that you don't fully understand the code you wrote)

before sending the image, send the size of the image as a long or int
on the other side, allocate a buffer to that size and keep calling recv() until you get that full size

be sure to read the man page for recv() carefully:

in particular, pay attention to the return value:

Return Value
These calls return the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.

this means you may expect to get a total of 500 bytes, but only get a chunk of 100 - this means you have to append to your buffer and keep calling recv() until you get all your bytes (paying very close attention to how much you are requesting each time).

to make your life easier, you may try sending a textfile first so it's a little easier to tell if the transmission is correct
Thanks I got the Solution :)
Topic archived. No new replies allowed.