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.

Immo

Member Since 11 May 2011
Offline Last Active Nov 17 2012 04:44 PM
-----

Posts I've Made

In Topic: NetDuino Plus not Receiving UDP Broadcast Packets

05 May 2012 - 02:17 PM

Hi Homey,

following code I use on ND+ to receive UDP Broadcast packets
from iPhone and it works fine here with 4.1 and 4.2:

private void threadStartTarget()
        {
            _socket = new Socket(AddressFamily.InterNetwork,
                SocketType.Dgram, ProtocolType.Udp);
            try
            {
                IPEndPoint broadcastEndPoint = new IPEndPoint(
                    System.Net.IPAddress.Any, LocalPort);
                
                _socket.SetSocketOption(SocketOptionLevel.Socket,
                    SocketOptionName.ReuseAddress, true);
                if (Broadcast)
                    _socket.SetSocketOption(SocketOptionLevel.Socket,
                        SocketOptionName.Broadcast, true);

                _socket.Bind(broadcastEndPoint);

                while (true)
                {
                    EndPoint remoteEndPoint = new IPEndPoint(
                        System.Net.IPAddress.Any, 0);

                    int availableBytes = _socket.Available;
                    if (availableBytes > 0)
                    {
                        byte[] receiveBuffer = new byte[availableBytes];
                        _socket.ReceiveFrom(receiveBuffer, ref remoteEndPoint);

                        if (DataReceived != null)
                            DataReceived(this,
                                new DataReceivedEventArgs(
                                    receiveBuffer, remoteEndPoint));
                    }
                    // This is recommends in thread loops to help prevent lock-ups
                    Thread.Sleep(10);
                }
            }
            catch (Exception e)
            {
                // catch all exceptions
                Debug.Print(e.ToString());
            }
            finally
            {
                doDispose();
                if (SocketDone != null)
                    SocketDone(this, EventArgs.Empty);

                Debug.Print("UDP server thread finished.");
            }
        }
HTH, Immo

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.