ENOTSOCK
Linux / POSIXERRORCommonNetworkHIGH confidence
Socket Operation on Non-Socket
Production Risk
Programming error; usually a wrong-variable or scope bug.
What this means
ENOTSOCK (errno 88) is returned when a socket operation (bind, connect, accept, send, recv, etc.) is called on a file descriptor that is not a socket.
Why it happens
- 1Passing a regular file fd to connect(), bind(), or recv()
- 2Using a closed socket fd that was reused for another file type
- 3Wrong variable holding the fd — off-by-one or scope error
How to reproduce
Calling recv() on a file descriptor opened as a regular file.
trigger — this will error
trigger — this will error
int fd = open("/tmp/myfile", O_RDWR);
// Mistakenly used fd where sockfd is expected
recv(fd, buf, sizeof(buf), 0);
// Returns -1, errno = ENOTSOCKexpected output
recv: Socket operation on non-socket (ENOTSOCK)
Fix
Verify the fd is a socket
WHEN When debugging ENOTSOCK
Verify the fd is a socket
// Check if fd is a socket
struct stat st;
fstat(sockfd, &st);
if (!S_ISSOCK(st.st_mode)) {
// sockfd is not a socket — trace how it was opened
}Why this works
S_ISSOCK() checks the file type; use fstat() to diagnose which fd was passed incorrectly.
What not to do
✕ Reuse the same variable name for both socket and file fds
This is a common source of ENOTSOCK — use distinct names like sockfd and filefd.
Sources
Official documentation ↗
Linux Programmer Manual recv(2)
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev