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

Netduino Plus Socket Server Help


  • Please log in to reply
6 replies to this topic

#1 rchelicopter

rchelicopter

    Member

  • Members
  • PipPip
  • 18 posts
  • LocationSan Diego, Ca

Posted 10 July 2012 - 09:34 PM

Hello Netduino Community, I have been playing with programming the Netduino Plus for a few months now trying to get a robust TCP server up and running. I have also been working on a windows form client to send data to the server. I am using message framing and I can detect when clients close the socket intentionally but I removed that for simplicity. It actually works suprisingly well considering I know very little about .net and ,netMF. My problem is that the Netduino Plus is on a wireless router and sometimes it loses connection to the client (basically the client loses connection unexpectedly), I have looked and looked and cannot find a method for the Netduino to detect when the client goes south on me. The client is basically pushing data to the server and so anything Ive tried in the while (true) loop stops when the client stops sending data so any code that runs in that loop to detect disconnects stops when data stops coming in (example, sending data to the client and getting a responce). I would like to be able to send data to the client outside the using loop at a slower interval than what I'm receiving but I cannot figure out how to do it. I would be more than happy to post my client and server projects if that would help. here is the code I originally started with and now I've returned to for learning using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; namespace Socket_Testing { 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, 10100)); socket.Listen(1); { while (true) using (Socket NetduinoSocket = socket.Accept()) { byte[] buffer = Encoding.UTF8.GetBytes("You Are Now Connected"); NetduinoSocket.Send(buffer, 0, buffer.Length, 0); while (true) { if (NetduinoSocket.Poll(1, SelectMode.SelectRead)) { byte[] bytes = new byte[10]; int count = NetduinoSocket.Receive(bytes); char[] chars = Encoding.UTF8.GetChars(bytes); string position = new string(chars, 0, count); Debug.Print(position); } } } } } } } }

#2 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 11 July 2012 - 09:14 AM

Hi

I would look at Stefan's excellent netmf toolbox particularly at the terminal server and simple socket classes. Typically with a socket I would use the events for connection and disconnection rather that the while loop you are currently using for detecting a connection. As it allows you greater control over the handling of such events.

Nak.

#3 rchelicopter

rchelicopter

    Member

  • Members
  • PipPip
  • 18 posts
  • LocationSan Diego, Ca

Posted 12 July 2012 - 05:06 PM

Hi

I would look at Stefan's excellent netmf toolbox particularly at the terminal server and simple socket classes. Typically with a socket I would use the events for connection and disconnection rather that the while loop you are currently using for detecting a connection. As it allows you greater control over the handling of such events.

Nak.


Thanks for your reply Nakchak,

I have played around with Stefan's terminal server and it is awesome to say the least. I was modifying it to better suit my needs (removing some functionality) and then I realised that I still have a problem that when the client "loses" connection unexpectedly the server still thinks its connected and I cannot connect again. Eventually i would like to end up with a solution that will take action as soon as the client drops out (wireless router).

Could you please elaborate a bit more on the "events for connection and disconnection" you eluded to? Perhaps you could point me to some documetation that helps explain this a bit more.

And thank you for your earlier comments.

#4 JimmyNet

JimmyNet

    Member

  • Members
  • PipPip
  • 12 posts

Posted 14 July 2012 - 03:38 PM

I had also problems with Wifly from Xbee to stay connected. Yesterday i solved it by: 1: Not to use DHCP from your router but to assign is with a static IP 2: Turn DHCP off in MFDeploy (network properties) and assign a static ip, gateway ip 3: In my Wifi router assign all wifi devices with a reserved dhcp ip address and mac address 4: you have a good connection? i put my near my wifi router and did a performance test. Since yesterdag is send a socket request every 5 minutes and it is doing this more then 12 hours. Before the adjustments i was losing connections between 10 a 20 minutes. Gr. jimmy.

#5 Nobby

Nobby

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 17 July 2012 - 06:16 AM

TCP sockets consist of remote and local endpoints by design so naturally you'd assume the state of the connection is managed by TCP but it isn't. The only way to determine the current state of the socket is: -Enforce an activity timeout -Attempt to read from the socket -Attempt to write to the socket In the first case, simply enforce a timeout of a desired interval. If no communications occur within a certain time-frame, forcefully terminate the socket. In the second case, the operation will block indefinitely if no deterministic data stream exists(i.e. client isn't required to send data regularly). If you set a read timeout on the socket, it will throw an exception but this is a false-positive in terms of detecting a client disconnecting. The best method is to write to the socket. This is commonly refered to as Keep-Alive. If the client has closed their socket at the remote endpoint, a socket exception will be thrown at the local endpoint and you can remove the client. Send a single byte or an appropriate byte pattern to the client. They can either absorb the data or make an appropriate response if required. The problem with this approach is that the client is required at at least monitor for this incoming Keep-Alive data. End-to-End data transfer with TCP is fully-interlocked. Even if you perform a write operation on a socket asynchronously, the data backs up in the server's memory(in the netduino RAM in this case) until the client reads all pending data. This is dangerous to server stability. If you are creating an HTTP server, the HTTP specification covers the aspect of Keep-Alive and the client notifies the server through GET/POST methods whether or not they would like the connection to remain open. Even then, this is usually in the order of minutes. HTTP is typically not used for persistent connections though except in the case of data streaming. Good luck

#6 Dave M.

Dave M.

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 15 January 2013 - 10:49 PM

Can anyone comment on nakchak's comment regarding the usage of events for connections / disconnections?  While I'd love to do that, I've gone through the API reference and used Intellisense to peek at the members, and it sure looks to me like Poll is the only way to get incoming data.



#7 rchelicopter

rchelicopter

    Member

  • Members
  • PipPip
  • 18 posts
  • LocationSan Diego, Ca

Posted 15 January 2013 - 11:31 PM

Hello,

I to would love to learn how to use the events for connection and disconnection rather than the while loop I'm currently using.  I have looked around a bit over the last few months and I havent found a good example to learn from.

 

Thanks.






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.