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

Simple Socket example?


  • Please log in to reply
7 replies to this topic

#1 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 17 February 2011 - 05:12 AM

Could someone post a simple Socket example that listen then whatever it receives it sends back? I am having a tough time just doing this. I am getting this error with this code on the GetChars line.
An unhandled exception of type 'System.Exception' occurred in mscorlib.dll

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.Text;

namespace NetduinoPlusApplication2
{
    public class Program
    {
        public static void Main()
        {
            using (System.Net.Sockets.Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Bind(new IPEndPoint(IPAddress.Any, 8080));
                socket.Listen(1);

                while (true)
                {
                    using (Socket newSocket = socket.Accept())
                    {
                        if (newSocket.Poll(-1, SelectMode.SelectRead))
                        {
                            byte[] bytes = new byte[newSocket.Available];
                            int count = newSocket.Receive(bytes);
                            char[] chars = Encoding.UTF8.GetChars(bytes);
                            string str = new string(chars, 0, count);

                            Debug.Print(str);
                        } 
                    }
                }
            }
        }
    }
}


#2 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 17 February 2011 - 05:33 PM

It might be the example client code I downloaded not sending UTF8. If it got that far it made the socket connection and sent data. I am going to be working on figuring out how to make a WebSocket server. I have been trying to find some good examples. Right now I am going ot base it off of this: http://ashishware.co...ocksample.shtml I want to avoid ad much processing on the netduino as possible but still control it and retreive information over the network port. My netduino will be connected to a Fonera router that has DD-WRT on it. If I could have the web hosting done off of hte netduino and on the fonera that should help out. I tried to put php on the fonera but there isn't enough room. That leaves me with jQuery and the socket hacks with that that use SWF or HTML5. Considering I am going to use an ipod tough SWF is out of the question. Safari mobile supports HTML5 WebSockets. With websockets there hsould be just the initial handshake for any significant data transfer. Once the socket is open then it can be simple single or double byte commands and responses. With jQuery I shouldn't have to refresh the webpage so no need to remake the socket unless it times out.

#3 Fred

Fred

    Advanced Member

  • Members
  • PipPipPip
  • 302 posts
  • LocationUK

Posted 17 February 2011 - 07:53 PM

I posted a web server example here. It's fairly simple code that listens on port 80 and responds depending on the URL parsed from the http header. Not that different to what you have above, but might give a pointer to something you missed.

#4 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 17 February 2011 - 08:51 PM

Yeah, I just realize that it won't be much different than some of the web server code that has been posted. It basically is the same accept the listeningSocket.Accept() will have to be open outside of the while(true) loop. The while(true) will have to be while(clientSocket not closed) and add in a timeout to the loop. Then add in the WebSocket handshake to the initial connect.

#5 Quiche31

Quiche31

    Advanced Member

  • Members
  • PipPipPip
  • 87 posts
  • LocationFrance

Posted 17 February 2011 - 09:12 PM

Could someone post a simple Socket example that listen then whatever it receives it sends back?

I haven't got any error, running your code as it, this is what I've got, using firmware 4.1.0.5
GET / HTTP/1.1
Host: 192.168.1.80:8080
Connection: keep-alive
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,fr;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3


#6 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 17 February 2011 - 09:42 PM

Yeah, it had to be the client code then not sending UTF8. I thought I was doing it right.

#7 Quiche31

Quiche31

    Advanced Member

  • Members
  • PipPipPip
  • 87 posts
  • LocationFrance

Posted 17 February 2011 - 09:44 PM

Yeah, it had to be the client code then not sending UTF8. I thought I was doing it right.

What do you mean client code? I used a browser (Chrome) invoking: http://192.168.1.80:8080 (the IP statically set for my N+), that's it. Try swapping firmware, just in case...

#8 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 17 February 2011 - 09:54 PM

What do you mean client code? I used a browser (Chrome) invoking: http://192.168.1.80:8080 (the IP statically set for my N+), that's it. Try swapping firmware, just in case...

Yeah, I didn't think about that. I found some example C# code that opens a socket and sends whatever you type.




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.