Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Web server, socket, reading buffer data?

web server socket buffer

  • Please log in to reply
2 replies to this topic

#1 TheBrian

TheBrian

    New Member

  • Members
  • Pip
  • 6 posts

Posted 10 February 2016 - 06:25 PM

How do I get at the data put into the buffer and sent to the web server? When using the web server code found on the netduino forums, I only seem to be able to read the header info from the buffer received. I need to see the degrees of rotation I'm trying to send. 
 
Sending code from MVC app:
 public ActionResult PostDegrees(string degree)
        {
            WebRequest request = WebRequest.Create("http://10.0.1.240"); // my N3 static IP
            request.Method = "POST";
            string postData = degree; //Degree to turn servo.
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            return null; //Probably should be something else.
        }

Receiving Code on Netduino 3:

  public void ListenForRequest(Servo servo)
        {
            while (true)
            {
                using (Socket clientSocket = socket.Accept())
                {
                    //Get clients IP
                    IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;
                    EndPoint clientEndPoint = clientSocket.RemoteEndPoint;
                    int bytesReceived = clientSocket.Available;
                    if (bytesReceived > 0)
                    {
                        //Get request
                        byte[] buffer = new byte[bytesReceived];
                        int byteCount = clientSocket.Receive(buffer);
                        string request = new string(Encoding.UTF8.GetChars(buffer));
                        Debug.Print(request);
                        //Servo code to rotate motor follows...
                    }
                }
            }
        }

Output from Debug.Print statement:
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: 10.0.1.240
Content-Length: 3
Expect: 100-continue
Connection: Keep-Alive

I'm moving a slider on a web page with a numeric range of 0-180, which is the range of motion of my servo connected to my Netduino 3. I just seem to be missing some important concept in reading the degree value that I believe I'm sending. At the moment my workaround is changing the sending code from WebRequest to HTTPWebRequest and stashing the degrees in the user agent property. This works great but just feels oh so wrong. Any insight/suggestions would really be appreciated.

 

 



#2 TechnoGuy

TechnoGuy

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts
  • LocationCanada

Posted 11 February 2016 - 12:56 AM

Hello Brian,

 

I'm intending this as a quick suggestion - I haven't looked at your code thoroughly.

 

What stands out to me in your code is your use of the "WebRequest" class rather than the "HttpWebRequest" class.

 

Would you please have a look at THIS forum posting, and response #8 in particular, which talks about the WebRequest class (which is abstract), and the HttpWebRequest class (which is an implementation of the WebRequest class).  Your solution may lie partially in switching from the abstract class to the derived one.  I believe that - when you look at the properties of the derived class, you'll see one or 2 extra items that need to be setup in order for the request to work.

 

That forum posting again, is:  http://forums.netdui...ng-to-database/

 

Also please take a look at Cuno Pfister's code (referenced in an earlier reply to the same forum posting) for a good programming example.

 

If this doesn't help you, please post again & I'll try to take a closer look at things.

 

Regards,

Ian


- Ian

 

My Current Dev Boards:

  • 3 x Netduino Plus 2
  • 1 x Netduino 3 WiFi

#3 TheBrian

TheBrian

    New Member

  • Members
  • Pip
  • 6 posts

Posted 11 February 2016 - 06:52 PM

Hi Ian,

 

Thanks for the reply. Actually at the end of my post I mention that I am currently using HTTPWebRequest, but the result is the same i.e. I only see the header info in the buffer received. So as a workaround I am using the UserAgent property and setting that value to the degrees I want my servo to turn to since I can see that property in the heading.

 

The code using WebRequest came directly from here:

https://msdn.microso...(v=vs.110).aspx

 

In that example a long string value is put in a field called postData and sent to the web server. It is that string that I would like to Debug.Print on the Netduino 3 end.

 

I read through the post you referenced. I also downloaded the XivelyClient you mentioned, put it in a quick console app, and altered it to send to my Netduino 3. The result is also the same. All versions, WebRequest, HttpWebRequest, and XivelyClient's HttpWebRequest, successfully enter the Netduino 3 code listening for requests (ListenForRequest above) . I just don't seem to be able to tweeze out the data value I purposefully put in the byte array. The buffer received only prints:

POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: 10.0.1.240
Content-Length: 3
Expect: 100-continue
Connection: Keep-Alive

Thanks again,

Brian







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.