Roger Lipscombe
Roger Lipscombe

Reputation: 91885

Is a non-blocking connect guaranteed to fail with EINPROGRESS?

If I set up a socket for non-blocking operation, as follows:

int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
int rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr));

...is connect guaranteed to fail with EINPROGRESS, or do I need to handle the case where it succeeds immediately?

Upvotes: 0

Views: 320

Answers (2)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136425

Not necessarily. Connecting to 127.0.0.1 may connect or fail immediately.

Upvotes: 3

user207421
user207421

Reputation: 310980

You need to handle the case where it succeeds immediately. That's why it returns 0 or -1. The documentation doesn't make any exception about that for non-blocking mode.

Upvotes: 2

Related Questions