Socket fails after first try on connecting to PPP

After I connect to PPP I send data via HTTP to a server. I make 2 HTTP requests and they both go through until the second one then shows the following exception. After this nothing will be sent to the server until I restart the system.

I need to be able to recover from any errors.


The thread '<No Name>' (0xc) has exited with code 0 (0x0).
    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (8) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::recv [IP: 0000] ####
    #### System.Net.Sockets.Socket::Receive [IP: 0018] ####
    #### System.Net.Sockets.NetworkStream::Read [IP: 0062] ####
    #### System.Net.InputNetworkStreamWrapper::RefillInternalBuffer [IP: 0038] ####
    #### System.Net.InputNetworkStreamWrapper::Read_HTTP_Line [IP: 004b] ####
    #### System.Net.HttpWebRequest::ParseHTTPResponse [IP: 002e] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 0035] ####
    #### MNTEnergy7.GSMmodemClass::SendSCADA [IP: 02c7] ####
    #### MNTEnergy7.GSMmodemClass::gsmHandler [IP: 02d0] ####
    #### SocketException ErrorCode = -1728053248
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
    #### SocketException ErrorCode = -1728053248
    #### SocketException ErrorCode = -1728053248
    #### SocketException ErrorCode = -1728053248
    #### Exception System.Net.WebException - 0x00000000 (8) ####
    #### Message: 
    #### System.Net.HttpWebRequest::GetResponse [IP: 00c8] ####
    #### MNTEnergy7.GSMmodemClass::SendSCADA [IP: 02c7] ####
    #### MNTEnergy7.GSMmodemClass::gsmHandler [IP: 02d0] ####
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll

I am using the following code to make the connection. sensorData contains the URL for the request. It works for the first 2 calls to the function.


            using (var req = HttpWebRequest.Create(sensorData) as HttpWebRequest)
            {
                try
                {
                    req.Timeout = 10000;    // 10 seconds timeout

                    using (var res = req.GetResponse() as HttpWebResponse)
                    {
                        using (var stream = res.GetResponseStream())
                        {
                            int offset = 0;
                            do
                            {
                                read = stream.Read(result, offset, result.Length - offset);

                                offset += read;

                                Thread.Sleep(20);
                            }
                            while (read != 0);
                            reply = new String(System.Text.Encoding.UTF8.GetChars(result));
                        }
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }

I think I found the issue.

This is the string I send as the url. I think the spaces in the data stamp are causing the failure.


AbsoluteUri = "http://rasuna.homedns.org:8085/httpds?&__device=mntwell1&pressure1=186.902&pressure2=-999.000&other1=-999.000&timestamp=06 Jun 2016 16:28:45"

Is there any functions built in to handle this?

My own code that uploads data to a SCADA server running Mango Automation. I build the URL based on the data to be send to the server.

The previous design used the built in TCP/IP of the modem which automatically converted the strings but now with PPP it will need to be done by myself. I saw some functions in .NET to do this but .NETMF doesn’t have these available. For now I just added minus (-) signs in place of the spaces. It’s only a timestamp and visual only to show the user when the data was last received.

Will this work?

[url]https://netmftoolbox.codeplex.com/wikipage?title=Toolbox.NETMF.Tools[/url]

Using: RawUrlEncode(Input)

Disclaimer: I Googled using my phone “urlencode .netmf” and this was the first listing. So maybe there are other options out there.

1 Like