- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm developing a TCP client using NicheStack.
Can the connect() for a TCP client be configured as "blocking" ? Currently, if no server is listening for the connection the connect() returns -1. So, I have to put the connect() in a loop with a 500 millisecond delay between connection attempts.Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's done when you create the socket and I believe it is blocking by default...in most network stacks. Double-check your socket settings.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, the socket is blocking by default.
In order to set non-blocking mode you must call: setsockopt(your_socket, SOL_SOCKET, SO_NONBLOCK, &max_socket, sizeof(int)); IIRC you could also set the default behaviour in Nichestack configuration file ipport.h- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for confirming that the connect() should block by default. For some unknown reason, in my case, it does not block.
I have tried to use setsockopt() to guarantee that the socket is setup as "blocking" but the connect() stills returns -1 if a server is not currently listening. By the way, my recv() call does block as expected. I'm going to continue to investigate why the connect() is not behaving as expected. I'm also going to check out the file "ipport.h".- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
After doing further investigation (using the debugger), the error causing the connect() to return -1 is ECONNREFUSED.
Of course, since there's no server listening for this client that would make sense but since the connect() should be blocked until the server is listening, why is the error ECONNREFUSED ?
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page