本来问题是这样的:
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, ???);==========================后来有位高手告诉我:
int optionValue = (int)IPAddress.HostToNetworkOrder(第几块网卡);
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, optionValue );========================
我有两个网卡,一个是192.168.1.27
另一个是192.168.2.27
========================
试一试这位朋友的方法:
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, 0);果然可以,其中绑定的IP是192.168.1.27。我想如果要绑定第二个IP(2.27)
只要用SetSocketOption(×,×,1)就行了。结果跟本不是这么回事,用1做参数总是绑定失败。
为什么啊???

解决方案 »

  1.   

    192.168.2.27这个网卡处于Alive状态而且我现在上网用的就是这个2.27.
      

  2.   

    int optionValue = (int)IPAddress.HostToNetworkOrder(第几块网卡);
    注意这一行,转换了整数的字节序,如果是0,转不转都是一样的,但如果是1,就不一样了
      

  3.   

    还是有错,但错信息不一样了。现在提示:套接字操作尝试一个无法连接的主机我想问一下: “第几块网卡”这个变量就是:IPAddress[] ipHost = Dns.GetHostAddresses(Dns.GetHostName());
    这块网卡的IP在ipHost中所处的序数吧? Yes or No? thx
      

  4.   

    这是MSDN里的例子,你看一下吧
    The following sample code shows you how to use SetSocketOption with the MulticastInterface socket option name. Type or paste the following code in the Main function of a new Microsoft Visual C# .NET console application: 
    int defaultPort = 5050;
    string localName = Dns.GetHostName();IPHostEntry hostEntry = new IPHostEntry();
    hostEntry = Dns.GetHostByName(localName);
    IPAddress localAddress = hostEntry.AddressList[0];Socket mcastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);// Bind the socket to default IP address and port.
    mcastSocket.Bind(new IPEndPoint(localAddress,defaultPort));Console.Write("\nSelect Adapter for outgoing Multicast packets (Adapter Index) :  ");
    int index = int.Parse(Console.ReadLine());
    int optionValue = (int)IPAddress.HostToNetworkOrder(index);Console.Write("\nMulticast Address - To add membership : ");
    IPAddress mcastAddress = IPAddress.Parse(Console.ReadLine());

    Console.Write("\nPort number - Where Multicast members are listening : ");
    int mcastPort = int.Parse(Console.ReadLine());
    MulticastOption mcastOpt = new MulticastOption(mcastAddress,localAddress);

    // Add membership to the group.
    mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOpt);

    // Set the required interface for outgoing multicast packets.
    mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, optionValue);// Send multicast packets.
    string data = "This is a multicast packet";
    mcastSocket.SendTo(ASCIIEncoding.ASCII.GetBytes(data), new IPEndPoint(mcastAddress,mcastPort));
      

  5.   

    老大,你能告诉我这些资料在MSDN的哪个地方啊?我只会在MSDN的SetSocketOption的介绍里找,里面没有这么详细的Example,
    请问你是如何在Msdn里找到这些资料的,这样我以后也知道去找如何在msdn里更多的资料,谢谢:)
      

  6.   

    搜索 关键字 比如MulticastInterface
      

  7.   

    太感谢老大你了!以前我只会用Index,原来用Search能找到更多的东西啊!
    谢谢