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

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

0 Members and 1 Guest are viewing this topic.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Receiving weird characters when using UDP
« on: October 07, 2015, 03:19:11 pm »
Hello

My code does not give any error, but it ain't really working like it should be.
I'm trying to send a simple message over UDP.

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

using namespace std;

int main() {
        sf::UdpSocket socket;
        string receiver;
        string msg;
        socket.bind(55001);
       
        //Configure send options
        /*cout << "Send to: ";
        getline(cin, receiver);*/

        receiver = "MY ONLINE IP";
        cout << "Message: ";
        getline(cin, msg);

        //Send to myself (IPCONFIG)
        socket.send(msg.c_str(), msg.size() +1, receiver, 55001);

        //Receive from myself
        char buffer[1024];
        size_t received = 0;
        sf::IpAddress sender;
        unsigned short port;
       
        socket.receive(buffer, sizeof(buffer), received, sender, port);
        cout << endl << endl << "From: " << sender << " - " << buffer << endl;

        cin.ignore().get();
        return 0;
}


 

This code, is filling the rest of the buffer with weird characters.

Quote
Message: Test


From: 0.0.0.0 - ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠pï▬☺Test

How do I need to fix this?

Thanks for reading,

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Receiving weird characters when using UDP
« Reply #1 on: October 07, 2015, 03:43:37 pm »
The buffer array is uninitialized so it contains random characters. So the problem is not related to UDP at all, it's just that you use uninitialized memory.

Additionally don't use endl and don't use using namespace std;.
std::endl does also flush the stream which is not always what you want, just use "\n" instead and it might be slightly more expressive.
And by explicitly writing the namespace, you'll make your code a lot more readable, because at a glance everybody knows from where a function originates from.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Receiving weird characters when using UDP
« Reply #2 on: October 07, 2015, 05:22:24 pm »
If I initialize it, it just returns an empty value or with whatever I initialize it.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Receiving weird characters when using UDP
« Reply #3 on: October 07, 2015, 06:08:19 pm »
Don't read more from the buffer than receive() put into it.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Receiving weird characters when using UDP
« Reply #4 on: October 07, 2015, 07:22:20 pm »
buffer[received] = '\0';
std::cout << buffer;

// or

std::cout << std::string(buffer, buffer + received);
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Receiving weird characters when using UDP
« Reply #5 on: October 07, 2015, 07:24:36 pm »
buffer[received] = '\0';
std::cout << buffer;
That can be dangerous when received == sizeof(buffer).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Receiving weird characters when using UDP
« Reply #6 on: October 07, 2015, 07:35:56 pm »
Then receive sizeof(buffer) - 1 :P
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Receiving weird characters when using UDP
« Reply #7 on: October 07, 2015, 08:01:09 pm »
Yeah, until the next maintainer thinks "why? we have one byte left" and two months later, peculiar bugs appear at the other corner of the application :D
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 #8 on: October 07, 2015, 10:30:37 pm »
Tried both solutions, none worked.
Still getting something empty returned.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Receiving weird characters when using UDP
« Reply #9 on: October 08, 2015, 07:59:43 am »
Quote
Tried both solutions, none worked.
Full code + output please ;)
Laurent Gomila - SFML developer

Niely

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

using namespace std;

int main() {
        sf::UdpSocket socket;
        string receiver;
        string msg;
        socket.bind(55001);
       
        //Configure send options
        /*cout << "Send to: ";
        getline(cin, receiver);*/

        receiver = "84.195.14.184";
        cout << "Message: ";
        getline(cin, msg);

        //Send to myself (IPCONFIG)
        socket.send(msg.c_str(), msg.size() +1, receiver, 55001);

        //Receive from myself
        char buffer[1024];
/*Also tried: char buffer[1024] = ""; & char buffer[1024] = {}; & ...*/
        size_t received = 0;
        sf::IpAddress sender;
        unsigned short port;
       
        socket.receive(buffer, sizeof(buffer), received, sender, port);
        buffer[received] = '\0';
        cout << "\n" << "\n" << "From: " << sender << " - " <<  buffer /*also tried only 'string(buffer, buffer + received)' without '\0'*/ << "\n";

        cin.ignore().get();
        return 0;
}


 

I can't declare my buffer like buffer[received], because I need it in my function 'send', and only send returns 'receive'.


Output: From: 0.0.0.0 -

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Receiving weird characters when using UDP
« Reply #11 on: October 08, 2015, 11:25:46 pm »
This is not the same output at all: no more garbage, it's completely empty. Print more information: the status returned by the receive function, and the value of the "received" variable. These are mandatory before trying to interpret the reception buffer.
Laurent Gomila - SFML developer

Niely

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

using namespace std;

int main() {
        sf::UdpSocket socket;
        string receiver;
        string msg;
        socket.bind(55001);

        //Configure send options
        /*cout << "Send to: ";
        getline(cin, receiver);*/

        receiver = "YOUR PUBLIC WI-FI IP";
        cout << "Message: ";
        getline(cin,msg);

        //Send to myself (IPCONFIG)
        int y = socket.send(msg.c_str(),msg.size() +1,receiver,55001);
        cout << "Send: " << y << endl;

        //Receive from myself
        char buffer[1024];
        size_t received = 0;
        sf::IpAddress sender;
        unsigned short port;

        int x = socket.receive(buffer,sizeof(buffer),received,sender,port);
        buffer[received] = '\0';
        cout << endl << endl << "From: " << sender << " - " <<  buffer << endl << "Receive: " << x;

        cin.ignore().get();
        return 0;
}
 

Output:
Quote
Message: Test
Send: 0


From: 0.0.0.0 -
Receive: 3

Send returns 0, receive returns 3.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Receiving weird characters when using UDP
« Reply #13 on: October 09, 2015, 11:23:29 pm »
Now see what sf::Socket::Status it is, try to interpret, ...
Laurent Gomila - SFML developer

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Receiving weird characters when using UDP
« Reply #14 on: October 10, 2015, 10:57:51 am »
I already tried looking into that. But status 3 is 'Error', an unknown error occurred.
How do I need to see my error in there?