Forums » Community Projects

Proper check for socket connection success

Jul 13, 2008 mr_spuck link
What is the proper platform independent way to check if a connection via TCPSocket succeeded? Without using timeouts.

I've thrown together a small test program to check out the possibilities.
http://home.arcor.de/famscheffler/ven/stest.lua

It just calls a couple of functions in the connect callback.
GetSocketError() would be the proper way under linux, but that doesn't work under windows.
Send() crashes the game under linux. So it's not an option.
Recv() would work under windows since it returns a "no connection" error but not under linux.
GetPeerName() seems to work everywhere .. if connected it returns the host otherwise nil. Not sure if that's the correct way though.

Until now I've used the fact that the read handler is called directly after a connection failed. Which is basically the effect of removing all the GetSocketError()'s from tcpsock.lua. But that only seems to work under linux and and has some ugly side effects.

EDIT:
actually.. seems to be the combination of other functions and Send on an unconnected socket that crashes under linux .. funky!