My program is able to connect to a remote server via a socket and it can send data across.
Upon disconnection (or if Ethernet cable is plugged in, after the device powering) I get this exception:
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
In this case, I frequently try to re-establish the connection by creating a new socket and reinitialising it, but the program is stopped in socket.connect(...) line after plugging in the Ethernet cable.
Would you please help me solve the problem?
Here is the code I wrote:
static Socket Connect(string host, int timeout, int port_num) { Socket connection; IPHostEntry hostEntry = Dns.GetHostEntry(host); // Look up host’s domain name to find IP address(es) IPAddress hostAddress = hostEntry.AddressList[0]; // Extract a returned address IPEndPoint remoteEndPoint = new IPEndPoint(hostAddress, port_num); while (true) { try { //Connect connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); connection.Connect(remoteEndPoint); connection.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); connection.SendTimeout = timeout; Debug.Print("Connection stablished"); break; } catch (SocketException e) { Debug.Print("Error Code: " + e.ErrorCode); Debug.GC(true); } } Debug.GC(true); return connection; }