SFML community forums

Help => Network => Topic started by: Niely on October 07, 2015, 03:19:11 pm

Title: Receiving weird characters when using UDP
Post by: Niely 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,
Title: Re: Receiving weird characters when using UDP
Post by: eXpl0it3r 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.
Title: Re: Receiving weird characters when using UDP
Post by: Niely on October 07, 2015, 05:22:24 pm
If I initialize it, it just returns an empty value or with whatever I initialize it.
Title: Re: Receiving weird characters when using UDP
Post by: Jesper Juhl on October 07, 2015, 06:08:19 pm
Don't read more from the buffer than receive() put into it.
Title: Re: Receiving weird characters when using UDP
Post by: Laurent on October 07, 2015, 07:22:20 pm
buffer[received] = '\0';
std::cout << buffer;

// or

std::cout << std::string(buffer, buffer + received);
Title: Re: Receiving weird characters when using UDP
Post by: Nexus on October 07, 2015, 07:24:36 pm
buffer[received] = '\0';
std::cout << buffer;
That can be dangerous when received == sizeof(buffer).
Title: Re: Receiving weird characters when using UDP
Post by: Laurent on October 07, 2015, 07:35:56 pm
Then receive sizeof(buffer) - 1 :P
Title: Re: Receiving weird characters when using UDP
Post by: Nexus 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
Title: Re: Receiving weird characters when using UDP
Post by: Niely on October 07, 2015, 10:30:37 pm
Tried both solutions, none worked.
Still getting something empty returned.
Title: Re: Receiving weird characters when using UDP
Post by: Laurent on October 08, 2015, 07:59:43 am
Quote
Tried both solutions, none worked.
Full code + output please ;)
Title: Re: Receiving weird characters when using UDP
Post by: Niely 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 -
Title: Re: Receiving weird characters when using UDP
Post by: Laurent 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.
Title: Re: Receiving weird characters when using UDP
Post by: Niely 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.
Title: Re: Receiving weird characters when using UDP
Post by: Laurent on October 09, 2015, 11:23:29 pm
Now see what sf::Socket::Status it is, try to interpret, ...
Title: Re: Receiving weird characters when using UDP
Post by: Niely 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?

Title: Re: Receiving weird characters when using UDP
Post by: Nexus 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" (https://github.com/SFML/SFML/blob/master/include/SFML/Network/Socket.hpp#L53-L60) ;)
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...
Title: Re: Receiving weird characters when using UDP
Post by: Niely 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?
Title: Re: Receiving weird characters when using UDP
Post by: Laurent 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.
Title: Re: Receiving weird characters when using UDP
Post by: Niely on October 10, 2015, 03:48:12 pm
Is this a SFML problem then?
Title: Re: Receiving weird characters when using UDP
Post by: Laurent 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.
Title: Re: Receiving weird characters when using UDP
Post by: Niely 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.
Title: Re: Receiving weird characters when using UDP
Post by: Nexus on October 10, 2015, 08:04:22 pm
Isn't that clear from the documentation (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1UdpSocket.php#ade9ca0f7ed7919136917b0b997a9833a)? 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.
Title: Re: Receiving weird characters when using UDP
Post by: Niely 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();
}
 
Title: Re: Receiving weird characters when using UDP
Post by: Niely 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.
Title: Re: Receiving weird characters when using UDP
Post by: Laurent on October 15, 2015, 07:53:56 am
Simple question: does replacing your public IP with localhost or your local address, changes anything?
Title: Re: Receiving weird characters when using UDP
Post by: Niely 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.
Title: Re: Receiving weird characters when using UDP
Post by: zsbzsb 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.
Title: Re: Receiving weird characters when using UDP
Post by: binary1248 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.
Title: Re: Receiving weird characters when using UDP
Post by: Niely 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.
Title: Re: Receiving weird characters when using UDP
Post by: zsbzsb 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
Title: Re: Receiving weird characters when using UDP
Post by: Niely on October 15, 2015, 10:45:58 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

Auch, indeed... You're right.
Will this work then, if I do port-forwarding?

Thanks a lot for the help all, by the way.
Title: Re: Receiving weird characters when using UDP
Post by: zsbzsb on October 15, 2015, 11:45:53 pm
Auch, indeed... You're right.
Will this work then, if I do port-forwarding?

You can forward all the ports you want on your router, but if you are sitting behind a NAT (from your ISP) there isn't much you can do. Google for more info, but basically ISPs implemented NATs because the internet was running out of IP addresses. The advantage to a NAT is that your ISP can group a whole bunch of customers behind a single public IP address. The disadvantage as you are experiencing is that you have no way to allow incoming connections (how is the NAT supposed to know which customer the intended data was for?).

In essence, your own home router is a NAT because it hides all of your other devices you have connected to it behind a single connection to the internet (through the ISP's modem). That is where port forwarding comes in, when you setup port forwarding the NAT (your router) now knows where to send incoming data for a specific port instead of just dropping it.

Generally ISPs offer a more expensive 'business' plan that includes a static public IP address which would then allow you to handling incoming connections (port forwarding) and host your own server.
Title: Re: Receiving weird characters when using UDP
Post by: binary1248 on October 16, 2015, 01:26:17 am

    +--------+                        +---------------------------+
    |        | ---- src = 55001 ----> | ---- randomize port ----> | --- src = 65432 ----+
    |   PC   |                        |          Gateway          |                     |
    |        | <----- ???             |   not mapped : drop <---- | <-- dst = 55001 ----+
    +--------+                        +---------------------------+

 
You made your life harder by using the same port to send and receive. If you use 2 different ports, you could port forward like zsbzsb said, but because you have to get into the hole that your outgoing data came out of, it is all down to guesswork if you use the same port. This is a feature (a good intended one) of stateful firewalls. It is meant to, among other things, protect you and your internal network. Instead of trying to circumvent it, you might want to consider alternate solutions if possible.
Title: Re: Receiving weird characters when using UDP
Post by: Niely on October 16, 2015, 10:03:00 pm
Auch, indeed... You're right.
Will this work then, if I do port-forwarding?

Generally ISPs offer a more expensive 'business' plan that includes a static public IP address which would then allow you to handling incoming connections (port forwarding) and host your own server.

So, not everyone can port-forward? Those with a 'cheaper' plan?
How do torrent-protocols and all other p2p applications work then?
Title: Re: Receiving weird characters when using UDP
Post by: Niely on October 16, 2015, 10:03:46 pm

    +--------+                        +---------------------------+
    |        | ---- src = 55001 ----> | ---- randomize port ----> | --- src = 65432 ----+
    |   PC   |                        |          Gateway          |                     |
    |        | <----- ???             |   not mapped : drop <---- | <-- dst = 55001 ----+
    +--------+                        +---------------------------+

 
You made your life harder by using the same port to send and receive. If you use 2 different ports, you could port forward like zsbzsb said, but because you have to get into the hole that your outgoing data came out of, it is all down to guesswork if you use the same port. This is a feature (a good intended one) of stateful firewalls. It is meant to, among other things, protect you and your internal network. Instead of trying to circumvent it, you might want to consider alternate solutions if possible.

Thanks a lot.
I'll be using two different ports, one for receiving and one for sending.