2012-04-27 67 views
1

我無法收到我自己的數據包在多播上發送。我創建了兩個Udpclient接收器,接收多播組上的數據包,發送者發送數據包。我的包被髮送到組,但我不能接收回來的數據包通過我發送....我無法收到我自己的Mcast數據包C#UdpClient

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); 

回答

0

在我看來,所有你需要做的是你的EndReceive後再次調用BeginReceive。否則,你只會得到1條消息,不會看到其他消息...

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); 
} 
相關問題