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

Author Topic: The problem of transferring data to clients with a UDP socket  (Read 380 times)

0 Members and 1 Guest are viewing this topic.

Otrellona

  • Newbie
  • *
  • Posts: 2
    • View Profile
There is a server that creates an array of integers and puts them in a package, and two clients that immediately after connecting receive a package with an array of integers and use it to generate a game map.

The problem is that if both clients are connected to localhost, then they receive the same data, when connected from different devices, clients receive different data.

server.cpp
    sf::UdpSocket socket;

    if (socket.bind(54000) != sf::Socket::Done) {
        return -1;
    }

    std::cout << "Server is listening on port 54000" << std::endl;

    const sf::Uint16 side = 10;
    sf::Uint16 level[side * side];
    for (int i = 0; i < std::size(level); i++) {
        int start = 0;
        int end = side;
        int x = rand() % (end - start + 1) + start;

        if (x < 3)
            level[i] = 1;
        else
            level[i] = 0;
    }
     ....
    sf::Packet packet;
    packet << level[side * side];
    socket.send(packet, client1Ip, client1Port);

client.cpp
    const sf::Uint16 side = 10;
    sf::Packet packet;

    if (socket.receive(packet, serverIp, port) != sf::Socket::Done) {
        std::cerr << "Error receiving data" << std::endl;
        return -1;
    }

    sf::Uint16 level[side*side];
    packet >> level[side*side];

What could be the reason for this behavior and how can it be fixed?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10916
    • View Profile
    • development blog
    • Email
Re: The problem of transferring data to clients with a UDP socket
« Reply #1 on: June 17, 2024, 12:23:51 pm »
What does "different data" mean exactly?

Note that UDP doesn't make any guarantees in order of packets and arrival of packets.
Locally you'll unlikely see such an issue, but once you go into the "wild west" that is the internet, your UDP packets can be dropped or they can be reordered.

The question might then be more specifically, do you get the data but with missing packets or in a different order?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Otrellona

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: The problem of transferring data to clients with a UDP socket
« Reply #2 on: June 17, 2024, 04:09:23 pm »
Thanks for respond, I solved the problem by using TCP socket and changing the way to send data, didn`t expect to UDP work so bad even with two players only.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10916
    • View Profile
    • development blog
    • Email
Re: The problem of transferring data to clients with a UDP socket
« Reply #3 on: June 17, 2024, 05:57:45 pm »
It's not really about the amount of players/clients but more about the quality of the connection.
For example if the connection is congested somewhere on the way, you can experience some dropped packets.

But yes, if you need a reliable connection you should use TCP or build your own reliable protocol on top of UDP.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kojack

  • Sr. Member
  • ****
  • Posts: 332
  • C++/C# game dev teacher.
    • View Profile
Re: The problem of transferring data to clients with a UDP socket
« Reply #4 on: June 19, 2024, 05:08:50 pm »
The main advantage to UDP is that some game data isn't necessary as such. For example movement data is going to be sent frequently, if you miss a packet or it arrives out of order you can ignore it because another packet is about to replace it in maybe 50-100ms anyway. It's faster to ignore the missing data and just get the next packet. But TCP would keep retrying to get the missing packet, holding up all of the next packets.

So most games use UDP with their own reliability protocol on top. Basically replicating that part of TCP for data that has to get through (like your map data). THis lets you pick which packets need the (potentially slower) reliable mode, whereas TCP forced every packet to be reliable.