Windows Phone 7 ResolveHostNameAsync
The class DeviceNetworkInformation packaged within namespace Microsoft.Phone.Net.NetworkInformation provides two overloaded methods to resolve hostname asynchronously.
The overloads differ such that former allows you to specify the network interface using which hostname needs be resolved where as earlier will use the first avialable network interface in the list (If both WiFi and Ethernet are available then my understanding is that Ethernet will take precedence)
Here is a simple app demonstrating how to retrieve IPs against hostname.
Note: Since call to method (ResolveHostNameAsync) is asynchronous in nature, the foreground thread won’t let you interact with the UI interface in the callback method nameResolutionCallback(NameResolutionResult result). That's why you will see that interaction with UI is managed by call to dispatcher. However a more sophisticated approach will be to execute this call on the background thread.Code behind
private void btnResolveHostName_Click(object sender, RoutedEventArgs e)
{
if(!string.IsNullOrEmpty(txtHostName.Text))
{
DnsEndPoint endPoint = new DnsEndPoint(txtHostName.Text, 80);
Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.ResolveHostNameAsync(endPoint, nameResolutionCallback, new object());
}
}
{
if (result.NetworkErrorCode == NetworkError.Success)
{
List<string> items = new List<string>();
items.Add(string.Format("Network Interface Type : {0}", result.NetworkInterface.InterfaceType)); foreach (IPEndPoint ipEndPoint in result.IPEndPoints)
{
items.Add(string.Format("Resvoeld IP : {0}:{1}", ipEndPoint.Address.ToString(), ipEndPoint.Port.ToString()));
} Deployment.Current.Dispatcher.BeginInvoke(() =>
{
lstInfo.ItemsSource = items;
txtHostName.Focus();
});
}
else
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(result.NetworkErrorCode.ToString(), "Error", MessageBoxButton.OK);
txtHostName.Focus();
});
}
}
XAML
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="Resolve Hostname" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel> <!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBox Name="txtHostName" Width="300" Margin="6,-5,150,623" Text="google.com"></TextBox>
<Button Name="btnResolveHostName" Margin="300,-5,-5,623" Click="btnResolveHostName_Click">Resolve</Button>
<ListBox Name="lstInfo" Margin="12,79,6,39" BorderThickness="1" BorderBrush="Azure"></ListBox>
</Grid>
</Grid>
EVDO network Interface is not listed by WireShark
Background Context
I am using EVDO broadband wireless Usb stick launched by Pakistan Telecommunication Limited (a 3.1 Mbps device). It is based on "CDMA2000" 3G wireless standard. It works well for me since I am at a rather lesser IT aware populated location. I came across an issue today while trying to capture network traffic on top of EVDO stick. I couldn't find much on this topic so I decided to write a post on this.
The Problem
I was able to get the network interface against EVDO using System.Net.NetworkInformation namespace,
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { ... }
as following,
"WAN (PPP/SLIP) Interface"
However this interface was not listed at all by either WireShark or by SharpPCap (which I was using to get Network Devices list) so I wasn’t able to send/receive packets. I was running on,OS: Windows XP (x86)
with following,
WinPCap: 4.1.1
WireShark: Version 1.2.8 (SVN Rev 32676)
SharpPCap: SharpPcap-3.5.0
Solution
Since WAN (PPP/SLIP) interface is no exception I was forced to think that the issue is not due to usb modem but rather with WinPCap. I goggled it a bit and identified that PPP capturing is possible on Windows XP (x86) with WinPCap last stable release version (at the time of this writing) 3.1 which can be downloaded from,
http://www.winpcap.org/archive/3.1-WinPcap.exe
- I uninstalled 4.1.1 and reverted back to 3.1 and on the go, both network interfaces (Generic dialup adapter & WAN (PPP/SLIP interface) ) were listed relative to EVDO under WireShark and using SharpPCap (since it utilizes WinPCap).
Note: As per initial tests, I was able to capture traffic using Wireshark but while trying to open either of these interfaces using SharpPCap, I was getting an exception at device.Open(),
"Unable to open the adapter (rpcap://\Device\NPF_GenericDialupAdapter). Error opening adapter: The system cannot find the device specified. (20)"
OR
Unable to open the adapter (rpcap://\Device\NPF_{F9409E20-55C1-43AD-992A-10929B.....}). Error opening adapter: The system cannot find the device specified. (20)
However since this post is primarily relative to listing of interfaces, the resolution was found. I will speak more on the issue of communication with EVDO device using SharpPCap in another blog post.
References
More details on the topic of PPP capturing can be found at,

