# Bind before connect

DevFeed: [Bind before connect](<https://devfeed.tech/articles/bind-before-connect-38934.md>)

Original publisher: [Read original article](<https://idea.popcount.org/2014-04-03-bind-before-connect>)

Author: Marek

Published: 2014-04-02T22:00:00Z

Content type: article

Language: en

Sources: [Marek Majkowski](<https://devfeed.tech/sources/marek-majkowski.md>)

Topics: [Linux Kernel](<https://devfeed.tech/topics/linux-kernel.md>), [ephemeral ports](<https://devfeed.tech/topics/ephemeral-ports.md>), [Kernel](<https://devfeed.tech/topics/kernel.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [article](<https://devfeed.tech/tags/article.md>), [automatically](<https://devfeed.tech/tags/automatically.md>), [bind](<https://devfeed.tech/tags/bind.md>), [bsd](<https://devfeed.tech/tags/bsd.md>), [call](<https://devfeed.tech/tags/call.md>), [connect](<https://devfeed.tech/tags/connect.md>), [ephemeral](<https://devfeed.tech/tags/ephemeral.md>), [ip](<https://devfeed.tech/tags/ip.md>), [kernel](<https://devfeed.tech/tags/kernel.md>), [linux](<https://devfeed.tech/tags/linux.md>), [linux-kernel](<https://devfeed.tech/tags/linux-kernel.md>), [number](<https://devfeed.tech/tags/number.md>), [operating-system](<https://devfeed.tech/tags/operating-system.md>), [port](<https://devfeed.tech/tags/port.md>), [range](<https://devfeed.tech/tags/range.md>), [socket](<https://devfeed.tech/tags/socket.md>), [source](<https://devfeed.tech/tags/source.md>), [system](<https://devfeed.tech/tags/system.md>), [tcp](<https://devfeed.tech/tags/tcp.md>)

## AI overview

Linux normally selects an ephemeral source port during `connect()`, allowing the same port to serve connections to different destinations; the excerpt says more than 28,000 simultaneous connections to one destination pair will fail by default. Binding a source IP first changes allocation: without reuse, a port is effectively tied up regardless of destination. Setting `SO_REUSEADDR` permits sharing across nonconflicting connections, including cases involving established or `TIME_WAIT` sockets, but a later `connect()` can still fail with `EADDRNOTAVAIL` if the chosen port conflicts, so retry may be needed. The excerpt recommends expanding the port range and using multiple source IPs for higher connection counts, and warns that `connect()` and bind-before-connect allocations interfere with each other. It distinguishes `SO_REUSEPORT` as primarily for distributing incoming traffic among sockets. These are the article's described Linux behaviors and caveats.

## Source excerpt

Bind before connect A TCP/IP connection is identified by a four element tuple: {source IP, source port, destination IP, destination port}. To establish a TCP/IP connection only a destination IP and port number are needed, the operating system automatically selects source IP and port. This article explains how the Linux kernel does the source port allocation. Ephemeral port range To establish a connection BSD API requires two steps: first you need to create a socket, then call on it.