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

Taking sockets further


  • Please log in to reply
6 replies to this topic

#1 avword

avword

    Member

  • Members
  • PipPip
  • 11 posts

Posted 28 September 2010 - 03:15 AM

After putting together some examples from these forums (thanks everyone!), I would like to take things further. Here is the server that I have so far:



namespace NetworkTester
{
    public class Program
    {

        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);



        public static byte[] StrToByteArray(string str)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            return encoding.GetBytes(str);
        }


        public static void Main()
        {
            // write your code here
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface networkInterface in networkInterfaces)
            {
                networkInterface.EnableStaticIP("192.168.1.36", "255.255.255.0", "192.168.1.1");
                Debug.Print("Gateway Address: " + networkInterface.GatewayAddress);
                Debug.Print("IP Address: " + networkInterface.IPAddress);
                Debug.Print("Subnet mask " + networkInterface.SubnetMask);

            }


            using (System.Net.Sockets.Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {

                socket.Bind(new IPEndPoint(IPAddress.Any, 80));
                Debug.Print("first");
                socket.Listen(1);
                Debug.Print("here");
                while (true)
                {

                    using (Socket commSocket = socket.Accept())
                    {
                        Debug.Print("here1");


                        commSocket.Send(StrToByteArray("\r\n" + "Hello"));
                        bool stay = true;
                    while (stay)
                    {

                        if (commSocket.Poll(-1, SelectMode.SelectRead))
                        {
                            Debug.Print("here2");


                 			byte[] bytes = new byte[commSocket.Available];
                            
                                int count = commSocket.Receive(bytes);
                                string s = new String(Encoding.UTF8.GetChars(bytes));
                                if (s == "c")
                                {
                                    commSocket.Send(StrToByteArray("\r\n" + "Good Night!"));
                                    stay = false;
                                }

                                if (s == "l")
                                {
                                    led.Write(true); // turn on the LED
                       			
                                        Thread.Sleep(100); // sleep for 250ms
                                        led.Write(false); // turn on the LED
                                }

                                if (s == "o")
                                {
                                    led.Write(false); // turn on the LED
                                    Thread.Sleep(100); // sleep for 250ms
                                    led.Write(true); // turn on the LED
                                }
                                Debug.Print(s);

                            }
                    }
                        Debug.Print("done");
                    }
                }
            }


        }

    }
}



Here is what I would like to do next:
1. I would like to accept multiple clients at the same time. Anyone have thoughts about how do do that?
2. Right now this gets hung up if one of the clients exits unexpectedly. How can I gracefully detect that a client went away?

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 28 September 2010 - 03:24 AM

1. I would like to accept multiple clients at the same time. Anyone have thoughts about how do do that?


If you run the .Accept(...) function again, you should be able to accept multiple clients.

2. Right now this gets hung up if one of the clients exits unexpectedly. How can I gracefully detect that a client went away?


First thoughts... When you get a new connection, perhaps you can create a new thread and service that connection (socket) from the new thread...and then set timeouts and catch exceptions if/when the socket times out...

Chris

#3 avword

avword

    Member

  • Members
  • PipPip
  • 11 posts

Posted 28 September 2010 - 03:31 AM

If you run the .Accept(...) function again, you should be able to accept multiple clients.



First thoughts... When you get a new connection, perhaps you can create a new thread and service that connection (socket) from the new thread...and then set timeouts and catch exceptions if/when the socket times out...

Chris



Right now .Accept is getting run again as it is in the first "while(true)" loop. So what happens is that after the first client disconnects (by pressing "c"), the second client is serviced. But until the first client disconnects, the second one is in a hung state. Could you point me to an example of how do each connection as a separate thread?

#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 28 September 2010 - 03:36 AM

Right now .Accept is getting run again as it is in the first "while(true)" loop. So what happens is that after the first client disconnects (by pressing "c"), the second client is serviced. But until the first client disconnects, the second one is in a hung state. Could you point me to an example of how do each connection as a separate thread?


The SocketServer sample which was installed with the .NET Micro Framework 4.1 SDK has a multi-threaded socket server sample... It could be implemented in a simpler fashion, but that should give you a start...

[If in case you're not familiar with multi-threading, we or someone in the community could maybe create a simpler sample as well.]

Chris

#5 avword

avword

    Member

  • Members
  • PipPip
  • 11 posts

Posted 28 September 2010 - 03:52 AM

The SocketServer sample which was installed with the .NET Micro Framework 4.1 SDK has a multi-threaded socket server sample... It could be implemented in a simpler fashion, but that should give you a start...

[If in case you're not familiar with multi-threading, we or someone in the community could maybe create a simpler sample as well.]

Chris


Ok cool, I see the example program (I didn't know those were there). I'll play around with that for a bit. From what I see so far, it makes sense. I see that it also shows how to end the session if the connection was closed or reset.

That said, if you or someone in the community had the time, I think a general multi-threading example would be very useful (but only if you have the spare time).


Thanks!!

#6 stacyh3

stacyh3

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts

Posted 28 September 2010 - 12:42 PM

Another quick tip: There's a static Encoding class that you can use instead of "newing" one up for StrToByteArray
Encoding.UTF8.GetBytes(s)
will do the trick and save you a little bit in terms of memory allocations.

#7 Charles

Charles

    Advanced Member

  • Members
  • PipPipPip
  • 192 posts

Posted 06 October 2010 - 02:53 PM

I just posted a Telnet Server example elsewhere in the forum that answers some of your questions... I think you will find the code very helpful. http://forums.netdui...r-with-a-story/




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.