2009-01-12 11 views
5

Der folgende C# -Code funktioniert ordnungsgemäß auf Vista, aber nicht auf XP mit:C# Socket mit Multicast auf XP

Socket: ein ungültiges Argument geliefert wurde.

Der Error-Code ist 10022.

Die Firewall auf XP deaktiviert wurde.

using System; 
using System.Net; 
using System.Net.Sockets; 

public class SocketTest 
{ 
    [STAThread] 
    public static void Main() 
    { 
     MySocket s = new MySocket(); 

     Console.WriteLine("done"); 
     Console.ReadLine(); 
    } 

    public class MySocket : Socket 
    { 
     public const ushort SocketTtl = 4; 
     public const string Address = "239.255.255.250"; 

     public IPAddress IPAddress = IPAddress.Parse(Address); 

     public MySocket() 
      : base(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) 
     { 
      SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); 
      SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, SocketTtl); 

      // This line throws SocketException: An invalid argument was supplied 
      // SocketException.ErrorCode: 10022 
      SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress)); 
     } 
    } 
} 

Irgendwelche Ideen?

Antwort

8

Sie müssen den Socket an eine Schnittstelle binden, bevor Sie die SocketOptionName.AddMembership-Option festlegen.

Edit: Gerade dies in der MSDN-Dokumentation überprüft (auch wenn es nur bis zu NT4 sagt):

Windows 98, Windows NT 4.0-Plattform Hinweis: Sie müssen die Bind-Methode aufrufen, bevor AddMembership als option mit Parameter.

+0

Warum verursacht dies ein XP Problem, aber nicht auf Vista? – Jeffrey

+0

Süß - Frage beantwortet. – Jeffrey