Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [AGK] How to send an image over a socket

Author
Message
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th Nov 2023 16:40 Edited at: 20th Nov 2023 19:10
This builds upon my other socket example. As you know, the tcp socket has a 1.4kb limitation so how do you send data larger than that? You break it into chunks, or multiple packets. Unlike UDP, TCP guarantees delivery AND order of the packets so long as the socket connection hasn't changed. The first step is in create a standard packet header your app will use. In my example, I simply write a code to the first byte. For normal game data which comes in at less than the 1.4kb limit, you could simply write 0 so the client knows it's a single packet with nothing following it. For an image, I send a 2 followed by total size to expect. The client will see the code, know it's an image and the next integer in that packet will contain the size. We then begin flushing the image data to the client. The client will receive the raw data and use it to rebuild the image until that size is met. Once all the bytes are received by the client, the client will then go back to reading packets the normal way; looking at the code in the first byte.

An option I didn't include but could be added is to send a checksum with the initial coded packet. Then when the image has been fully reconstructed you can use the checksum to verify the integrity of the image.

edit: corrected a typo that somehow still worked correctly.

Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda
Pixel-Perfect Collision

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2024-04-27 17:34:12
Your offset time is: 2024-04-27 17:34:12