Archived

This topic is now archived and is closed to further replies.

Novalis

Using connect() in a non-blocking environment...

Recommended Posts

Novalis    122
Salutations... I''m coding a networking module right now and I''ve decided to make use of select() instead of setting my sockets to non-blocking mode. My app is not multi-threaded so it is imperative that none of my networking code blocks. My problem is that I don''t see any way to open the initial connection (I''m using SOCK_STREAM, not SOCK_UDP) without using the (normally) blocking connect() function. What''s the best way to deal with this? Must I set the socket to non-blocking to connect it?

Share this post


Link to post
Share on other sites
Fruny    1658
AFAIK

On a server : Have select() test the listening socket too. If it tells you that you can read/write (I don''t remember which) to it, then you can call accept().

On a client : Set it non-blocking using fcntl(). connect() will return an error and set errno to EINPROGRESS (other functions would set it to EWOULDBLOCK). To determine when the connection is finally complete, check for read/writeability with select().

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...

Share this post


Link to post
Share on other sites
Novalis    122
Fruny: You are correct on all accounts, but that''s not really the answer to my question

The question is: "Is there any alternative to setting the socket to non-blocking before calling connect()?"

Share this post


Link to post
Share on other sites
Fruny    1658
I''m afraid not. And I can''t find anything in my books either.

Maybe (just maybe), you can re-establish blocking once the connection is complete, though I have the feeling it wouldn''t work.

Plus, just checking in Unix Network Programming, vol1, p410, there are some issues to what select actually detects on a non-blocking connect : readable if success, readable and writeable if failure...

Yuck.

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...

Share this post


Link to post
Share on other sites
Novalis    122
Well, I don''t have any "official" books on hand, but the concensus of online FAQs and what have you seems to be that the non-blocking flag of a socket can be set and cleared at will.

Share this post


Link to post
Share on other sites