Jump to content
  • Advertisement
Sign in to follow this  
jeff8j

c tcp socket connect async?

This topic is 2136 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way to use a c socket or some lowlevel lowlevel alternative to connect asynchronously?

I can read and write asynchronously by using select but my code has to wait several seconds in some situations for connect to complete

I know I can setup a thread to handle the connection and do what I need but I was hoping for a built in system way is there one?

 

I think I can set the socket to non blocking but then how do I know if its connected and if it failed?

 

I think the hold up is the gethostbyname part so I need help with both of those

Edited by jeff8j

Share this post


Link to post
Share on other sites
Advertisement

The holdup should be in both. Both combined could take minutes to complete depending on settings.

Asynch connect is complicated, hardly well documented, and suffers from difference between major OSes and even their distributions.

 

It is in most cases unwanted on the client-side. You should consider just running a thread.

It is not hard to implement sockets, but there are so many pitfalls, especially regarding errors.

 

Alternatively, you could use a socket library, such as boost::asio or QT networking.

 

For asynch connect, the socket needs to be nonblocking before connecting. The connect should return immediately.

For windows this process is easier with ConnectEx and its counterparts. On Linux it is slightly more complicated.

See:

http://stackoverflow.com/questions/10187347/async-connect-and-disconnect-with-epoll-linux

http://stackoverflow.com/questions/2875002/non-blocking-tcp-connect-with-epoll

Share this post


Link to post
Share on other sites

Thanks Kaptein im trying to keep away from any libraries for ease of licencing and distribution across patforms so im going to try the ConnectEx and non blocking sockets

 

I found async dns code here which looks good http://adns.sourceforge.net/

but now my question is how do I know if I should lookup ipv6 aaaa records or ipv4 a records?

I wouldnt want to lookup a ipv6 and fallback to ipv4 that could take twice as long so does the operating system know which to use I assume it would so the best way would be to ask the os somehow which one to use?

I currently only do ipv4 but while im in there messing with things might as well try to add ipv6 

Share this post


Link to post
Share on other sites
If you create the socket as non blocking:
 
socket(..., SOCK_NONBLOCK, ...)
Then you can use select(2) or poll(2) to check if the connect is finished (ignoring error / success for now).

Now, you can check the status of the socket to see if it is successful or failed with getsockopt(2).

getsockopt(fd, SOL_SOCKET, SO_ERROR, ...)
This example is for BSD sockets (grabbed from my current project) but Winsock should be very similar. Edited by Karsten_

Share this post


Link to post
Share on other sites

connect() can also return errors, as well as the error fd from select(). getsockopt() returns some errors as well, on all OSes.

I don't think I've ever written IPv6 implementation, so I can't help you there. You will need to write multiplatform resolver since resolving on Win vs Linux is actually quite different.

 

See:

https://github.com/fwsGonzo/cppcraft/tree/master/source/library/network

 

That implementation doesn't use asynch connect. Instead I'm using threads, and it simplifies things greatly.

It has been tested, but not well enough for me to think of it as "working as intended". It also has some missing functionality. I also know it doesn't identify all the errors that doesn't mean you connection has been lost, as well as those that doesn't mean operations failed.

I know this because the server guy wrote a client library so that all the networking is managed by him. I noticed some differences in how we interpreted certain errors.

 

Sockets are generally a complete mess of old stuff that shouldn't be there anymore, as well as incomplete and ambigous documentation. Not to mention Linux, father of 'errno.' Sprinkle some differences between distributions and OSes and you have the current version of cross platform networking. Some amalgamation of versions of BSD sockets sprinkled with religion.

Share this post


Link to post
Share on other sites

I suggest using a thread for this, since doing it asycronously will just complicate things even more.imo.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!