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

Author Topic: UDP Socket not recieving updated data.  (Read 7681 times)

0 Members and 1 Guest are viewing this topic.

average

  • Newbie
  • *
  • Posts: 2
    • View Profile
UDP Socket not recieving updated data.
« on: January 01, 2021, 03:43:56 pm »
I am tryng to send a packet of data to another instance of a program on the same PC using UDP Socket but when I update the data and send it the recieve only recievs the intial data.  I have gotr my TCP stuff working but i dont know if im missing something on the UDP side. I have followed along with the SFML tutorials for sockets and packets so I'm a little stuck now.
I'm setting up the sockets like this:
        sf::TcpSocket serverSocket;
        serverSocket.setBlocking(true);
        sf::Socket::Status status = serverSocket.connect("127.0.0.1", 1000);

        if (status != sf::Socket::Done)
        {
                //error...
        }


        while(serverSocket.receive(serverData) != sf::Socket::Done)
        {
                //error
        }

        serverData >> playerID >> mapSeed >> recievedPort;
       
        std::cout << recievedPort;

        std::cout << mapSeed << "\n";
        std::cout << playerID << "\n";

        if (playerID == 1)
        {
                ship.setPosition(100, 100);
                sendPort = recievedPort + 1;
                bindPort = recievedPort;
        }

        if (playerID == 2)
        {
                ship.setPosition(1600, 900);
                sendPort = recievedPort - 1;
                bindPort = recievedPort;
        }

        sf::UdpSocket playerSocket;
        playerSocket.setBlocking(false);
        if (playerSocket.bind(bindPort) != sf::Socket::Done)
        {
                // error...
        }

        srand(mapSeed);

        for (int i = 0; i < XTILES; i++)
        {
                for (int j = 0; j < YTILES; j++)
                {

                        tileMap[i][j].setRenderValue(rand() % 20 + 1);

                }
        }
 
Which recieves fine and I send my data like this (All the packet data is float):
playerData.x = ship.getPosition().x;
                playerData.y = ship.getPosition().y;
                playerData.shipOrientation = ship.getOrientation();
                playerData.cannonOrientation = ship.getCannonOrientation();
                playerData.time = 0;

                playerPacket << playerData;

                if (playerSocket.send(playerPacket,sf::IpAddress::LocalHost , sendPort) != sf::Socket::Done)
                {
                        // error...
                }


                sf::IpAddress sender;
                if (playerSocket.receive(enemyPacket, sender, bind) != sf::Socket::Done)
                {
                        // error...
                }

                enemyPacket >> enemyData;

                eShip.setPosition(enemyData.x, enemyData.y);
 

The enemy data recieved is just always the the initial data sent. Am I missing anything?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: UDP Socket not recieving updated data.
« Reply #1 on: January 01, 2021, 04:43:10 pm »
Maybe you have a persistent sf::Packet instance, and you forget to clear it between each new data?
Laurent Gomila - SFML developer

 

anything