I’am building for some time CCTV keyboard controller for IP PTZ camera with Spider II and ethernetJ11D (.NETMF 4.3 SDK 2015 R1). After sending few hundreds http requests each time it ends the same with SocketException ErrorCode = 10053.
Here is my code that makes trouble
static void NewSendSocket(Message message, int count)
{
string serverIP = "192.168.100.10";
int serverPort = 80;
const Int32 c_microsecondsPerSecond = 100000;
Socket serverSocket = ConnectSocket(serverIP, serverPort);
try
{
if (serverSocket != null)
{
// Create a socket connection to the specified server and port.
using (serverSocket)
{
// Send request to the server.
String request = "GET " + message.data + " HTTP/1.1\r\n" +
"Host:" + serverIP + "\r\n" +
"Content-Length:" + message.data.Length + "\r\n" +
"Connection:close\r\n" +
"Cache-Control:no-cache\r\n" +
"\r\n";
Byte[] bytesToSend = Encoding.UTF8.GetBytes(request);
serverSocket.Send(bytesToSend, bytesToSend.Length, 0);
Debug.Print("Socket " + serverSocket.LocalEndPoint.ToString() + " : " + message.command.ToString() + " Length: " + message.data.Length + " Count: " + count.ToString() + " at " + DateTime.UtcNow.TimeOfDay.ToString() + " Thread :" + Thread.CurrentThread.ManagedThreadId.ToString());
if (message.parseResponse)
{
// Allocate a buffer that we'll keep reusing to receive HTML chunks
Byte[] buffer = new Byte[1024];
// 'page' refers to the HTML data as it is built up.
String page = String.Empty;
// Poll for data until 1 second time out - Returns true for data and connection closed
while (serverSocket.Poll((30 * c_microsecondsPerSecond), SelectMode.SelectRead))
{
// Zero all bytes in the re-usable buffer
Array.Clear(buffer, 0, buffer.Length);
// Read a buffer-sized HTML chunk
Int32 bytesRead = serverSocket.Receive(buffer);
// If 0 bytes in buffer, then connection is closed
if (bytesRead == 0)
break;
// Append the chunk to the string
page = page + new String(Encoding.UTF8.GetChars(buffer));
}
if (page != "") preParseResponse(page);
}
}
Debug.Print("Socket relased at: " + DateTime.UtcNow.TimeOfDay.ToString());
}
else
{
Debug.Print("Socket null");
}
}
catch (Exception e)
{
Debug.Print("new Sender Excp");
if (serverSocket != null) serverSocket.Close();
}
}
private static Socket ConnectSocket(String server, Int32 port)
{
// Get server's IP address.
// IPHostEntry hostEntry = Dns.GetHostEntry(server);
try
{
IPAddress hostEntry = IPAddress.Parse(server);
IPEndPoint hostep = new IPEndPoint(hostEntry, port);
// Create socket and connect to the server's IP address and port
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(hostep);
Debug.Print("Socket connected. LocalEndPoint " + socket.LocalEndPoint.ToString() + " RemoteEndPoint " + socket.RemoteEndPoint.ToString());
return socket;
}
catch
{
Debug.Print("Socket connection failed: " + server);
}
return null;
}
Below some info form Output window with proper application behaviour and then on 00:27:06.5090757 it jammed for 45 sek
Message. Queue cont: 2 at 00:27:04.7541167
************** SendEvent Set ************************
Socket connected. LocalEndPoint 192.168.100.14:52004 RemoteEndPoint 192.168.100.10:80
Socket 192.168.100.14:52004 : GET_PAN_POS Length: 106 Count: 1 at 00:27:05.4710115 Thread :8
Socket relased at: 00:27:05.5351009
Response Queue count:1
----- Sender queue count: 2
----- Sender overal count: 1513
Message. Queue cont: 2 at 00:27:05.7941196
************** SendEvent Set ************************
Azymut :83 00:27:05.7978129 Socket connected. LocalEndPoint 192.168.100.14:52005 RemoteEndPoint 192.168.100.10:80
Socket 192.168.100.14:52005 : SET_OSD Length: 163 Count: 2 at 00:27:06.4083988 Thread :8
Socket relased at: 00:27:06.4338229
----- Sender queue count: 2
----- Sender overal count: 1514
Message. Queue cont: 2 at 00:27:06.5090757
************** SendEvent Set ************************
Message. Queue cont: 3 at 00:27:07.2578566
************** SendEvent Set ************************
Message. Queue cont: 4 at 00:27:07.9710597
************** SendEvent Set ************************
Unfortunately now application behaves much less predictable then before. One time I reached over 20000 requests, once less then 500. But everytime it ends up with jammed socket. What is worse - now I have spontaneous and uncontrolled application restart instead of catching Exception. It looks like VS Debugger is losing its connection to process, since I have no Exception info in VS Output window
So now it end up with no Exception, nor other Debug.Print info. After few seconds after last info in VS Output window application restarts itself.
(have no Watchdog feature setup).
In the problem code, you are not declaring or instantiating the socket within the using statement. When everything go well, the socket is not disposed and allowed to go out of scope, for later handling by GC. When there is an error, the socket is disposed by a close.
Try declaring and instantiating within the using statement.
@ John - not yet. But application talks to industrial grade CCTV camera Bosch MIC 7230 and I’m sure it is capable to serve all requests.
We use similar application (using windows socket and HTTP requests) runing on Embedded PC platform for 2 yers since now and there are no issues.
I tried also hard reset camera after exception occured but there was no result - socket was still jamed and only way to make it running again was Spider hard reset.
@ John
So here it is.
For our purpose important is to have sending speed 5 Hz minimum.
In this example I omit parsing response void but I think this has no influence on exception occurrence
@ John
I could be problem. It conects to CCTV IP camera. Do you have any avaliable in your office? If you could give me name and type I colud prepare proper HTTP request string. If not I will prepare server side code asap.
@ John
In a meantime we tried our app on Raptor + ENC28 and it works for few days with 1,2 M sent and parsed requests without issue.
We have to proceed our project so decided to change platform to G400 and I would like close this thread now.
But in my opinion this issue with Socket on SpiderS you should investigate further, since really smth is wrong.
You may test my code against any Http Server listening on port 80 and sending any response for request.