加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

在XP上使用Multicast的C#SocketException

发布时间:2020-12-14 02:02:16 所属栏目:Windows 来源:网络整理
导读:以下C#代码在Vista上正常运行,但在XP上失败: SocketException:提供了无效的参数. ErrorCode是10022. XP上的防火墙已被禁用. using System;using System.Net;using System.Net.Sockets;public class SocketTest{ [STAThread] public static void Main() { M
以下C#代码在Vista上正常运行,但在XP上失败:

SocketException:提供了无效的参数.

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

有任何想法吗?

解决方法

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

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

Windows 98,Windows NT 4.0 Platform Note: You must call the Bind method before using AddMembership as the optionName parameter.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读