[英]c# - get source IP of multicast packet with recieveFrom
Good day all 美好的一天
Problem: 问题:
I am trying to get the source IP of multicast packets, however all that I get is 0.0.0.0:80 我试图获取组播数据包的源IP,但我得到的只是0.0.0.0:80
What I have tried: 我尝试过的:
I tried methods shown in these sites, not sure if I correctly implemented it, but all return the same IP which is 0.0.0.0, this post and this one 我尝试了这些网站中显示的方法,不确定我是否正确实现了它,但都返回相同的IP,即0.0.0.0, 这个帖子和这个
Both links refer to using socket.recieveFrom() or socket.BeginRecieveMessageFrom() instead of socket.recieve() 两个链接都指的是使用socket.recieveFrom()或socket.BeginRecieveMessageFrom()而不是socket.recieve()
private void recieveText()
{
//initialise multicast group and bind to interface
Socket _listener_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, _PORT);
_listener_socket.Bind(ipep);
IPAddress localip = IPAddress.Parse("224.5.6.7");
_listener_socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(localip, IPAddress.Any));
//recieve data to multicast group
while (_listener_socket.IsBound)
{
updateLabel("listening...");
byte[] b = new byte[1024];
updateLabel("message recieved");
updateRedBox("\n---------------------------------\n New Message :\n");
EndPoint IPEPoint = (EndPoint)ipep;
_listener_socket.BeginReceiveMessageFrom(b, 0, b.Length, 0, ref IPEPoint, null, null);
updateRedBox(IPEPoint.ToString());
char[] chars = new char[b.Length / sizeof(char)];
System.Buffer.BlockCopy(b, 0, chars, 0, b.Length);
string t = new string(chars).Trim();
updateRedBox(t);
updateRedBox("\n----------------------------------\n");
}
}
You should use the synchronous ReceiveMessageFrom
call or call EndReceiveMessageFrom
after calling the asynchronous BeginReceiveMessageFrom
您应该使用同步
ReceiveMessageFrom
通话或拨打EndReceiveMessageFrom
调用异步后BeginReceiveMessageFrom
private void recieveText()
{
//initialise multicast group and bind to interface
Socket _listener_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, _PORT);
_listener_socket.Bind(ipep);
IPAddress localip = IPAddress.Parse("224.5.6.7");
_listener_socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(localip, IPAddress.Any));
//recieve data to multicast group
while (_listener_socket.IsBound)
{
updateLabel("listening...");
byte[] b = new byte[1024];
updateLabel("message recieved");
updateRedBox("\n---------------------------------\n New Message :\n");
EndPoint IPEPoint = (EndPoint)ipep;
var res = _listener_socket.BeginReceiveMessageFrom(b, 0, b.Length, 0, ref IPEPoint, null, null);
SocketFlags flags = SocketFlags.None;
IPPacketInformation packetInfo;
_listener_socket.EndReceiveMessageFrom(res, ref flags, ref IPEPoint, out packetInfo);
updateRedBox(IPEPoint.ToString());
char[] chars = new char[b.Length / sizeof(char)];
System.Buffer.BlockCopy(b, 0, chars, 0, b.Length);
string t = new string(chars).Trim();
updateRedBox(t);
updateRedBox("\n----------------------------------\n");
}
}
Look at the 3 lines following BeginReceiveMessageFrom
that I have added. 查看我添加的
BeginReceiveMessageFrom
之后的3行。 In addition to the Remote IP Address, you can use the flags to find out whether this message was received as a Multicast message and the multicast group IP Address can be obtained from packetInfo
除了远程IP地址,您可以使用标志来确定此消息是否作为多播消息接收,并且可以从
packetInfo
获取多播组IP地址
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.