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

Author Topic: UDP works locally but not through the internet  (Read 2057 times)

0 Members and 1 Guest are viewing this topic.

guassmith

  • Newbie
  • *
  • Posts: 3
    • View Profile
UDP works locally but not through the internet
« on: March 05, 2016, 01:00:38 am »
I am trying to create a server and client using sf::UdpSocket, when testing locally on the same computer the program worked and sent the messages, however when trying it over the internet with a friend it didn't work, no errors, the message just wasn't received.
I changed the code to use sf::TcpSocket and this time it worked, both locally and over the internet. I definitely port forwarded correctly, as I checked both using this website http://www.portchecker.net/udp.php, and http://portforward.com/'s downloadable port checker. Besides this I already have working UDP ports for my teamspeak server, so it's clearly not all UDP traffic.
I googled to see if anyone had this problem but couldn't find anything.
Server Code:
        sf::UdpSocket socket;
        char buffer[1024];
        size_t received = 0;
        sf::IpAddress sender;
        unsigned short port;

        if (socket.bind(2802) != sf::Socket::Done)
        {
                cout << "Error, couldn't bind udp socket!" << endl;
        }
        socket.receive(buffer, sizeof(buffer), received, sender, port);
        cout << sender.toString() << " said: " << buffer << std::endl;

        string message = "Hi, I am " + sf::IpAddress::getLocalAddress().toString();
        socket.send(message.c_str(), message.size() + 1, sender, port);
       

Client Code:
        if (socket.bind(sf::Socket::AnyPort) != sf::Socket::Done)
        {
                cout << "Error, couldn't bind udp socket!" << endl;
        }
        string message = "Hi, I am " + sf::IpAddress::getLocalAddress().toString();
        socket.send(message.c_str(), message.size() + 1, server_ip, server_port);

        char buffer[1024];
        std::size_t received = 0;
        sf::IpAddress sender;
        unsigned short port;

        socket.receive(buffer, sizeof(buffer), received, sender, port);
        cout << sender.toString() << " (The Server) sent back its ip address: " << buffer << endl;
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: UDP works locally but not through the internet
« Reply #1 on: March 05, 2016, 12:39:49 pm »
Did you enable port forwarding for TCP and UDP?

In the end if a UDP packet doesn't end up at the destination, you should be that surprised, because UDP is an unreliable protocol. Any router along the way to the destination could decide to drop your packet. Then again it shouldn't be happening all the time.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

guassmith

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: UDP works locally but not through the internet
« Reply #2 on: March 05, 2016, 02:21:55 pm »
Did you enable port forwarding for TCP and UDP?

In the end if a UDP packet doesn't end up at the destination, you should be that surprised, because UDP is an unreliable protocol. Any router along the way to the destination could decide to drop your packet. Then again it shouldn't be happening all the time.
Yes, I tried both TCP/UDP and UDP on it's own, neither worked. I understand that UDP may drop packets, but I thought that to be pretty rare, considering I tried this like 5 times, I guess I could try sending the message constantly.

guassmith

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: UDP works locally but not through the internet
« Reply #3 on: March 05, 2016, 02:36:22 pm »
Allright, I updated the program and put the socket.send in the main game loop. It now works, thank you, I really wasn't expecting it to drop that much, I guess I should have done this in the first place.