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??
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.
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:
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