C#SocketException与XP上的多播 [英] C# SocketException with Multicast on XP

查看:135
本文介绍了C#SocketException与XP上的多播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下C#代码在Vista上可以正常运行,但在具有以下功能的XP上无法运行:

The following C# code works correctly on Vista, but fails on XP with:

SocketException:提供了无效的参数.

SocketException: An invalid argument was supplied.

错误代码为10022.

The ErrorCode is 10022.

XP上的防火墙已被禁用.

The firewall on XP has been disabled.

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选项之前,您需要将套接字绑定到接口.

You need to bind the socket to an interface before setting the SocketOptionName.AddMembership option.

刚刚在MSDN文档中对此进行了验证(尽管它最多只显示NT4):

Just verified this in the MSDN docs (though it says only up to NT4):

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

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

这篇关于C#SocketException与XP上的多播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