A non-blocking connection is performed. Under windows, the moment of installing a socket can be defined using select, if the socket is writable, then the connection is established. And how under linux? After initialization, it immediately becomes writable. And accordingly, an attempt to write something to the socket will call SIGPIPE.
int sockfd = ::socket(AF_INET, SOCK_STREAM, 0); int arg = ::fcntl(sockfd, F_GETFL); long flags = arg & ~O_NONBLOCK; flags |= O_NONBLOCK; fcntl(sockfd, F_SETFL, flags); struct sockaddr_in dest; dest.sin_family = AF_INET; dest.sin_port = htons(1111); inet_aton("127.0.0.1", &dest.sin_addr); int rc = ::connect(sockfd, (struct sockaddr* )&dest, sizeof(dest)); fd_set fdWrite; FD_ZERO(&fdWrite); FD_SET(sockfd, &fdWrite); struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; rc = ::select(int(sockfd) + 1, 0, &fdWrite, 0, &tv); int er = errno; return 0;