2012-03-10 39 views
2

送信と受信の両方で同じポート(9050など)を使用する場合にのみ動作しますが、同時に複数のクライアントで同じマルチキャストを受信する方法コンピューター?それは、複数回、私はudpClientクラスでfalseにExclusiveAddressUseプロパティを設定し、複数のクライアント.netコードのudpマルチキャストポート

http://codeidol.com/csharp/csharp-network/IP-Multicasting/Csharp-IP-Multicast-Support/

using System; 
    using System.Net; 
    using System.Net.Sockets; 
    using System.Text; 
    class UdpClientMultiSend 
    { 
     public static void Main() 
     { 
     UdpClient sock = new UdpClient(); 
     IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050); 
     byte[] data = Encoding.ASCII.GetBytes("This is a test message"); 
     sock.Send(data, data.Length, iep); 
     sock.Close(); 
     } 
    } 




using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
class UdpClientMultiRecv 
{ 
    public static void Main() 
    { 
    UdpClient sock = new UdpClient(9050); 
    Console.WriteLine("Ready to receive…"); 
    sock.JoinMulticastGroup(IPAddress.Parse("224.100.0.1"), 50); 
    IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0); 
    byte[] data = sock.Receive(ref iep); 
    string stringData = Encoding.ASCII.GetString(data, 0, data.Length); 
    Console.WriteLine("received: {0} from: {1}", stringData, iep.ToString()); 
    sock.Close(); 
    } 
} 

答えて

0

で同じポートを使用する場合、同じポートを使用してのソケットエラーを作成します。

使用方法の詳細については、documentationを参照してください。

+0

で送信または受信しますか? –

+0

私は両方ともそれが既に偽であることを確認しました –

3

がこの投稿への回答を参照してください: Connecting two UDP clients to one port (Send and Receive)

あなたが結合する前にソケットオプションを設定する必要があります。

static void Main(string[] args) 
{ 
    IPEndPoint localpt = new IPEndPoint(IPAddress.Any, 6000); 

    UdpClient udpServer = new UdpClient(); 
    udpServer.Client.SetSocketOption(
     SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 
    udpServer.Client.Bind(localpt); 

    UdpClient udpServer2 = new UdpClient(); 
    udpServer2.Client.SetSocketOption(
     SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 

    udpServer2.Client.Bind(localpt); // <<---------- No Exception here 

    Console.WriteLine("Finished."); 
    Console.ReadLine(); 
} 
+0

これは 'JoinMulticastGroup'がなくても動作します、ありがとう! – Val