How do you get the sender IP address of a UDP multicast packet? The current code is set in a synchronous / blocking manner (see Note below). Here is the code:
private void receive() { string mcastGroup = SetMcastGroup(); s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); s.EnableBroadcast = true; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5000); s.Bind(ipep); IPAddress ip = IPAddress.Parse(mcastGroup); s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any)); while (true) { try { byte[] b = new byte[4096]; s.Receive(b); string str = Encoding.ASCII.GetString(b, 0, b.Length);
Note. This question arises from chat, as this is not my code. I only ask because I understand the problem.
source share