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

Author Topic: TCP Packets - Custom recipient?  (Read 2022 times)

0 Members and 1 Guest are viewing this topic.

Lys

  • Newbie
  • *
  • Posts: 3
    • View Profile
TCP Packets - Custom recipient?
« on: January 18, 2014, 02:48:52 pm »
Hello,
I intend to use SFML for the server side of my game and so far I've managed to register incoming clients etc. and it is working fine.
The next logical step would be sending real data between the server and the client but I am not using SFML for my client side. I wanted to use the SFML packets with TCP but I just read the following in the packets tutorial:
Quote
However, it has a drawback: to preserve message boundaries, sf::Packet has to send extra bytes with your data, which implies that you can only receive them with a sf::Packet if you want them to be properly decoded. In other words, you can't send a SFML packet to a custom recipient, it has to use a SFML packet too. Note that this applies to TCP only, UDP is fine since the protocol itself ensures message boundaries.
So as far as I understood, it is required to use SFML on both ends if I want to use the SFML packet structure, right? If this is the case, there surely is another way to "decode" the packet on the client side, can anyone point me to the code where the packet is encoded or has anyone ever written a function that would manually do this (C++ preferably)? I guess it would be easier to write something like that instead of wrapping my send functions to use byte arrays.
Thanks, Lys

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: TCP Packets - Custom recipient?
« Reply #1 on: January 18, 2014, 03:11:28 pm »
The packet structure is simple:
32 bit unsigned in network byte order that is amount of data in packet, followed by that amount of bytes.
All ints are in network order, strings are length prefixed by 32 bit unsigned but are not null terminated, floats and doubles are just memcpied into it. That's it(I hope I didn't make a mistake).
https://github.com/SFML/SFML/blob/master/src/SFML/Network/TcpSocket.cpp
https://github.com/SFML/SFML/blob/master/src/SFML/Network/Packet.cpp

You might want to look into SFNUL in projects section too, it's a replacement networking module that has API similar to SFMLs.
« Last Edit: January 18, 2014, 03:13:56 pm by FRex »
Back to C++ gamedev with SFML in May 2023