C# 我无法从UdpClient接收我自己的Mcast数据包
我无法接收通过多播发送的我自己的数据包。我创建了两个Udpclient,接收方接收多播组上的数据包,发送方发送数据包。我的数据包已发送到该组,但我无法接收回我发送的数据包C# 我无法从UdpClient接收我自己的Mcast数据包,c#,udp,multicast,C#,Udp,Multicast,我无法接收通过多播发送的我自己的数据包。我创建了两个Udpclient,接收方接收多播组上的数据包,发送方发送数据包。我的数据包已发送到该组,但我无法接收回我发送的数据包 public void Join() { IPAddress ip1 = IPAddress.Any; localep = new IPEndPoint(ip1, port); Receiver = new UdpClien
public void Join()
{
IPAddress ip1 = IPAddress.Any;
localep = new IPEndPoint(ip1, port);
Receiver = new UdpClient();
Receiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Receiver.Client.Bind(localep);
Sender = new UdpClient();
Sender.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Sender.Client.Bind(localep);
IPAddress ip = IPAddress.Parse(IP);
remoteep = new IPEndPoint(ip, port);
Sender.JoinMulticastGroup(ip);
Sender.EnableBroadcast = true;
Sender.MulticastLoopback = true;
Receiver.JoinMulticastGroup(ip);
Receiver.EnableBroadcast = true;
Receiver.MulticastLoopback = true;
udpState.ipEndpt = RemoteIpEndPoint;
udpState.udpClient = Receiver;
Receiver.BeginReceive(new AsyncCallback(GetMsg), udpState);
}
void GetMsg(IAsyncResult ar)
{
UdpClient udpClient = (UdpClient)((UdpState)(ar.AsyncState)).udpClient;
IPEndPoint ipEndpt = (IPEndPoint)((UdpState)(ar.AsyncState)).ipEndpt;
RecByte = Receiver.EndReceive(ar, ref ipEndpt);
}
//Sending packets logic
McastOTS.Sender.Send(sendBytes, sendBytes.Length, McastOTS.remoteep);
在我看来,你所需要做的就是在你结束接收后再次打电话给BeginReceive。否则,您将只收到一条消息,而不会看到任何其他消息
void GetMsg(IAsyncResult ar)
{
UdpClient udpClient = (UdpClient)((UdpState)(ar.AsyncState)).udpClient;
IPEndPoint ipEndpt = (IPEndPoint)((UdpState)(ar.AsyncState)).ipEndpt;
RecByte = Receiver.EndReceive(ar, ref ipEndpt);
Receiver.BeginReceive(GetMsg, udpState);
}