EPROTOTYPE
Linux / POSIXERRORCommonNetworkHIGH confidence

Protocol Wrong Type for Socket

Production Risk

Programming error; ensure client and server use the same socket type.

What this means

EPROTOTYPE (errno 91) is returned when a socket operation specifies a protocol that does not match the socket type. For example, connecting a SOCK_DGRAM socket to an address that requires SOCK_STREAM.

Why it happens
  1. 1Calling connect() on a SOCK_DGRAM socket to a TCP endpoint
  2. 2Specifying a protocol that is incompatible with the socket type
How to reproduce

connect() to a Unix socket path when the socket type does not match.

trigger — this will error
trigger — this will error
// Connecting SOCK_DGRAM to a SOCK_STREAM endpoint
int sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
// Returns -1, errno = EPROTOTYPE if server is SOCK_STREAM

expected output

connect: Protocol wrong type for socket (EPROTOTYPE)

Fix

Match socket type to the server

WHEN When EPROTOTYPE is returned on connect()

Match socket type to the server
// If server is SOCK_STREAM, create matching client socket
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));

Why this works

The client socket type must match the type the server socket was created with.

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