Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

sockets - C# Getting sender address from UDP message

I have an embedded Ethernet interface (Lantronix XPort) that responds to a UDP broadcast with its identifying information.

I am able to multicast the "magic packet" and datagrams are received by the listener correctly, however I also need to find out what IP Address send that response datagram. If it were TCP, I would do socket.RemoteEndPoint, but that throws an exception when applied to a UDP socket.

public class Program
{
    public static void Main(string[] args)
    {
        // magic packet
        byte[] magicPacket = new byte[4] { 0, 0, 0, 0xf6 };

        // set up listener for response
        Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        // EDIT: Also, had to add this to for it to broadcast correctly
        sendSocket.EnableBroadcast = true;
        IPEndPoint listen_ep = new IPEndPoint(IPAddress.Any, 0);
        sendSocket.Bind(listen_ep);

        // set up broadcast message
        EndPoint send_ep = new IPEndPoint(IPAddress.Parse("192.168.255.255"), 30718);
        sendSocket.SendTo(magicPacket, magicPacket.Length, SocketFlags.None, send_ep);

        DateTime dtStart = DateTime.Now;
        while (true)
        {
            if (sendSocket.Available > 0)
            {
                byte[] data = new byte[2048];
                // throws SocketException
                //IPEndPoint ip = sendSocket.RemoteEndPoint as IPEndPoint;
                sendSocket.Receive(data, SocketFlags.None);
                if (data.Length > 4)
                {
                    PrintDevice(data);
                }
            }

            if (DateTime.Now > dtStart.AddSeconds(5))
            {
                break;
            }

            Console.WriteLine(".");

            Thread.Sleep(100);
        }

        // wait for keypress to quit
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
} 

Any thoughts? Is there a better strategy to reading the response datagrams that would let me ascertain the Remote IP Address?

EDIT:

As is typical, the minute I post on SO, a moment of clarity hits me.

Turns out I can just do this:

                EndPoint remote_ep = new IPEndPoint(IPAddress.Any, 0);
                // Use ReceiveFrom instead of Receieve
                //sendSocket.Receive(data, SocketFlags.None);
                sendSocket.ReceiveFrom(data, ref remote_ep);

And remote_ep now contains the remote endpoint information!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Take a look at ReceiveFrom instead of Receive, it will let you pass in a reference to an Endpoint.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...