Jump to content
  • Advertisement
Sign in to follow this  
CodaKiller

Wait for connection to be accepted on an Async socket.

This topic is 3337 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

I need to check if connect was accepted or rejected before continuing so that I can return a null pointer from my function if it is rejected. Here is my function:
E_SOCKET* lua_create_server( std::string ip )
{
	sockaddr_in server_address;
	server_address.sin_family = AF_INET;
	server_address.sin_port = htons(5001);
	server_address.sin_addr.s_addr = inet_addr(ip.c_str());
	E_SOCKET* socket = NULL;
	if( connect(g_TCP_Send_Socket, (struct sockaddr *)&server_address, sizeof(server_address)) == 0)
	{
		socket = new E_SOCKET;
		socket->address = ip;
		socket->port = 5001;
	}
	return socket;
}


I thought checking if an error is returned would work but it appears that the function does not wait for the connection to be accepted before continuing, how can I do this manually?

Share this post


Link to post
Share on other sites
Advertisement
You can call connect() in a loop, until it either returns success, or sets errno to something other than EWOULDBLOCK/EAGAIN.

You could also use select() to wait for a connection on the socket, or take the socket out of non-blocking mode (which isn't gaining you anything here to start with), and just call connect once.

TBH, these questions you are asking are all *really* basic, and are covered by every book and tutorial out there, not to mention the man pages for each function. I would really recommend that you sit down with something like Beej's guide to Networking, and solve all your issues once and for all.

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
You can call connect() in a loop, until it either returns success, or sets errno to something other than EWOULDBLOCK/EAGAIN.

You could also use select() to wait for a connection on the socket, or take the socket out of non-blocking mode (which isn't gaining you anything here to start with), and just call connect once.

TBH, these questions you are asking are all *really* basic, and are covered by every book and tutorial out there, not to mention the man pages for each function. I would really recommend that you sit down with something like Beej's guide to Networking, and solve all your issues once and for all.


I forgot to add that this is on an async socket, I was able to get it to work by repeatedly calling connect but then I hit another problem.

I have to accept the connection to get the IP and if I don't want it I just close the socket, this is all well and good but it also makes my function think that the connection was accepted... Grrrr...

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!