【问题标题】:C# SocketException with Multicast on XP在 XP 上使用多播的 C# SocketException
【发布时间】:2010-10-01 00:02:49
【问题描述】:

以下 C# 代码在 Vista 上正常工作,但在 XP 上失败:

SocketException:提供了一个无效的参数。

错误代码是 10022。

XP 上的防火墙已被禁用。

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

有什么想法吗?

【问题讨论】:

    标签: c# sockets multicast


    【解决方案1】:

    在设置 SocketOptionName.AddMembership 选项之前,您需要将套接字绑定到接口。

    编辑:刚刚在 MSDN 文档中验证了这一点(尽管它说最多只能达到 NT4):

    Windows 98、Windows NT 4.0 平台注意:在使用 AddMembership 作为 optionName 参数之前,您必须调用 Bind 方法。

    【讨论】:

    • 为什么这会导致 XP 问题,但在 Vista 上却没有?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 2013-03-29
    • 2023-01-04
    • 2010-09-12
    相关资源
    最近更新 更多