Skip to content
Advertisement

Attempt to re-bind an already closed listening socket is failing (EADDRINUSE)?

The following is the essence of my test fixture –

SetUp()
{
    g_listen_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    /* localhost is the server */
    bind(g_listen_sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
    listen(g_listen_sock, max_connections);
}

testcase()
{
    hdl = accept(g_listen_sock, NULL, NULL);
    -- send()/recv() data on the socket --
}

TearDown()
{
    shutdown(g_listen_sock, SHUT_RDWR);
    close(g_listen_sock);
    g_listen_sock = INVALID_SOCKET;
}

In the normal use of the application the listening socket is bound only once during the lifetime of the application, however the test setup repeatedly opens and closes the listening socket. The first iteration of the testcase works fine but subsequent iterations fail at the bind() call with errno == 98 i.e. EADDRINUSE.

How do I avoid this situation? The solution ideally wouldn’t require me to have a separate test version of the code, for e.g. using SO_REUSEADDR while testing.

P.S. – the same code works fine on Windows, the bind() failure happens on Linux.

Advertisement

Answer

What you are trying to get around is a built-in functionality of the TCP layer of networking. The linux kernel will not allow you to rebind that socket because the closed socket will be in the TIME_WAIT state. There is nothing you can do to circumvent that besides using SO_REUSEADDR (as you have already pointed out), or by using a different port for each test which it doesn’t sound like you want to do either.

Unfortunately, TCP was not designed to close and open the same IP/port multiple times in a row for testing, so you will have to pick your poison if you still want to do this kind of testing.

Also see this answer for a more in-depth exploration of your problem.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement