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

Author Topic: [SOLVED] why doesn't sf::udpSocket::anyport work here?  (Read 4166 times)

0 Members and 2 Guests are viewing this topic.

C_Worm

  • Newbie
  • *
  • Posts: 18
    • View Profile
[SOLVED] why doesn't sf::udpSocket::anyport work here?
« on: April 10, 2019, 04:27:33 pm »
        char avsluta = 'n';
        char serverOrClient = '0';
        char buffer[200] = "";
        std::string message = "";
        sf::IpAddress IP = sf::IpAddress::getLocalAddress();
        unsigned short serverPort = sf::UdpSocket::AnyPort;
        unsigned short clientPort = sf::UdpSocket::AnyPort;[/glow]

        std::cout << "Your IP-address: " << IP << std::endl;

        std::cout << "Server (s) or client (c)?: ";
        std::cin >> serverOrClient;


        bool done = false;

        while (!done)
        {
                if (serverOrClient == 's')
                {
                        sf::UdpSocket socket;
                        if (socket.bind(serverPort) != sf::Socket::Done)
                        {
                                std::cout << "server kunde inte binda till port\n\n";
                        }
                        else
                        {
                                std::cout << "Server -  " << "port: " << socket.getLocalPort() << " IP-address: " << sf::IpAddress::getLocalAddress() << "\n\n";
                               
                        }
                        std::cout << "\n\nSend message: ";
                        std::cin >> buffer;

                        if (socket.send(buffer, sizeof(buffer), IP, clientPort) != sf::Socket::Done)
                        {
                                std::cout << "Kunde inte senda! \n\n";
                        }
                        else
                        {
                                std::cout << "Message sent: '" << buffer << "' to client at port: " << clientPort << "\n\n";
                        }
                       
                }
                else if (serverOrClient == 'c')
                {
                        sf::UdpSocket socket;
                        if (socket.bind(clientPort) != sf::Socket::Done)
                        {
                                std::cout << "Kunde inte binda! \n\n";
                        }
                        else
                        {
                                std::cout << "Client - " << "port: " << socket.getLocalPort() << " IP-address: " << sf::IpAddress::getLocalAddress() << "\n\n";
                        }

                        std::size_t received;
                        if (socket.receive(buffer, sizeof(buffer), received, IP, serverPort) != sf::Socket::Done)
                        {
                                std::cout << "kunde inte receiva! \n\n";
                        }
                        else
                        {
                                std::cout << "Sender: " << IP << " ||  Port : " << serverPort << std::endl;
                                std::cout << "meddelande mottaget: " << buffer << std::endl;
                        }


                }


                std::cout << "avsluta? j/n: ";
                std::cin >> avsluta;
                if (avsluta == 'j')
                {
                        done = true;
                }
        }
 

Hello! if i assign clientPort and serverPort manually with values like:

   unsigned short serverPort = 4000;
   unsigned short clientPort = 4001;


this code manages to send information from server to client.

but when i do this:

   unsigned short serverPort = sf::UdpSocket::AnyPort;
   unsigned short clientPort = sf::UdpSocket::AnyPort;


The server sends to port 0 and not the actuall clientPort.
So no message is reveived by the client.

why is this?
« Last Edit: April 10, 2019, 11:02:29 pm by C_Worm »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: why doesn't sf::udpSocket::anyport work here?
« Reply #1 on: April 10, 2019, 04:47:22 pm »
Please use [code=cpp][/code] tags when posting code.

Do you understand what UDP Broadcasting is?
Do you know what AnyPort does?

Why do you expect it to work with AnyPort?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

C_Worm

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: why doesn't sf::udpSocket::anyport work here?
« Reply #2 on: April 10, 2019, 05:06:13 pm »
in this example i guess sf::UdpSocket::Anyport generates a port that's not occupied and assigns it to "port".

and that's what it does because the program displays the port with getlocalport().

the thing is that inside let's call it "server mode":

if (socket.send(buffer, sizeof(buffer), IP, clientPort) != sf::Socket::Done)

here clientPort == 0;

but in "client mode":

if (socket.bind(clientPort) != sf::Socket::Done)

here clientPort contains the port that was assigned with sf::UdpSocket::Anyport;


but when i hard code the ports with a value, like:

 unsigned short clientPort = 4001;

clientPort IS assigned 4001 in both "server mode" AND"client mode".



since i don't know what port would be availabe on some other computer, I thought it would be a good idea to let the program find a free port by itself rather than me hardcoding a port value...

please let me know if im thinking wrong here :)
« Last Edit: April 10, 2019, 05:09:32 pm by C_Worm »

C_Worm

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: why doesn't sf::udpSocket::anyport work here?
« Reply #3 on: April 10, 2019, 11:00:27 pm »
I think i solved myself.

when you open up two different programs, sf::UdpSocket::Anyport will generate two different ports for:

unsigned short clientPort;

thus the "client" will not receive the information sent from the server.

On the other hand when I assign a predetermined value for the port like:

unsigned short clientPort = 4001;

the clienPort will be the same in both programs running and therefore the "client will receive the information!

:)