EISCONN
Linux / POSIXERRORCommonNetworkHIGH confidence

Transport Endpoint Is Already Connected

Production Risk

Programming error; track connection state to avoid redundant connect() calls.

What this means

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.

Why it happens
  1. 1Calling connect() twice on the same socket
  2. 2Calling sendto() with a destination address on a connected UDP socket
How to reproduce

Second connect() call on an already-connected socket.

trigger — this will error
trigger — this will error
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

Connect only once per socket lifetime
// 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

Use send() instead of sendto() on connected UDP
// 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.

Sources
Official documentation ↗

Linux Programmer Manual connect(2)

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Linux / POSIX errors