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?