Transport Endpoint Is Already Connected
Production Risk
Programming error; track connection state to avoid redundant connect() calls.
EISCONN (errno 106) is returned when connect() is called on a socket that is already connected, or when sendto()/sendmsg() specifies a destination address on a connected socket.
- 1Calling connect() twice on the same socket
- 2Calling sendto() with a destination address on a connected UDP socket
Second connect() call on an already-connected socket.
connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)); // Try to connect again connect(sockfd, (struct sockaddr*)&addr2, sizeof(addr2)); // Returns -1, errno = EISCONN
expected output
connect: Transport endpoint is already connected (EISCONN)
Fix 1
Connect only once per socket lifetime
WHEN When managing socket lifecycle
// Track connection state
if (!connected) {
if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
connected = true;
}
}Why this works
A connected socket stays connected until closed; use a flag to avoid double-connect.
Fix 2
Use send() instead of sendto() on connected UDP
WHEN On a connected UDP socket
// After connect() on a UDP socket, use send() not sendto() connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)); send(sockfd, buf, len, 0); // OK // sendto() with a dest addr on a connected socket → EISCONN
Why this works
Connected UDP sockets use the address from connect(); sendto() with an explicit address conflicts.
Linux Programmer Manual connect(2)
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev