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.

bghavami

Member Since 20 Jan 2013
Offline Last Active Mar 28 2016 04:14 AM
-----

Posts I've Made

In Topic: UDP and ESP8266

07 January 2016 - 04:33 PM

I hope you have solved the problem by now, but if you haven't yet here is the solution.

 

      private static int nPort = 12345;
      private static EndPoint EndPointRemote;
      private static int nBufSize = 1024;

      public void Start()
      {
         //
         //  Wait until we have an IP address.
         //
         while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any)
         {
               Thread.Sleep(10);
         }
         Debug.Print("IP Address: " + IPAddress.GetDefaultLocalAddress());

         EndPointRemote = new IPEndPoint(IPAddress.Any, nPort);
         Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
         socket.Bind(EndPointRemote);
         int bytesRead;
         byte[] buffer = new byte[nBufSize + 1];

         while (true)
         {
            if( (bytesRead = socket.ReceiveFrom(buffer, nBufSize, SocketFlags.None, ref EndPointRemote)) > 0 )
            {
               Debug.Print("Message received from " + EndPointRemote.ToString() + ": " + new string(System.Text.Encoding.UTF8.GetChars(buffer, 0, bytesRead)));
            }
            Thread.Sleep(0);
         }
      }

 

Let me know if this work for you.

 

Bizhan.


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.