同一个应用程序中的c#udp客户端服务器过滤掉本地应用程序发送的数据

时间:2018-06-04 18:57:32

标签: c# udp

我有一个客户端服务器应用程序,它同时监听和发送相同的多播地址/端口。问题是我将收到发送数据的环回。

UdpClient client = new UdpClient();

client.ExclusiveAddressUse = false;
IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 62255);

client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.ExclusiveAddressUse = false;

client.Client.Bind(localEp);

IPAddress multicastaddress = IPAddress.Parse("239.10.4.1");
client.JoinMulticastGroup(multicastaddress);
IPEndPoint remoteep = new IPEndPoint(multicastaddress, 62255);

while (true)
{
    Byte[] buffer = client.Receive(ref localEp);

    string msgString = "test";
    byte[] buffer2 = Encoding.Unicode.GetBytes(msgString);
    client.Send(buffer2, buffer2.Length, remoteep);
}

我曾尝试使用localEp.Address.ToString()过滤掉本地应用发送的内容,但当我尝试使用多个NIC的计算机时,我仍然收到本地消息。还有其他方法吗?

0 个答案:

没有答案