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

Author Topic: Code using udp socket not working  (Read 4104 times)

0 Members and 1 Guest are viewing this topic.

Shinoken

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Code using udp socket not working
« on: November 07, 2022, 05:06:40 pm »
Why does this code work if i test it by opening the .exe file 2 times on my computer but doesn't if i try it on separate ones?

#include "SFML\Graphics.hpp"
#include "SFML\Window.hpp"
#include "SFML\Network.hpp"\

#include <iostream>
#include <vector>

using namespace std;

int main()
{
        sf::UdpSocket socket;
        sf::IpAddress thisIp = sf::IpAddress::getLocalAddress();
        socket.bind(sf::Socket::AnyPort);
        char clientServerOption;
        cout << "Type 'c' for client and 's' for server"<<endl;
        cin >> clientServerOption;

        if(clientServerOption == 'c')
        {
                string str;
                cout << "Type the IP address here" << endl;
                cin >> str;
                sf::IpAddress serverIp = sf::IpAddress(str);
                cout << "Type the Server Port here" << endl;
                cin >> str;
                unsigned short serverPort = stoi(str);

                cout << "Send: ";
                cin.ignore();
                getline(cin, str, '\n');

                sf::Packet packet;
                packet << str;
                socket.send(packet, serverIp, serverPort);
                socket.receive(packet, serverIp, serverPort);
                packet >> str;
                cout << "Server says: " << str << endl;
        }
        else if(clientServerOption == 's')
        {
                vector <sf::IpAddress> clientIps;
                vector <unsigned short> clientPorts;
                clientIps.push_back(sf::IpAddress());
                clientPorts.push_back(0);
                unsigned short thisPort = socket.getLocalPort();
                cout << "Server hosted with ip " << thisIp << " on port " << thisPort << endl;
                sf::Packet packet;
                socket.receive(packet, clientIps.at(0), clientPorts.at(0));
                string getMessage;
                packet >> getMessage;
                packet.clear();
                cout << getMessage << endl;
                packet << "Message Received";
                socket.send(packet, clientIps.at(0), clientPorts.at(0));
        }

        system("pause");
       
}
« Last Edit: November 09, 2022, 08:39:39 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Code using udp socket not working
« Reply #1 on: November 09, 2022, 08:42:49 am »
Are the separate ones on the same local network (LAN)?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Shinoken

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Code using udp socket not working
« Reply #2 on: November 09, 2022, 09:56:04 am »
Yes they are, I tried it on my school's network since i don't have multiple computers at home so that may be why

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Code using udp socket not working
« Reply #3 on: November 09, 2022, 02:28:04 pm »
Hard to say, but might well be that they're filtering certain UDP traffic. Depending on which two computers you've tried, that they're in separate networks.

Would be useful to run Wireshark to see what's actually going on, on the network, i.e. if the data is sent correctly and arrives but somehow fails on the application layer.

Just to try, you could also use a TcpSocket which is a bit more reliable and see if you manage to get a connection between the server and client.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Shinoken

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Code using udp socket not working
« Reply #4 on: November 09, 2022, 02:30:13 pm »
Next time i'll try pinging the two computers and send you the result, didn't think of trying even though they should be in the same network, also how would i go on about using wireshark?

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Code using udp socket not working
« Reply #5 on: November 09, 2022, 06:17:51 pm »
Hard to say, but might well be that they're filtering certain UDP traffic. Depending on which two computers you've tried, that they're in separate networks.
I've run into that at my work (college) in the past when teaching network programming.
The IT department decided to block all local UDP except a couple of ports for certain software. It took a while to get them to give me some unblocked ports (they gave me 1300-1399).

Also there were 2 different ethernet networks in the building that weren't routed to each other. My classroom had maybe 6 computers (including the front teacher PC) on one network, and the other 19 on the other network.


Shinoken

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Code using udp socket not working
« Reply #6 on: November 09, 2022, 09:52:26 pm »
I just asked two of my friends to test it and they both told me didnt work on their computers, pings worked though so it's some problem in my code

 

anything