使用套接字的 Winforms C# 应用程序在 winXp 下工作,但在 Windows 7 下抛出错误

发布于 2024-10-05 00:52:37 字数 1256 浏览 3 评论 0原文

这是连接的属性和方法。

protected Socket _socketConnection =
            new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
private string _host = "";
private string _hostIpAddress = "";
private int _port = 0;

  public void Connect()
        {
            // don't allow two connections
            if (_socketConnection.Connected)
                return;


            // get the ip address from the hostname
            IPHostEntry ipHostEntry = Dns.GetHostByName(_host);
            _hostIpAddress = ipHostEntry.AddressList[0].ToString();

            // create the socket endpoint
            IPAddress ipAddress = IPAddress.Parse(_hostIpAddress);
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, _port);

            // connect
            try
            {
                _socketConnection.Connect(ipEndPoint);
                if (OnConnect != null)
                    OnConnect();
            }
            catch
            {
                throw;
            }
        }

当我在 Windows 7 下运行该应用程序时,出现以下错误:

在 getsockopt 或 setsockopt 调用中指定了未知、无效或不受支持的选项或级别。

我看到过有关在套接字上设置特定选项的消息,但这是一个已经运行多年的应用程序,并且仅当该应用程序安装在 Windows 7 上时才会发生。

是否有兼容性标志需要调整或其他什么?

谢谢!

Here's the properties and the method that connects.

protected Socket _socketConnection =
            new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
private string _host = "";
private string _hostIpAddress = "";
private int _port = 0;

  public void Connect()
        {
            // don't allow two connections
            if (_socketConnection.Connected)
                return;


            // get the ip address from the hostname
            IPHostEntry ipHostEntry = Dns.GetHostByName(_host);
            _hostIpAddress = ipHostEntry.AddressList[0].ToString();

            // create the socket endpoint
            IPAddress ipAddress = IPAddress.Parse(_hostIpAddress);
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, _port);

            // connect
            try
            {
                _socketConnection.Connect(ipEndPoint);
                if (OnConnect != null)
                    OnConnect();
            }
            catch
            {
                throw;
            }
        }

When I run the app under Windows 7 I get the following error:

An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call.

I've seen messages that talk about setting a particular option on the socket, but this is an app that has been working for years and is only happening when this app is installed on Windows 7.

Is there a compatibility flag to tweak or something?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尤怨 2024-10-12 00:52:37

也许在 Win7 上您会获得 IPv6 作为 _hostIpAddress。实例化套接字时尝试使用类似的内容:

if(Socket.OSSupportsIPv6 && _hostIpAddress.AddressFamily == AddressFamily.InterNetworkV6) 
{
   // newer OS
   _socketConnection = new Socket(
       AddressFamily.InterNetworkV6, 
       SocketType.Stream, 
       ProtocolType.Tcp);
   _socketConnection.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);
} else { 
   // older OS
   _socketConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}

Perhaps on Win7 you get a IPv6 as the _hostIpAddress. Try using something like this when instantiating the socket:

if(Socket.OSSupportsIPv6 && _hostIpAddress.AddressFamily == AddressFamily.InterNetworkV6) 
{
   // newer OS
   _socketConnection = new Socket(
       AddressFamily.InterNetworkV6, 
       SocketType.Stream, 
       ProtocolType.Tcp);
   _socketConnection.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);
} else { 
   // older OS
   _socketConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文