尝试连接 TCP/wifi/ad-hoc 的随机行为
我正在尝试使用 OpenNETCF.Net 将我的 PDA 连接到临时网络。我想强制设置链接本地地址,而不是依赖后备机制,因为这需要太长时间。然后我想打开到另一个设备的 TCP 连接。我真的很困惑,因为关于如何完成此操作的信息似乎很少,尤其是执行这些步骤的顺序。
我的尝试有时会起作用:
var adp = NetworkInterface.GetAllNetworkInterfaces().
First(v => v is WirelessZeroConfigNetworkInterface)
as WirelessZeroConfigNetworkInterface;
if (adp.AssociatedAccessPoint != selectedAP.Name) {
adp.AddPreferredNetwork(selectedAP);
adp.ConnectToPreferredNetwork(selectedAP.Name);
}
adp.Unbind();
adp.CurrentIpAddress = IPAddress.Parse("169.254.1." + (new Random()).Next(2, 254).ToString());
adp.CurrentSubnetMask = IPAddress.Parse("255.255.0.0");
adp.Rebind();
顺便说一句,我删除了 CurrentIpAddress 和 CurrentSubnetMask 设置器中的 Rebind 调用,只需手动执行 1 次即可。看起来没什么区别,只是速度快了一点。
结果各不相同。有时所有呼叫似乎都成功,但我仍然连接到同一个 SSID,而不是我想要连接的 SSID。有时我会收到“无法查询 WZC 接口”、“未找到首选列表”或“无法设置 WZC 接口”的信息。有时无线适配器的索引会发生变化。
然而,有时一切都很好。 TCP 连接打开并按预期工作。但更常见的是,我在尝试写入流时收到 SocketExceptions。
一切都是在不涉及 Activesync 的情况下完成的,调试完全是文本文件/消息框:( 如果有人可以描述我应该采取的步骤,我会非常高兴。
I'm trying to connect my PDA to an ad-hoc network using OpenNETCF.Net. I want to force setting a link-local address and not rely on a fallback mechanism because that takes a little too long. Then I want to open a TCP connection to another device. I'm really confused as there seems very little information on how to get this done, especially the order in which these steps should be performed.
My attempt, that sometimes works:
var adp = NetworkInterface.GetAllNetworkInterfaces().
First(v => v is WirelessZeroConfigNetworkInterface)
as WirelessZeroConfigNetworkInterface;
if (adp.AssociatedAccessPoint != selectedAP.Name) {
adp.AddPreferredNetwork(selectedAP);
adp.ConnectToPreferredNetwork(selectedAP.Name);
}
adp.Unbind();
adp.CurrentIpAddress = IPAddress.Parse("169.254.1." + (new Random()).Next(2, 254).ToString());
adp.CurrentSubnetMask = IPAddress.Parse("255.255.0.0");
adp.Rebind();
By the way, I removed the Rebind calls in the setters of CurrentIpAddress and CurrentSubnetMask and just do 1 manually. Doesn't seem to make a difference, except it's a bit faster.
The results vary. Sometimes all the calls seem to succeed, but I'm still connected to the same SSID and not the one I want to connect to. Sometimes I get "Unable to Query WZC Interface", "No preferred list found", or "Unable to Set WZC Interface". Sometimes it seems the index of the wireless adapter changes.
However, sometimes everything works fine. The TCP connection opens and works as expected. More often though, I'm getting SocketExceptions when trying to write to the stream.
Everything is done without Activesync being involved, debugging is completely text files/messageboxes :(
I'd be very happy if someone could describe the steps I should take.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终我们尝试使用不同的 PDA,它似乎总是工作得很好。无论如何,该程序的整个 Wi-Fi 功能都被清除了,因为似乎没有市场兴趣。
Eventually we tried with a different PDA, which always seems to work just fine. The entire Wi-Fi functionality of the program was purged anyway, as there seems to be no market interest.