ENOTCONN
Linux / POSIXERRORCommonNetworkHIGH confidence

Transport Endpoint Is Not Connected

Production Risk

Programming error; always verify connection before data operations.

What this means

ENOTCONN (errno 107) is returned when a send, receive, or shutdown operation is attempted on a socket that has not been connected yet.

Why it happens
  1. 1Calling send() or recv() before connect() completes
  2. 2Calling shutdown() on an unconnected socket
  3. 3getpeername() on an unconnected socket
How to reproduce

recv() before connect() is called.

trigger — this will error
trigger — this will error
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
// Attempting recv without connecting first
recv(sockfd, buf, sizeof(buf), 0);
// Returns -1, errno = ENOTCONN

expected output

recv: Transport endpoint is not connected (ENOTCONN)

Fix

Connect before sending or receiving

WHEN Using a connection-oriented socket

Connect before sending or receiving
// Connect first, then communicate
if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
  recv(sockfd, buf, sizeof(buf), 0);
}

Why this works

TCP sockets must be connected (or accepted from a listener) before data can flow.

Sources
Official documentation ↗

Linux Programmer Manual recv(2)

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

← All Linux / POSIX errors