Solved A problem with bind

I am writing a program that listens on a socket. Today I introduced quite a few changes and a problem popped up which it was not there yesterday.

If after closing the program I start it immediately I get error 48 (address already in use) on bind. I am at a loss as to what changes could bring this disaster upon me but in the meantime noticed something in the documentation:

bind(2)

Binding an address in the UNIX domain creates a socket in the file system that must be deleted by the caller when it is no longer needed (using unlink(2)).

I do not do that. Can that be the ultimate cause of the problem? I am asking this because to be honest I do not fancy deleting files which I didn't create. After all the nuisance lasts only about a minute.

-
 
Use netstat to debug what or who is using the socket.

The fact that it only lasts for ~1 minute means that someone somewhere has failed to close the socket correctly.

bind(2)
Binding an address in the UNIX domain creates a socket in the file system that must be deleted by the caller when it is no longer needed (using unlink(2)).-
That only applies to Unix domain sockets, not to TCP/IP sockets. Unix domain sockets are a bit unusual, and most people don't use them these days.
 
Use netstat to debug what or who is using the socket.

Ok! This is what it says:

Code:
Proto Recv-Q Send-Q Local Address          Foreign Address        (state)
tcp4       0      0 192.168.1.17.http-alt  192.168.1.13.58474     TIME_WAIT
tcp4       0      0 192.168.1.17.http-alt  192.168.1.13.58473     TIME_WAIT

The fact that it only lasts for ~1 minute means that someone somewhere has failed to close the socket correctly.

I visit 192.168.1.17 from Edge on 192.168.1.13. It opens two connections and requests favicon.ico on one of those. The root page it has cached. The program returns 404. At this point I press Enter and the program closes the open connections. The relevant three lines:

Code:
printf("%x : Closing connection\n", Socket);
if (-1 == shutdown(Socket, SHUT_RDWR)) printf("%x : shutdown failed: %d\n", Socket, errno);
if (-1 == close(Socket)) printf("%x : close failed: %d\n", Socket, errno);

After that it closes the listening socket:

Code:
printf("Closing ListenSocket\n");
if (-1 == close(g_ListenSocket)) printf("close failed on ListenSocket: %d\n", errno);

Now the output:

Code:
Service is starting...
3 : Connection event
5 : Socket registered
3 : Connection event
6 : Socket registered
5 : Read event
5 : Reading Job started
5 : Bytes received: 401
GET /favicon.ico HTTP/1.1

Host: 192.168.1.17:8080
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0
Accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Referer: http://192.168.1.17:8080/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,ru;q=0.8


Path: /favicon.ico
5 : Sent 404

Service is closing...
Cancellation event
5 : Closing connection
6 : Closing connection
Closing ListenSocket

I do not see anything left unattended. Yet the problem is there.

If I first close the browser and only then close the program there's no problem. The output in this case:

Code:
Path: /favicon.ico
5 : Sent 404
6 : Read event
5 : Read event
6 : Shutting down early...
5 : Shutting down early...

Service is closing...
Cancellation event
Closing ListenSocket

That only applies to Unix domain sockets, not to TCP/IP sockets. Unix domain sockets are a bit unusual, and most people don't use them these days.

Thanks for that.
 
Back
Top