Socket.Bind for Multi NIC Setup For Connecting, not Listening.

  • 14 years ago

    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'.


    3Com NIC, hooked up to my ADSL Router [ip:, External Ip: 83.160.*].
    LinkSys 802.11b/g wlan usb adapter, connected to a different ADSL Router [ip:, 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:








    Public Class MultiIP

    Public Function AvailableIPs() As System.Net.IPAddress()

    Dim strMachineName As String

    'Get the Host Name

    strMachineName = Dns.GetHostName()


    "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


    String.Format("IP Addresses {0}: {1} ", count, ipAddr(count).ToString))


    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 = ""

    Dim S As New System.Net.Sockets.Socket(IP.AddressFamily, SocketType.Stream, ProtocolType.Tcp)


    New System.Net.IPEndPoint(IP, 0))


    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 = ""


    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


    Return "Unable to resolve"

    End If

    ' Return page

    End Function

    End Class




    Main Form:


    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.


    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click

    ' Refresh the available IP list.


    End Sub

    Private Sub RefreshIPs()

    ' Remove the current selection

    cboIPs.SelectedIndex = -1

    ' Clear all present items


    ' 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


    End If


    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))


    lblExternalIP.Text = eIP

    End Sub



Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“You can stand on the shoulders of giants OR a big enough pile of dwarfs, works either way.”