How to send image data over the network?
Hello,i would like to know that how to send the image data over the network.I captured the screen into a device context and i would like to send it to the server side so that server can save it as image. I'm using c++ with mfc and my target platform is windows based pc. Thanks in advance.
Well, it's a bit hard to answer your question without knowing your background. However, you can consult MSDN for tutorials on Winsock, the Win32 sockets API. Sockets are used for communicating with other computers. Basically you need to open a socket to the other computer, which is ready to accept your incoming connections, and then you can simply send the data to that remote connection. On the other side you'll need to receive the data and there you have it.
Dubito, Cogito ergo sum.
Since you are using MFC you can create a CSocket for the network connection, the associate a CSocketFile with the CSocket and then attach a CArchive. Once you have the CArchive you can use a CBitmap to get the screen image from the device context and then serialize the CBitmap on the CArchive.
I would recommend compressing the image to something like JPEG before transmitting it -- the size difference between BMP and JPG can be 10x, and with networking, size matters. Only if you need a perfect replica of the screen should you not use something like JPEG (although PNG likely will still do better than BMP).
If you're on Windows, you can also use the built-in internet service interfaces and create a POST request that sends the image to a web service of your choice.
If you're on Windows, you can also use the built-in internet service interfaces and create a POST request that sends the image to a web service of your choice.
enum Bool { True, False, FileNotFound };
Thanks for the replies above. I tried to use the CSocketFile and CArchive method but my problem is i can't retried the CBitmap.I have something like below:
if(ar.IsStoring())
ar<<bmp;
else
ar>>bmp;
ar is a variable of CArchive and I got compile error for ar>>bmp.
error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'CBitmap' (or there is no acceptable conversion)
I would like to try to convert it to png file format before transmitting as well but how to create a CImage data from the device context of the image and make it serializeable for CArchive?
if(ar.IsStoring())
ar<<bmp;
else
ar>>bmp;
ar is a variable of CArchive and I got compile error for ar>>bmp.
error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'CBitmap' (or there is no acceptable conversion)
I would like to try to convert it to png file format before transmitting as well but how to create a CImage data from the device context of the image and make it serializeable for CArchive?
It's really unoptimized but what's stopping you from writing whatever image to disk and repeatedly doing an fstream.read(byte) followed by a socket.send(byte)? Once you can get the Bitmap object into a byte array, sending it's a piece of cake in or out of MFC. CArchive has really been more trouble than it was worth IMO, so I don't know why MSDN recommends it.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cbitmap.3a3a.getbitmapbits.asp
That's a link to MSDN's entry on CBitmap. You have to give it a number of bytes to copy (which the last line or so says how to get) and a pointer to a byte array. I'd second trying to find a way to compress it to something other than a raw bitmap, and that's elementary to do in C#, but you're on your own in C++ though you do have over 20 years of libraries out there.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cbitmap.3a3a.getbitmapbits.asp
That's a link to MSDN's entry on CBitmap. You have to give it a number of bytes to copy (which the last line or so says how to get) and a pointer to a byte array. I'd second trying to find a way to compress it to something other than a raw bitmap, and that's elementary to do in C#, but you're on your own in C++ though you do have over 20 years of libraries out there.
Quote:
Original post by RKillian
It's really unoptimized but what's stopping you from writing whatever image to disk and repeatedly doing an fstream.read(byte) followed by a socket.send(byte)? Once you can get the Bitmap object into a byte array, sending it's a piece of cake in or out of MFC. CArchive has really been more trouble than it was worth IMO, so I don't know why MSDN recommends it.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cbitmap.3a3a.getbitmapbits.asp
That's a link to MSDN's entry on CBitmap. You have to give it a number of bytes to copy (which the last line or so says how to get) and a pointer to a byte array. I'd second trying to find a way to compress it to something other than a raw bitmap, and that's elementary to do in C#, but you're on your own in C++ though you do have over 20 years of libraries out there.
I don't want to save it into the disk first because I scare the player might hack the image before it sends to server.
A user can hack it while it's still sitting in memory.... remember, ANYTHING the client touches is hackable - you can NEVER trust the client.
FTA, my 2D futuristic action MMORPG
Use GetObject to get the necessary information about the bitmap. Send this information using the socket stream to the server. The server can then reconstruct the bitmap and do whatever it needs.
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement