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

Author Topic: UDP Failed to bind socket to port 45666  (Read 5540 times)

0 Members and 1 Guest are viewing this topic.

koker_007

  • Newbie
  • *
  • Posts: 7
    • View Profile
UDP Failed to bind socket to port 45666
« on: June 20, 2017, 06:52:33 pm »
Hello, problem with using a UDP socket. I have 2 devices, both in my application work as a receiver and as a sender. However, on one of them, errors constantly appear in the console window: "Failed to bind socket to port 45666".

Still "receive()" very often returns the status::error, but not always, sometimes it turns out to receive a message.

Function of reception in class
        void react_element() {
                send_new_data();

                sf::Packet packet_sf_in;

                sf::IpAddress ip_in("0.0.0.0");

                unsigned short port;
                sf::UdpSocket  packet_sf;

                packet_sf.bind(45666);

                packet_sf.setBlocking(0);


                for (GLushort num_try = 0 , num_packet_accept = 0; num_try < parametr.max_try_listen && num_packet_accept < parametr.max_accept_packet; num_try++) {
                        sf::Socket::Status status = sf::Socket::Status::NotReady;

                        status = packet_sf.receive( packet_sf_in, ip_in, port);

                        if (status == sf::Socket::Status::Done) {

                                num_packet_accept++;

                                if (sizeof(packet) == packet_sf_in.getDataSize()) {

                                        pack = ((packet*)packet_sf_in.getData());

                                        string ip_str;
                                        GLuint ip_int;
                                        ip_int = ip_in.toInteger();

                                        ip_str = int_ip_to_string_ip(ip_int);


                                        GLbyte ok_connect_ip = 0;
                                        GLushort num_connecion = 0;

                                        conection_data* data_pointer = 0;

                                        if (out_pack.its_new_connection_yn(ip_str)) {

                                                data_pointer = out_pack.create_new_connection(ip_str, 45666);
                                                cout << "New connection: " << ip_str << '\n';

                                        }
                                        else {

                                                data_pointer = out_pack.get_pointer_connection(ip_str);

                                        }


                                        if (data_pointer != 0) {
                                               
                                                if (data_pointer->its_new_in_packet_yn(pack->get_id_packet())) {
                                                       
                                                        Calculation_data_new_in_packet(pack, ip_str);
                                                       
                                                        pack = 0;
                                                }
                                                else {
                                                       

                                                }

                                        }


                                }
                                else {
                                        cout << "Socket in: BAD SIZE PACKET" << '\n';
                                }
                        }
                        else if (status == sf::Socket::Status::Disconnected) {
                                cout << "Socket in: Status::Disconnected " << '\n';
                        }
                        else if (status == sf::Socket::Status::Error) {
                                cout << "Socket in: Status::Error " << '\n';
                        }
                        else if (status == sf::Socket::Status::Partial) {
                                cout << "Socket in: Status::Partial " << '\n';
                        }

                }
        }
 


Parameters that are used in the same class
        packet* pack = 0;

        struct parameters{
               
                GLushort max_try_listen = 1000;
               
                GLushort max_accept_packet = 20;

                GLushort num_old_save_packet = 0;

        };
        parameters parametr;
 

There is very little written in the SFML documentation why an error occurs. Please explain more why such an error occurs and most importantly - how to solve it?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: UDP Failed to bind socket to port 45666
« Reply #1 on: June 20, 2017, 08:14:06 pm »
Are you able to bind your UDP socket to that port in a simpler application? You can try the "sockets" example of the SFML SDK, for example.
Laurent Gomila - SFML developer

koker_007

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: UDP Failed to bind socket to port 45666
« Reply #2 on: June 21, 2017, 05:33:34 am »
strange... I wrote following:

