So my device will constantly be awaiting a socket connection using the following code:
X509Certificate cert = new X509Certificate(Resources.GetBytes(Resources.BinaryResources.certificate), password);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, relayPort);
server.Bind(localEndPoint);
server.Listen(5);
while (true)
{
try
{
// accept client connections
Debug.Print("wait for Connect");
Socket Client = server.Accept();
SslStream s = new SslStream(Client);
s.AuthenticateAsServer(cert, SslVerification.NoVerification, SslProtocols.Default);
byte[] receivedFromClient = ReceiveClientData(Client, s);
String receivebuffer = new string(Toolbox.NETMF.Tools.Bytes2Chars(receivedFromClient));
if (receivebuffer.Length > 0 && receivebuffer.Equals("something"))
{
do something...
}
if (receivebuffer.Length > 0 && receivebuffer.Equals("something else"))
{
do something else...
}
Debug.Print(receivebuffer);
s.Write(ConvertStringForMessage("respond"), 0, ConvertStringForMessage("respond").Length);
s.Dispose();
Client.Close();
}
catch(Exception ex)
{
Debug.Print(ex.Message);
break;
}
}
Debug.Print("Relay Thread Ending");
Now when I simulate a network outage the socket errors as expected:
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (10) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::poll [IP: 0000] ####
#### System.Net.Sockets.Socket::Poll [IP: 0011] ####
#### System.Net.Sockets.Socket::Accept [IP: 0017] ####
#### SocketException ErrorCode = 10050
#### SocketException ErrorCode = 10050
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10050
#### SocketException ErrorCode = 10050
Exception was thrown: System.Net.Sockets.SocketException
The question is…what is the correct way to handle a network outage for this socket… Then recreating the socket connection with the same function?