public void send_multicast(string message)
{
UdpClient c = new UdpClient(10102);
Byte[] sendBytes = Encoding.ASCII.GetBytes(message);
IPAddress m_GrpAddr = IPAddress.Parse("224.0.0.1");
IPEndPoint ep = new IPEndPoint(m_GrpAddr,10102);
c.MulticastLoopback=true;
c.JoinMulticastGroup(m_GrpAddr);
c.Send(sendBytes,sendBytes.Length,ep);
Console.WriteLine(message);
}
public string recv_multicast()
{
Console.WriteLine("was here");
String strData = "";
//String Ret = "";
ASCIIEncoding ASCII = new ASCIIEncoding();
UdpClient c = new UdpClient(10101);
// Establish the communication endpoint.
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 10101);
IPAddress m_GrpAddr = IPAddress.Parse("224.0.0.1");
c.JoinMulticastGroup(m_GrpAddr);
Byte[] data = c.Receive(ref endpoint);
strData = ASCII.GetString(data);
//Ret += strData + "\n";
return strData;
}
端口有什么问题吗?
recv 方法被阻止但没有收到消息?
在wireshark中,我可以看到从本地地址端口10102到224.0.0.1 dest_port 0的消息,但是recv没有从多播地址获取消息。
顺便说一句,我在同一台计算机上运行两个实例。参考:http: //msdn.microsoft.com/en-us/library/ekd1t784.aspx
**得到解决方案:在发送例程中
IPEndPoint ep = new IPEndPoint(m_GrpAddr,10102);
应该
IPEndPoint ep = new IPEndPoint(m_GrpAddr,10101);
收货港**