Main Site Documentation

Issue with receiving UDP Packets in UDP Receiver Console Application


#1

Hi

My laptop is connected to our company internal network and I attached an image which shows my network configuration

I also connected the FEZ device to a switch of the network and I can ping the FEZ device through the command prompt on my laptop .

I have written a UDP Sender Gadgeteer application which code is as follows :

   string[] dnsAddresses = { "172.16.40.2", "172.16.40.5" };

   void ProgramStarted()
        {
            InitEthernet();
            BlinkLED();
            SendUDPMessage();
        }

        private void InitEthernet()
        {
            if (!ethernet.Interface.IsOpen)
            {
                ethernet.Interface.Open();
            }
            NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet.Interface);
            ethernet.Interface.NetworkInterface.EnableStaticIP("172.16.43.193", "255.255.255.0", "172.16.43.254");
            ethernet.Interface.NetworkInterface.EnableStaticDns(dnsAddresses);
        }

        private void BlinkLED()
        {
            multicolorLed.BlinkRepeatedly(GT.Color.Purple);
        }

        private static void SendUDPMessage()
        {
            int numOfUDPMessagesSent = 0;
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPAddress DestinationIP = new IPAddress(new byte[] { 172, 16, 43, 175 });
            IPEndPoint DestinationEndPoint = new IPEndPoint(DestinationIP, 2000);
            byte[] buf;
            for (int y = 0; y < 1000; y++)
            {
                Thread.Sleep(2000);
                buf = Encoding.UTF8.GetBytes("Hello World from FEZ Spider" + y.ToString());
                try
                {
                    socket.SendTo(buf, DestinationEndPoint);
                    numOfUDPMessagesSent += 1;
                    Debug.Print("Num of udp sent : " + numOfUDPMessagesSent.ToString());
                }
                catch
                {
                }
            }
        }

when I deploy It to the FEZ device in the output window I see the messege : “Num of udp sent : i”

which meens that UDP Packets our beeing sent on the network to my laptop cause the DestinationIP is my laptop IP in the network .

after that I tried to write a simple console application and run It on my laptop to receive UDP Packets being sent by the FEZ device gadgeteer application .
Here is the code of this console application :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace UDPReceiver
{
    class Program
    {


        static void Main(string[] args)
        {
            Socket serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
            EndPoint ServerEndPoint = new IPEndPoint(IPAddress.Any, 2000);
            serversocket.Bind(ServerEndPoint);
            int i = 1;

            while (true)
            {
                if (serversocket.Poll(-1, SelectMode.SelectRead))
                {
                    EndPoint recEndPoint = null;
                    byte[] inBuf = new byte[serversocket.Available];
                    int count = serversocket.ReceiveFrom(inBuf, ref recEndPoint);
                    Console.Write(Encoding.UTF8.GetChars(inBuf));
                    Console.Write("From" + recEndPoint.AddressFamily.ToString());
                }
            }
        }
    }
}

Now here is the issue : when I run this console application and try to receive the UDP Packets sent by FEZ device
at this line of code :

int count = serversocket.ReceiveFrom(inBuf, ref recEndPoint);

I get the error which sais the second parameter of ReceiveFrom function which is remoteEP is null .

what should I do ? thanks in advance .


#2

Check codeshare and forum we have UDP examples.

Here’s one of them
https://www.ghielectronics.com/community/codeshare/entry/221


#3

I changed my receiver code to this and It worked !

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace UDPReceiver
{
    class Program
    {


        static void Main(string[] args)
        {
            Socket serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
            EndPoint ServerEndPoint = new IPEndPoint(IPAddress.Any, 2000);
            serversocket.Bind(ServerEndPoint);
            int i = 1;

            while (true)
            {
                if (serversocket.Poll(-1, SelectMode.SelectRead))
                {
                    byte[] inBuf = new byte[serversocket.Available];
                    int count = serversocket.ReceiveFrom(inBuf, ref ServerEndPoint);
                    Console.WriteLine (Encoding.UTF8.GetChars(inBuf));
                    Console.WriteLine("From" + ServerEndPoint.AddressFamily.ToString());
                }
            }
        }
    }
}


#4

Great! Codeshare is a very valuable resource.