I also posted this quesion in the Microsoft Newsgroup, but stuck out on an answer. [link]
My problem is that i am unable to bind a socket to a Local IP Endpoint that is not 'my current link to the outside world'.
Overview:
3Com NIC, hooked up to my ADSL Router [ip: 192.168.1.33, External Ip: 83.160.*].
LinkSys 802.11b/g wlan usb adapter, connected to a different ADSL Router [ip: 10.0.0.5, External IP: <can't remember, but also an online ip>]
I made a small tool that would allow me to pick one of the IP's that are available on my system (as long as they are IPv4), bind to it (allowing the .NET framework to pick a port), connecting to a webserver, pulling some HTML data, and return a part of it to the Main Form.
This works for my main NIC, but it will hang on Socket.Connect (right after bind), when i unplug my LAN, and the WLAN becomes the main source for internet access, it works for the WLAN card...
The Main form only has a ComboBox named 'cboIPs', a Label named 'lblExternalIP' and two Buttons named 'btnRefresh' and 'btnGetExternal'.
Here's the source i used:
Class:
Imports
System.Diagnostics
Imports
System.net
Imports
System.net.sockets
Namespace
NKCSS.NET
Public Class MultiIP
Public Function AvailableIPs() As System.Net.IPAddress()
Dim strMachineName As String
'Get the Host Name
strMachineName = Dns.GetHostName()
Debug.WriteLine(
"Host Name: " + strMachineName)
'Get the Host by Name
Dim ipHost As IPHostEntry
ipHost = Dns.GetHostEntry(strMachineName)
'Get the list of addresses associated with the host in an array
Dim ipAddr() As System.Net.IPAddress = ipHost.AddressList
Dim count As Integer
'Enumerate the IP Addresses
For count = 0 To ipAddr.Length - 1
Debug.WriteLine(
String.Format("IP Addresses {0}: {1} ", count, ipAddr(count).ToString))
Next
Return ipAddr
End Function
Public Function GetExternalIP(ByVal IP As IPAddress) As String
If Not IP.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then Throw New Exception("IPv4 Only for now!")
Dim Server As String = "www.whatismyip.com"
Dim S As New System.Net.Sockets.Socket(IP.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
S.Bind(
New System.Net.IPEndPoint(IP, 0))
System.Diagnostics.Debug.WriteLine(S.LocalEndPoint)
S.Connect(Server, 80)
Dim Req As String = String.Format("GET / HTTP/1.1{0}Host: {1}{0}Connection: Close{0}{0}", vbCrLf, Server)
Dim SendBuffer As [Byte]() = System.Text.Encoding.ASCII.GetBytes(Req)
Dim ReceiveBuffer(255) As [Byte]
S.Send(SendBuffer, SendBuffer.Length, 0)
Dim BytesLeft As Int32
Dim page As String = ""
Do
BytesLeft = S.Receive(ReceiveBuffer, ReceiveBuffer.Length, 0)
page &= System.Text.Encoding.ASCII.GetString(ReceiveBuffer, 0, BytesLeft)
Loop While BytesLeft > 0
Dim Pattern As String = "<title>whatismyip\.com\s-\s(?<ip>.*?)</title>"
Dim Regex As New System.Text.RegularExpressions.Regex(Pattern)
Dim Match As System.Text.RegularExpressions.Match = Regex.Match(page.ToLower)
If Match.Success Then
Return Match.Groups("ip").Value
Else
Return "Unable to resolve"
End If
' Return page
End Function
End Class
End
Namespace
Main Form:
Public
Class Form1
Dim i As New NKCSS.NET.MultiIP
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Refresh the available IP list.
RefreshIPs()
End Sub
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
' Refresh the available IP list.
RefreshIPs()
End Sub
Private Sub RefreshIPs()
' Remove the current selection
cboIPs.SelectedIndex = -1
' Clear all present items
cboIPs.Items.Clear()
' Walk thru all the ip's
For Each IP As System.Net.IPAddress In i.AvailableIPs()
' Only add IPv4 addresses for now...
If IP.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
cboIPs.Items.Add(IP)
End If
Next
End Sub
Private Sub btnGetExternal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetExternal.Click
Dim eIP As String = i.GetExternalIP(CType(cboIPs.SelectedItem, System.Net.IPAddress))
Debug.WriteLine(eIP)
lblExternalIP.Text = eIP
End Sub
End
Class
No one has replied yet! Why not be the first?
Sign in or Join us (it's free).