Random behaviour trying to connect TCP/wifi/ad-hoc
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 开发者_Go百科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.
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.
精彩评论