Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could connect() fail and set errno to EADDRINUSE

Tags:

c

posix

sockets

I'm writing some critical code and need at one point to establish a TCP connection using connect().

I dig into POSIX manpages to ensure that my code is fully POSIX compliant, and have to write unit tests covering all possible error cases.

Considering this document, I fail to understand what the following means:

EADDRINUSE Attempt to establish a connection that uses addresses that are already in use.

At first sight I thought I could be triggered by the lack of ephemeral port, but this error is reported by EADDRNOTAVAIL (which I successfully triggered in an unit test).

Concretely, what does it mean? Which condition could lead to this error? I understand the semantic for this error on bind(), but how could connect() raise that?

like image 328
Etienne Doms Avatar asked Apr 04 '17 05:04

Etienne Doms


1 Answers

My feeling, though this is guesswork, is that this error is for non-TCP sockets, specifically unix sockets - AF_UNIX (also known as AF_LOCAL)

The linux connect() mangpage puts it just slightly differently than the one you linked:

EADDRINUSE Local address is already in use.

The usage of the word "Local" led me to the unix manpage which states

EADDRINUSE The specified local address is already in use or the filesystem socket object already exists.

like image 59
e.dan Avatar answered Nov 14 '22 23:11

e.dan