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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - thewhitebridge

Pages: [1]
1
Network / 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

Pages: [1]
anything