SFML community forums

Help => Network => Topic started by: jm2118 on July 01, 2017, 02:34:30 am

Title: UDP Non-Blocking Socket Problem - Adding a Receive() call breaks code
Post by: jm2118 on July 01, 2017, 02:34:30 am
I am having a problem sending messages with non-blocking UDP sockets. I have reduced the problem to two minimal pieces of code. The following code works:

#include<SFML\Graphics.hpp>
#include<SFML\Network.hpp>
#include<string>
#include<iostream>

using namespace std;

//Variables
sf::RenderWindow window;
sf::UdpSocket socket;
sf::Packet packet;
sf::IpAddress address;
unsigned short port = 3500;
int message = 0;

int main()
{
        window.clear();
        window.display();

        //Setup
        string line;
        socket.bind(3500);
        cout << "Enter IP: " << endl;
        cin >> line;
        address = sf::IpAddress(line);

        //Note - non blocking
        socket.setBlocking(false);

        while (!sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
        {
                window.clear();
                window.display();

                packet.clear();
                packet << 100;
                socket.send(packet, address, port);
               
                socket.receive(packet, address, port);
                while (packet.getDataSize() > 0)
                {
                        int message;
                        packet >> message;
                        cout << message << endl;
                }
        }
}

But the following does not:

#include<SFML\Graphics.hpp>
#include<SFML\Network.hpp>
#include<string>
#include<iostream>

using namespace std;

//Variables
sf::RenderWindow window;
sf::UdpSocket socket;
sf::Packet packet;
sf::IpAddress address;
unsigned short port = 3500;
int message = 0;

int main()
{
        window.clear();
        window.display();

        //Setup
        string line;
        socket.bind(3500);
        cout << "Enter IP: " << endl;
        cin >> line;
        address = sf::IpAddress(line);

        //Note - non blocking
        socket.setBlocking(false);

        while (!sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
        {
                window.clear();
                window.display();

                packet.clear();
                packet << 100;
                socket.send(packet, address, port);
               
                socket.receive(packet, address, port);
                while (packet.getDataSize() > 0)
                {
                        int message;
                        packet >> message;
                        cout << message << endl;

                        socket.receive(packet, address, port);
                }
        }
}

The only difference between the two pieces of code is the extra socket.receive() call. Why is this causing the second piece of code to break down?
Title: Re: UDP Non-Blocking Socket Problem - Adding a Receive() call breaks code
Post by: jm2118 on July 02, 2017, 06:20:42 pm
Problem resolved when I replaced

socket.receive(packet, address, port);

with

sf::IpAddress copyAddress = address;
unsigned short copyPort = port;
socket.receive(packet, copyAddress, copyPort);

Is socket.receive() supposed to modify the address and port arguments?
Title: Re: UDP Non-Blocking Socket Problem - Adding a Receive() call breaks code
Post by: Gleade on July 03, 2017, 12:41:37 am
Problem resolved when I replaced

socket.receive(packet, address, port);

with

sf::IpAddress copyAddress = address;
unsigned short copyPort = port;
socket.receive(packet, copyAddress, copyPort);

Is socket.receive() supposed to modify the address and port arguments?

Yes, that way you know the IP address / port of the sender.
Title: Re: UDP Non-Blocking Socket Problem - Adding a Receive() call breaks code
Post by: jm2118 on July 03, 2017, 12:57:19 am
Ah, ok. Is there a way to call the receive function without giving it an address and port?
Title: Re: UDP Non-Blocking Socket Problem - Adding a Receive() call breaks code
Post by: Gleade on July 03, 2017, 02:23:49 am
Ah, ok. Is there a way to call the receive function without giving it an address and port?

Not without editing the SFML source. If you don't need that information, just create two local-scope variables like you've done and let them go out of scope :)
Title: Re: UDP Non-Blocking Socket Problem - Adding a Receive() call breaks code
Post by: Laurent on July 03, 2017, 07:58:48 am
And note that this stuff is very well explained in the documentation and tutorials... ;)