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

Author Topic: Occasional lags within UDP network  (Read 2294 times)

0 Members and 1 Guest are viewing this topic.

thewhitebridge

  • Newbie
  • *
  • Posts: 1
    • View Profile
Occasional lags within UDP network
« on: October 03, 2010, 01:10:35 pm »
Hello!
We we are quite new to network programming, so we just wantet to try sfml with a small network program, that sends nothing but a single float.
For visualisation, we are drawing and rotating a sprite from the sfml graphics library, and send that to a 2nd computer, which draws a sprite with that value, too.

But here is the problem: Although most of the time it works flawlessly, every 10 to 15 seconds, the rotation stops and starts a few times, and than catches up fast again.  

So: are we having bad computers or are we doing something in this code terribly wrong, that causes lags?

Heres the sending part:

Code: [Select]
#include <iostream>
#include "SFML/Network.hpp"
#include "SFML/Graphics.hpp"

int main()
{
    sf::Clock Clock;

    sf::UdpSocket SendSocket;

    sf::IpAddress sender;
    sf::Packet paket;
    unsigned short port;
    float rotation;

    sf::RenderWindow App(sf::VideoMode(400, 300, 32), "SFML Network");

    sf::Sprite Sprite;
    Sprite.SetPosition(200.f, 150.f);
    Sprite.SetScale(60.f, 60.f);

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        rotation=Clock.GetElapsedTime()*100;
        paket << rotation;

        if(SendSocket.Send(paket, "127.0.0.1", 4567)!=sf::Socket::Done){
            std::cout << "Error" << std::endl;
        }
        std::cout << paket.GetDataSize() << std::endl;
        paket.Clear();



        Sprite.SetRotation(rotation);
        App.Clear();
        App.Draw(Sprite);
        App.Display();

        sf::Sleep(0.001f);
    }

    return EXIT_SUCCESS;
}


and the receiving part:

Code: [Select]

#include <iostream>
#include "SFML/Network.hpp"
#include "SFML/Graphics.hpp"

int main()
{
    sf::UdpSocket ReceiveSocket;
    if (ReceiveSocket.Bind(4567))
    {
        std::cout << "Error Binding" << std::endl;
    }

    sf::IpAddress sender;
    sf::Packet paket;
    unsigned short port;
    float rotation;

    sf::RenderWindow App(sf::VideoMode(400, 300, 32), "SFML Network");

    sf::Sprite Sprite;
    Sprite.SetPosition(200.f, 150.f);
    Sprite.SetScale(60.f, 60.f);

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        if(ReceiveSocket.Receive(paket, sender, port)!=sf::Socket::Done) {
            std::cout << "Error Recieving" << std::endl;
        }

        paket >> rotation;

        Sprite.SetRotation(rotation);
        App.Clear();
        App.Draw(Sprite);
        App.Display();
    }

    return EXIT_SUCCESS;
}



I hope, you find something in here, because, well...otherwise its our computers fault^^.

Best regards

Heinrich

ekun

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Vagante
    • Email
Occasional lags within UDP network
« Reply #1 on: October 21, 2010, 05:02:39 am »
It looks like from your code you're constantly pumping out packets. Keep in mind that any game that uses networking should never be doing this.

Networks can only handle so many packets at a time, and sending too many can result in a packet buffer problem.

What your test should be doing is restraining the sending of packets to a specific number of frames per second.

Many action games send game packets from 10 - 20 fps. Play with these numbers to find something that works for you.
@ekunenuke