使用套接字的 Winforms C# 应用程序在 winXp 下工作,但在 Windows 7 下抛出错误
这是连接的属性和方法。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许在 Win7 上您会获得 IPv6 作为 _hostIpAddress。实例化套接字时尝试使用类似的内容:
Perhaps on Win7 you get a IPv6 as the _hostIpAddress. Try using something like this when instantiating the socket: