Main Site Documentation

Socket.ReceiveFrom(Byte[], ref EndPoint)


Hi all,

A quick question for the networking gurus out there -

UDPSocket.ReceiveFrom(inBuffer, ref RXEndPoint);

My understanding of the EndPoint pass by reference is that infomation regarding the incoming packet is updated in the passed EndPoint. In this case the IP address of the incoming packet is what I am interested in.

RxIP = (RXEndPoint as IPEndPoint).Address;

Using the code snippet above I can access the Address property. However it points at the original values that the EndPoint was configured with (IPAddress.Any, 10000).

I understand that if the connection protocol was TCP and it was connected it would return the IPEndPoint of the remote connection. In this case though I am using UDP.

Short of implementing a complete Raw socket handler (Something I don’t have the time or knowledge to do :’( ) is there a way to find out what the IP address of the incoming UDP packet? (IPHeader info that the UDP packet was framed in)

I’ve also noted the complete lack of a UDPClient class (System.Ext.Net… etc…) Is this something that will be coming to .NetMF 4.x?



I checked out a UDP ReceiveFrom() on a project I am working on, and the remote address is being set correctly after the read.

Please develop a small program, which demonstrates your problem, and post the code.



I’ve pasted some sample code below - The key area is inside the UDP_RXThreadFunction()

class UDPSockets
            private Thread UDP_RXThread;
            private Socket UDPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            private IPAddress ipAddress = IPAddress.Parse("");        
            private const int TXPort = 5000;
            private const int RXPort = 10000;

            private EndPoint RXEndPoint;

            public delegate void EventHandler(byte[] Data, int Length, Interface PHY, IPAddress RxIP);
            public virtual event EventHandler DataReceived;

            public UDPSockets()
                RXEndPoint = new IPEndPoint(IPAddress.Any, RXPort);

                UDP_RXThread = new Thread(UDP_RXThreadFunction);

                catch (Exception ex)
                    Debug.Print("Cannot connect to port.");


            public void TXData(byte[] Data, IPAddress IP)
                int Count = 0;
                IPEndPoint TXEndPoint = new IPEndPoint(ipAddress, TXPort);

                while ((Data.Length - Count) > 0)
                    Count += UDPSocket.SendTo(Data, Count, Data.Length - Count, SocketFlags.None, TXEndPoint);

            private void UDP_RXThreadFunction()
                int count = 0;
                byte[] inBuffer = new byte[1024];
                IPAddress RxIP = new IPAddress(0xFFFFFFFF);

                while (true)
                    if (UDPSocket.Poll(-1, SelectMode.SelectRead))
                        count = UDPSocket.ReceiveFrom(inBuffer, ref RXEndPoint);
                        RxIP = (RXEndPoint as IPEndPoint).Address;
                        DataReceived(inBuffer, count, Interface.Ethernet, RxIP);


I usually do not use the Poll() method, since I use an infinite wait.

Does the problem show up after the first ReceiveFrom() or later? After the first receive, RXEndPoint will be set to a remote address/port, and all future ReceiveFrom() will be waiting for packets from that host only.

I would use:

EndPoint ep = RXEndPoint;

ReceiverFrom(…, ref ep);

This way, RXEndPoint keeps the correct values, and the reference within EP will be changed to the remote end.

I don’t have time right now to load your code and see what is happening in more detail.