Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sending 'images' over in packets  (Read 7457 times)

0 Members and 1 Guest are viewing this topic.

RyanPaul87

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Sending 'images' over in packets
« on: December 08, 2021, 06:43:20 pm »
I was wondering if some body could give me some starting points, for sending images (or the image data) to a server which would send it to a few dozen clients. Happy to do the work myself but just wanted a jumping point.

I understand I will overload the << >> operators, but, what's the best way to unpack an image (png for instance) into raw data then repack at the other end without losing data.

It doesn't need to be displayed by the server, but pass to other clients, obviously.

Thanks for your patience.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Sending 'images' over in packets
« Reply #1 on: December 09, 2021, 08:58:10 am »
If you're already dealing with "raw" data, it's probably easier to use the sf::TcpSocket::send(const void*    data, std::size_t size, std::size_t& sent) function.
For that you'd then either need to read the image file in its compressed form (less data to transfer) or you can load it with sf::Image and send the uncompressed RGBA data
TCP should ensure that data is being delivered.

On the server side, you just push the same data to the clients.
On the other client side, you take the image data and if it was compressed, you can use loadFromMemory or otherwise, directly load it into an sf::Image again.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

RyanPaul87

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Sending 'images' over in packets
« Reply #2 on: December 09, 2021, 07:59:12 pm »
Hi, thanks for this. I have a few questions on what you've said for clarity.

You said 'if youre already dealing with raw dats, it'd be easier to use TcpSocket::send(const void* data, size t, size t) etc, roughly speaking.

Do you mean, if I'm already doing it this way as opposed to packets, to just continue to do so.

From here, are you saying I can load it into and sf::image 'image' and then send 'image.' If so where does the image become raw data, can you show me a sample code if it's easy enough.

I appreciate you supporting me.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Sending 'images' over in packets
« Reply #3 on: December 11, 2021, 10:20:48 pm »
If all you're sending is a random block of binary data, than that's what send(const void* data, ...) is best for.
But of course you can also pack this data into an sf::Packet and send it over the wire.

I recommend you checkout the official VOIP example, that does exactly this, but with audio instead of image data.

https://github.com/SFML/SFML/blob/master/examples/voip/Client.cpp#L73-L78
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything