Hard
bind() to a source address before connect() under connection churn
A Linux client opens short-lived TCP connections to 40 backend addresses. With TIME_WAIT included, about 30,000 of its sockets exist at any moment, roughly 750 per backend. ip_local_port_range is the default 32768-60999. With a plain connect() this ran for months. After the bind() below was added to pin the source address to the exchange-facing NIC, bind() starts failing with EADDRINUSE. Why does adding it change anything?
main.cpp
int fd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in src{};
src.sin_family = AF_INET;
src.sin_addr.s_addr = inet_addr("10.20.0.15"); // exchange-facing NIC
src.sin_port = 0; // let the kernel pick
bind(fd, (sockaddr*)&src, sizeof(src)); // now fails: EADDRINUSE
connect(fd, (sockaddr*)&dst, sizeof(dst));43 of 45 in Networking