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

Author Topic: Receiving weird characters when using UDP  (Read 14867 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Receiving weird characters when using UDP
« Reply #15 on: October 10, 2015, 11:03:48 am »
But status 3 is 'Error', an unknown error occurred.
I don't know how you count, but 3 is "Disconnected" ;)
enum Status
{
    Done,         ///< The socket has sent / received the data
    NotReady,     ///< The socket is not ready to send / receive data yet
    Partial,      ///< The socket sent a part of the data
    Disconnected, ///< The TCP socket has been disconnected
    Error         ///< An unexpected error happened
};

And you're still having potential UB, despite my advice above...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Receiving weird characters when using UDP
« Reply #16 on: October 10, 2015, 11:24:33 am »
Ah, I looked here: http://www.sfml-dev.org/documentation/2.0-fr/classsf_1_1Socket.php#a51bf0fd51057b98a10fbb866246176dc

But I'm not even using TCP, I'm using UDP and that's a non-connection based protocol if I'm right?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Receiving weird characters when using UDP
« Reply #17 on: October 10, 2015, 03:00:47 pm »
Quote
Ah, I looked here: http://www.sfml-dev.org/documentation/2.0-fr/classsf_1_1Socket.php#a51bf0fd51057b98a10fbb866246176dc
This is the documentation for SFML 2.0.

Quote
But I'm not even using TCP, I'm using UDP and that's a non-connection based protocol if I'm right?
True, so I'm not sure what could cause this error to be returned in this context. Maybe we should try to reproduce the problem, for further investigation.
Laurent Gomila - SFML developer

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Receiving weird characters when using UDP
« Reply #18 on: October 10, 2015, 03:48:12 pm »
Is this a SFML problem then?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Receiving weird characters when using UDP
« Reply #19 on: October 10, 2015, 06:00:10 pm »
It is very unlikely, given the basic stuff that we're talking about, but since I can't spot an obvious mistake in your code, I'd say we should try to reproduce it, to start.
Laurent Gomila - SFML developer

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Receiving weird characters when using UDP
« Reply #20 on: October 10, 2015, 07:20:47 pm »

#include <iostream>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>

using namespace std;

int main() {
        sf::UdpSocket socket;
        sf::IpAddress ip = "84.195.14.184";
        unsigned short port = 54000;
        size_t x = 100;
        sf::Socket::Status status;

        char data[50] = "Test";

        //Send part
        status = socket.send(data, x, ip, port);
        if (status == sf::Socket::Done) {
                cout << "[Announcement] Packed sended successfull." <<endl;
        } else {
                cout << "[Error] Failed. Error code returned: " << status << " (Send)." <<endl;
        }

        //Receive part
        char receivedPacket[50];
        status = socket.receive(receivedPacket, sizeof(receivedPacket) +1, x, ip, port);
        if (status == sf::Socket::Done) {
                cout << "[Announcement] Packet received succesfull." <<endl;
        } else {
                cout << "[Error] Failed. Error code returned: " << status << " (Receive)." <<endl;
        }

        cin.ignore().get();
}
 

Tried again from scratch and failed again.
However, I think I saw something strange...

If you look at the receive function on line 27.
When looking at the documentation, the parameters are:
(data, size, received, remoteAddress, remotePort).
Size is the maximum numbers I can receive,
received is the actual number of bytes I received.

So you'd expect, that these should be my parameters:
(receivedPacket, x, sizeof(receivedPacket), ip, port)
x: I manually set a maximum I want to receiver,
sizeof(receivedPacket): Parameter gets (actual) size of packet.

Weird enough, if I take this order I get an error.
Why is that?
I personally don't understand that part.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Receiving weird characters when using UDP
« Reply #21 on: October 10, 2015, 08:04:22 pm »
Isn't that clear from the documentation? The second parameter is the size of your buffer, while the third is an output parameter denoting the actually received size. Since a reference is used for the output parameter, you can't pass any temporary objects.

And for the third time,
    char receivedPacket[50];
    status = socket.receive(receivedPacket, sizeof(receivedPacket) +1, x, ip, port);
is again wrong.

Please read the documentation carefully; it's important to avoid such bugs, as they are capable of blowing up your entire program.
« Last Edit: October 10, 2015, 08:07:22 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Receiving weird characters when using UDP
« Reply #22 on: October 10, 2015, 09:34:34 pm »
Is this better then?

size_t z;
status = socket.receive(receivedPacket, x, z, ip , port);

Still receiving the same error code with my sockets though.

Full code:
#include <iostream>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>

using namespace std;

int main() {
        sf::UdpSocket socket;
        sf::IpAddress ip = "84.195.14.184";
        unsigned short port = 54000;
        size_t x = 100;
        sf::Socket::Status status;

        char data[50] = "Test";

        //Send part
        status = socket.send(data, x, ip, port);
        if (status == sf::Socket::Done) {
                cout << "[Announcement] Packed sended successfull." <<endl;
        } else {
                cout << "[Error] Failed. Error code returned: " << status << " (Send)." <<endl;
        }

        //Receive part
        char receivedPacket[50];
        size_t z;
        status = socket.receive(receivedPacket, x, z, ip , port);
        if (status == sf::Socket::Done) {
                cout << "[Announcement] Packet received succesfull." <<endl;
        } else {
                cout << "[Error] Failed. Error code returned: " << status << " (Receive)." <<endl;
        }

        cin.ignore().get();
}
 

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Receiving weird characters when using UDP
« Reply #23 on: October 14, 2015, 06:57:16 pm »
Is there already a solution found? Does such a simple network code works for others? I'd like to continue my project.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Receiving weird characters when using UDP
« Reply #24 on: October 15, 2015, 07:53:56 am »
Simple question: does replacing your public IP with localhost or your local address, changes anything?
Laurent Gomila - SFML developer

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Receiving weird characters when using UDP
« Reply #25 on: October 15, 2015, 07:42:28 pm »
Nope it doesn't. Tried 127.0.0.1, localhost, my ipconfig (local IP in network), and of course my public one. No difference. Firewall is off by the way.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Receiving weird characters when using UDP
« Reply #26 on: October 15, 2015, 08:14:31 pm »
Dumb statement: but I wasn't aware that the same socket could receive its own data that it just sent (TCP or UDP for that matter). What if you actually open a second socket for receiving and only bind the receiving socket? Oh, and your revised code never binds to a port.
« Last Edit: October 15, 2015, 08:17:57 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Receiving weird characters when using UDP
« Reply #27 on: October 15, 2015, 08:43:39 pm »
#include <iostream>
#include <string>
#include <cstring>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>

using namespace std;

int main() {
    sf::UdpSocket socket;
    sf::IpAddress ip = "127.0.0.1";
    unsigned short port = 54000;
    size_t x = 100;
    sf::Socket::Status status;

    socket.bind(54000);

    char data[50] = "Test";

    //Send part
    status = socket.send(data, x, ip, port);
    if (status == sf::Socket::Done) {
        cout << "[Announcement] Packed sended successfull." <<endl;
    } else {
        cout << "[Error] Failed. Error code returned: " << status << " (Send)." <<endl;
    }

    //Receive part
    char receivedPacket[50];
    memset(receivedPacket, 0, 50);
    size_t z;
    status = socket.receive(receivedPacket, x, z, ip , port);
    if (status == sf::Socket::Done) {
        cout << "[Announcement] Packet received succesfull." <<endl;
    } else {
        cout << "[Error] Failed. Error code returned: " << status << " (Receive)." <<endl;
    }

    cout << receivedPacket << endl;

    cin.ignore().get();
}
You didn't bind the socket to a port, so obviously the operating system didn't know where to receive the data from. Using the above code I can receive the test string as expected.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Receiving weird characters when using UDP
« Reply #28 on: October 15, 2015, 09:09:28 pm »
#include <iostream>
#include <string>
#include <cstring>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>

using namespace std;

int main() {
    sf::UdpSocket socket;
    sf::IpAddress ip = "127.0.0.1";
    unsigned short port = 54000;
    size_t x = 100;
    sf::Socket::Status status;

    socket.bind(54000);

    char data[50] = "Test";

    //Send part
    status = socket.send(data, x, ip, port);
    if (status == sf::Socket::Done) {
        cout << "[Announcement] Packed sended successfull." <<endl;
    } else {
        cout << "[Error] Failed. Error code returned: " << status << " (Send)." <<endl;
    }

    //Receive part
    char receivedPacket[50];
    memset(receivedPacket, 0, 50);
    size_t z;
    status = socket.receive(receivedPacket, x, z, ip , port);
    if (status == sf::Socket::Done) {
        cout << "[Announcement] Packet received succesfull." <<endl;
    } else {
        cout << "[Error] Failed. Error code returned: " << status << " (Receive)." <<endl;
    }

    cout << receivedPacket << endl;

    cin.ignore().get();
}
You didn't bind the socket to a port, so obviously the operating system didn't know where to receive the data from. Using the above code I can receive the test string as expected.

Thanks! This code now indeed works for local IP-address, but it doesn't for the public one.

Dumb statement: but I wasn't aware that the same socket could receive its own data that it just sent (TCP or UDP for that matter). What if you actually open a second socket for receiving and only bind the receiving socket? Oh, and your revised code never binds to a port.

Is this the way to do it then?
#include <iostream>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>

using namespace std;

int main() {
        sf::UdpSocket socket1;
        sf::UdpSocket socket2;
                                                //REPLACE IP ADDRESS HERE!!!
        sf::IpAddress ip = "PUBLIC IP";
        unsigned short port = 55001;
        size_t x = 50;
        sf::Socket::Status status;

        char data[50] = "Test";
       
        //Send part
        status = socket1.send(data, x, ip, port);
        if (status == sf::Socket::Done) {
                cout << "[Announcement] Packed sended successfull." <<endl;
        } else {
                cout << "[Error] Failed. Error code returned: " << status << " (Send)." <<endl;
        }

        //Receive part
        socket2.bind(port);
        char receivedPacket[50] = {"njknjfkz"};
        size_t z;
        status = socket2.receive(receivedPacket, x, z, ip, port);
        if (status == sf::Socket::Done) {
                cout << "[Announcement] Packet received succesfull." <<endl;
        } else {
                cout << "[Error] Failed. Error code returned: " << status << " (Receive)." <<endl;
        }

        cout << endl << "Received: " << receivedPacket << " - " << z <<endl;

        cin.ignore().get();
}
 
Because with this, the sending is successful, but the receiving just don't display.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Receiving weird characters when using UDP
« Reply #29 on: October 15, 2015, 09:57:24 pm »
Is this the way to do it then?
<code>

Well according to binary1248 a UDP socket can receive from itself and as you already said it works so no need for the second socket. But when it comes to sending stuff to your public IP and hoping it gets through that is an entirely different issue. You are most likely behind a NAT from your ISP so there is no way for you to allow incoming connections.

See: http://www.sfml-dev.org/faq.php#network-internet-network
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor