TInyClr.Networking detect if Socket.Connect times out or connects

I see that there is not a Socket.Connected property in TinyClr.Networking as there is in .net core, without it, how does one detect if the socket is connected after Socket.Connect or if it has timed out?

I have an intermittently available server so I would like to do something equivalent to:

while(true)
{
mySocket.Connect(remoteEndpoint);
if(mySocket.Connected)
{
break;
}
else
{
mySocket.Close();
}
}

Agree better if we have property. But for now, you can inherit from socket class and define your connect method. Something like:

public bool Connected = false;

public void Connnect(.....) {
      Connected  = false;
      base.Connect(.....);
      Connected  = true;
}

socket connect will throw an exception if could not connect, so Connected is not true if connect failed.

while (true)
{
try
{
syncSocket.Connect(remoteEndpoint);
break;
}
catch
{

            }
        }

If that were true then this should work but the exception never occurs, the Connect falls through and the loop exits.

Doesn’t work in the subclass either… after timeout, no exception…

I noticed that connect happens in a a few hundred milliseconds if a server is listening so tried to connect on timed thread but got exception in Socket::Connect

Hi, can I understand that you are OK with the solution above, no need property for now?

no - the timed thread solution does not work

I DID get timed thread to work! I was originally running my main loop in a secondary thread. When I moved it to the main thread, it started working !!! … Here is the final code:

PS… the worker instantiation should be ahead of the while loop…

I see socket timeout default is “Infinite”, try to set socket timeout then you don’t need your thread, I guess.

…will give it a try… thanks for all of your help!

TinyClr is awesome. So is the Fez Duino… Really fun (and fast!) to develop embedded in the very robust Visual Studio tool chain. Hard to beat native debugging, nuGet, GitHub repositories + change management, intellicode and all the rest of the the other available goodies …

3 Likes

Awesome! We are glad you like it.