Click here to Skip to main content
15,877,857 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am facing a strange issue where my UDP socket that was earlier bound to a port is not unbinding from the port when I call closesocket(). The API call doesn't return any error but netstat command and TCPView app still show that port is still in use by my application.

What I have tried:

I tried creating the socket, binding, reading and closing on the same thread but still seeing the same issue.
Posted
Updated 20-Mar-22 18:57pm
v2

1 solution

const char on = 1;

int iResult = setsockopt(udp_socket, SOL_SOCKET, SO_REUSEADDR, &on, 
                sizeof(on));

if (iResult != NO_ERROR)
{
    std::cerr << "Can not enable REUSEADDR mode." 
        << " Error code: " << iResult << "\n";

    closesocket(udp_socket);
    return INVALID_SOCKET;
}

// now bind socket to address
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900