int main(void)
{
        sf::IpAddress ip_in("0.0.0.0");

        unsigned short port;
        cout << "Create packet end udp socket - next" << '\n';
        system("pause");
        sf::Packet packet_sf_in;
        sf::UdpSocket  packet_sf;


        cout << "bind port 45666 - next" << '\n';
        system("pause");
        packet_sf.bind(45666);

        cout << "Set blocking 0 - next" << '\n';
        system("pause");
        packet_sf.setBlocking(0);

        cout << "receive func - next" << '\n';
        system("pause");
        sf::Socket::Status status = sf::Socket::Status::NotReady;
        status = packet_sf.receive(packet_sf_in, ip_in, port);

        if (status == sf::Socket::Status::Done) {
                cout << "Status done" << '\n';
        }
        else if (status == sf::Socket::Status::Disconnected) {
                cout << "Status Disconnected" << '\n';
        }
        else if (status == sf::Socket::Status::Error) {
                cout << "Status Error" << '\n';
        }
        else if (status == sf::Socket::Status::Partial) {
                cout << "Status Partial" << '\n';
        }

        system("pause");

}
 

This time he did not give any error...
I checked correct inclusion of library in the debag version used by sfml-network-d-2.dll
So error is not here...
I used the sniffer to find out if port is busy with another program, but it is free.
also port 45666 is open.

I do not even know where the source of the this problem.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: UDP Failed to bind socket to port 45666
« Reply #3 on: June 21, 2017, 08:02:00 am »
You have one simple code that works, so now you must find what makes your original code not work.
Laurent Gomila - SFML developer

koker_007

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: UDP Failed to bind socket to port 45666
« Reply #4 on: June 21, 2017, 10:56:51 am »
Just tell what are the reasons for the error: "Failed to bind socket to port 45666"
please.

This error is registered somewhere inside the sfml and I would like to know what conditions for its appearance, so that it could be fixed.
I have several times rewritten the code that processed the data transfer but this error remains.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: UDP Failed to bind socket to port 45666
« Reply #5 on: June 21, 2017, 12:09:24 pm »
Quote
Just tell what are the reasons for the error: "Failed to bind socket to port 45666"
please.
The underlying OS function fails. That's all.
Laurent Gomila - SFML developer

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: UDP Failed to bind socket to port 45666
« Reply #6 on: June 22, 2017, 04:19:11 am »
Are you running both applications on the same computer?
Sorry if you answered this in your original post, I couldn't really get that from "I have 2 devices, both in my application work as a receiver and as a sender."

koker_007

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: UDP Failed to bind socket to port 45666
« Reply #7 on: June 22, 2017, 10:24:23 am »
Gleade, sorry for my English. I meant that my program both sends and receives packets on the UDP protocol. Of course, at least 2 devices are required to test the connection, in my case it is. However, an error occurs on one of the devices: "Failed to bind socket to port 45666"

Initially, when I met this error, I thought that it was due to the fact that this port is already being used on the device by some other program. But the sniffer showed that this is not true.
After checking the small (simple) SFML code, it turned out that it works, so the error is somewhere inside the logic my program. I most likely misuse SFML and I need to understand what conditions can cause this error so that I can fix it

Now I think that it arises from the fact that one and the same port is used both for sending and receiving messages, but this assumption causes me the following questions:
Why then does the error not appear on the first device, if the program works exactly the same?
Maybe my mistake is that UDP requires closing like TCP, but it's very strange and I'm in a confusion.

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: UDP Failed to bind socket to port 45666
« Reply #8 on: June 23, 2017, 12:54:37 am »
Now I think that it arises from the fact that one and the same port is used both for sending and receiving messages, but this assumption causes me the following questions:
Why then does the error not appear on the first device, if the program works exactly the same?
Maybe my mistake is that UDP requires closing like TCP, but it's very strange and I'm in a confusion.

Is it possible for you to produce complete and minimal code that replicates your problem? I might be able to help you out further. Your second question after your assumption regarding UDP requiring the closure of the TCP connection is wrong, they're both independent of each other. But it definitely sounds like there is something wrong in your logic and not the devices.

koker_007

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: UDP Failed to bind socket to port 45666
« Reply #9 on: June 28, 2017, 06:45:02 am »
Forgive me for waiting.
Unfortunately I could not find out which small part of the code is causing the error, so I decided to just cut out the entire block responsible for sending packets. Sorry that it did not turn out to be less code. Simply if you reduce it, the error disappears. For this reason, all the logic of sending and receiving is visible. Exactly the same as in my program. But all the logic of sending and receiving is visible, exactly as in my program.

http: / / www. filedropper.com/main_37

For work you will need more GLEW GLFW libraries. I know that SFML is able to create a window and draw with OpenGL but I want to use it only to transfer messages between devices.
Thanks for help.

 

anything