关于UNIX域套接字SOCK_STREAM模式下connect()报错106的技术咨询
Hey there, let's break down this error—it's error code 106, which maps to EISCONN on most Unix-like systems. This pops up when you try to call connect() on a socket that’s already successfully linked to a peer. Here are the most common causes and straightforward fixes:
Common Causes & Solutions
1. Accidental repeated connect() calls in the client
This is the #1 culprit. If your client code calls connect() more than once on the same socket file descriptor after a successful connection, you’ll hit this error every single time.
For example, a loop that checks connection status but fails to track whether you’re already connected:
// Bad example: unguarded repeated connect calls int sockfd = socket(AF_UNIX, SOCK_STREAM, 0); struct sockaddr_un server_addr; // ... initialize server_addr details ... // This loop will keep calling connect even after success while (1) { if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { perror("connect failed"); } // Client work here }
Fix: Add a simple flag to track connection state, so connect() only runs when the socket is unconnected:
// Better example: track connection status int sockfd = socket(AF_UNIX, SOCK_STREAM, 0); struct sockaddr_un server_addr; // ... initialize server_addr details ... int is_connected = 0; while (1) { if (!is_connected) { if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == 0) { is_connected = 1; printf("Connected to server!\n"); } else { perror("Connect failed, retrying in 1s..."); sleep(1); } } // Do client tasks (send/receive data) }
2. Reusing an already-connected socket file descriptor
If you close a socket but don’t properly reset the file descriptor (or accidentally reuse it before creating a new socket), you might end up calling connect() on a descriptor still tied to an active connection.
Fix: Always close the socket with close(sockfd) when done, mark the descriptor as invalid (set to -1), and create a new socket before attempting to reconnect:
// Proper reconnection flow int sockfd = -1; struct sockaddr_un server_addr; // ... initialize server_addr details ... // When you need to connect/reconnect: if (sockfd != -1) { close(sockfd); sockfd = -1; } sockfd = socket(AF_UNIX, SOCK_STREAM, 0); if (sockfd == -1) { perror("Failed to create socket"); exit(EXIT_FAILURE); } if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { perror("Connect failed"); close(sockfd); sockfd = -1; }
3. Edge case: Server-side connection mishandling
Rarely, this can happen if the server accepts a connection but immediately closes it, and the client tries to reconnect without resetting its socket. Double-check that your server’s accept() logic isn’t closing accepted sockets unintentionally (unless that’s part of your design).
Quick Debugging Tips
- Add debug prints right before every
connect()call to log the socket descriptor value and your expected connection state. - Use
ss -x(ornetstat -xon older systems) to list active Unix domain socket connections—this lets you confirm if the socket is already connected before yourconnect()attempt.
内容的提问来源于stack exchange,提问作者user9365205

