A socket connection is uniquely identified by a combination of its local IP:Port and remote IP:Port. Multiple apps can be bound to the same local IP:Port as long as they are each connected to a different remote IP:Port.
If a local IP:Port is already bound for listening (bind()
and listen()
have been called for it), other sockets can still bind()
to that same local IP:Port but only if the SO_REUSEADDR
(and on some platforms, SO_REUSEPORT
) socket option is used. Otherwise, the bind()
fails with an "already in use" error.
When multiple client sockets connect()
to the same remote IP:Port, a local binding is typically not specified, which allows connect()
to perform an implicit bind()
to a random available local IP:Port to avoid conflicts with other connections. If bind()
is explicitly called and succeeds, and then connect()
is called to connect to a remote IP:Port that is already connected to the local IP:Port, connect()
will fail.