SFML community forums

Help => Network => Topic started by: C_Worm on April 10, 2019, 04:27:33 pm

Title: [SOLVED] why doesn't sf::udpSocket::anyport work here?
Post by: C_Worm 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?
Title: Re: why doesn't sf::udpSocket::anyport work here?
Post by: eXpl0it3r 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?
Title: Re: why doesn't sf::udpSocket::anyport work here?
Post by: C_Worm 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 :)
Title: Re: why doesn't sf::udpSocket::anyport work here?
Post by: C_Worm 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!

:)