Hi all,
I’m having problems with network sockets. After about 25 data requests from my pc to the cobra (over the network), the cobra starts throwing exceptions (see below). Can anyone tell me what I might be doing wrong?
using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Microsoft.SPOT;
namespace HAMSCobra
{
#region EventHandler
public delegate void MessageEventHandler(Object sender, SocketCommunicationEventArgs nea);
/// <summary>
/// EventArgs for the SocketCommunicationEventHandler
/// </summary>
public class SocketCommunicationEventArgs : EventArgs
{
private string _receivedMessage;
public SocketCommunicationEventArgs(string receivedMessage)
{
_receivedMessage = receivedMessage;
}
public string ReceivedMessage
{
get { return _receivedMessage; }
}
}
#endregion
#region CommunicationHandler
class SocketCommunicationHandler
{
private string data = null;
private static byte[] bytes = null;
private static int _clientPort;
private static int _listenPort;
private static IPAddress _ipAddress = IPAddress.Parse("10.0.10.123");
private static IPAddress _domoticaServerIP = IPAddress.Parse("10.0.10.65");
private const char end = ';';
private static bool started = false;
private int x = 1;
private Socket sListener = null;
private Socket sHandler;
public event MessageEventHandler MessageEvent;
public void onMessageEvent(string news)
{
if (MessageEvent != null)
{
var scea = new SocketCommunicationEventArgs(news);
MessageEvent(this, scea);
}
}
/// <summary>
/// Start the SocketCommunicationHandler Thread
/// </summary>
/// <param name="ClientPort">Port used to send message to</param>
/// <param name="ListenPort">Port used to listen for incomming messages</param>
public SocketCommunicationHandler(int clientPort = 9999, int listenPort = 9998)
{
_clientPort = clientPort;
_listenPort = listenPort;
if (!started)
{
new Thread(StartListeningServer).Start();
new Thread(StartListening).Start();
started = true;
}
}
private void StartListeningServer()
{
sListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEndPoint = new IPEndPoint(_ipAddress, _listenPort);
sListener.Bind(localEndPoint);
sListener.Listen(10);
}
/// <summary>
/// Start listening for incomming socket messages
/// </summary>
private void StartListening()
{
try
{
while (true)
{
try
{
using (sHandler = sListener.Accept())
{
data = null;
bytes = new Byte[1024];
while (true)
{
int bytesRec = sHandler.Receive(bytes);
data = new string(Encoding.UTF8.GetChars(bytes));
// Look for the end of the message (must end with ;)
if (data.IndexOf(end) > -1)
{
break;
}
}
// Fire and event containing the received message
Debug.Print("Text received : " + x + " --> " + data);
x++;
onMessageEvent(data);
}
}
catch (SocketException e)
{
Debug.Print("!!!!!!!! sHandler Error !!!!!!!!");
Debug.Print(e.Message.ToString());
}
finally
{
bytes = null;
sHandler.Close();
sHandler = null;
}
}
}
catch (SocketException e)
{
Debug.Print("!!!!!!!!!!!!!!!! StarListening Socket Error !!!!!!!!!!!!!!!");
Debug.Print(e.Message.ToString());
}
}
/// <summary>
/// Send a message to the other side
/// </summary>
/// <param name="msg">The message to send</param>
public bool send(string msg)
{
IPEndPoint hostep = new IPEndPoint(_domoticaServerIP, _clientPort);
using (Socket sSender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
try
{
sSender.Connect(hostep);
}
catch (SocketException z)
{
Debug.Print("Problem connecting to host");
Debug.Print(z.Message.ToString());
sSender.Close();
return false;
}
try
{
byte[] buf = Encoding.UTF8.GetBytes(msg.Length.ToString() + "," + DateTime.Now.ToString() + "," + msg + ";");
sSender.Send(buf, 0, buf.Length, SocketFlags.None);
}
catch (SocketException x)
{
Debug.Print("Problem sending data");
Debug.Print(x.Message.ToString());
sSender.Close();
return false;
}
sSender.Close();
return true;
}
}
}
#endregion
}
the error after 25 attempts:
Event received: temp;
Text received : 24 --> temp;
Event received: temp;
Text received : 25 --> temp;
Event received: temp;
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (4) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::socket [IP: 0000] ####
#### System.Net.Sockets.Socket::.ctor [IP: 001f] ####
#### HAMSCobra.SocketCommunicationHandler::send [IP: 0011] ####
#### HAMSCobra.SocketMessageActions::.ctor [IP: 009d] ####
#### HAMSCobra.Program::sch_MessageEvent [IP: 0016] ####
#### HAMSCobra.SocketCommunicationHandler::onMessageEvent [IP: 001b] ####
#### HAMSCobra.SocketCommunicationHandler::StartListening [IP: 00a5] ####
#### SocketException ErrorCode = 10024
#### SocketException ErrorCode = 10024
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10024
#### SocketException ErrorCode = 10024
!!!!!!!! sHandler Error !!!!!!!!
Exception was thrown: System.Net.Sockets.SocketException
Text received : 26 --> temp;
Event received: temp;
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (4) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::socket [IP: 0000] ####
#### System.Net.Sockets.Socket::.ctor [IP: 001f] ####
#### HAMSCobra.SocketCommunicationHandler::send [IP: 0011] ####
#### HAMSCobra.SocketMessageActions::.ctor [IP: 009d] ####
#### HAMSCobra.Program::sch_MessageEvent [IP: 0016] ####
#### HAMSCobra.SocketCommunicationHandler::onMessageEvent [IP: 001b] ####
#### HAMSCobra.SocketCommunicationHandler::StartListening [IP: 00a5] ####
#### SocketException ErrorCode = 10024
#### SocketException ErrorCode = 10024
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10024
#### SocketException ErrorCode = 10024
!!!!!!!! sHandler Error !!!!!!!!
Exception was thrown: System.Net.Sockets.SocketException
means too many sockets open though in my finally block I’m closing the socket. I read somewhere that this can be caused by the GC who didn’t had to time to clean up the sockets yet. My polling happens every 5 secs.