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

Author Topic: UDP Non-Blocking Socket Problem - Adding a Receive() call breaks code  (Read 3903 times)

0 Members and 1 Guest are viewing this topic.

jm2118

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
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?
« Last Edit: July 02, 2017, 01:27:35 am by jm2118 »

jm2118

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
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?

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
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.

jm2118

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Ah, ok. Is there a way to call the receive function without giving it an address and port?

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
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 :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
And note that this stuff is very well explained in the documentation and tutorials... ;)
Laurent Gomila - SFML developer