Get specific Inerface / IP from a Remte IP

G

Guest

I am using a toolkit, part of which creates TCP/IP connections using a
function 'Connect(<local IP>,<local Port>,<Remote IP>,<Remote Port>)'. This
function's signiture looks very similar to the Socket object signiture. If
you pass a blank local IP it will get the IP for the DEFAULT interface, which
is not always the desired IP. How can I get the specific Local IP from a
Remote IP?

EX:

Interface 1: 10.1.1.1
Interface 2: 192.168.1.1

..Connect("",0,"10.1.1.2",104)
Sets local ip to 10.1.1.1

..Connect("",0,"192.168.1.2",104)
Sets local ip to 10.1.1.1

I would like to pass the Local IP form the remote IP instead of the blank
string. Any help would be great!

~rlc
 
G

Guest

Well here is my solution, hopefully I can have them update the toolkit to
avoid extra connections....

Dim l_Socket As System.Net.Sockets.Socket
Dim l_RemoteIP As System.Net.IPAddress
Dim l_RemoteEP As System.Net.IPEndPoint
Dim l_LocalEP As System.Net.IPEndPoint
Dim l_LocalIP As String

Try

l_RemoteIP = System.Net.IPAddress.Parse(txtRemoteIP.Text)
l_RemoteEP = New System.Net.IPEndPoint(l_RemoteIP, 104)
l_Socket = New
System.Net.Sockets.Socket(Sockets.AddressFamily.InterNetwork,
Sockets.SocketType.Stream, Sockets.ProtocolType.Tcp)
l_Socket.Connect(l_RemoteEP)

l_LocalEP = l_Socket.LocalEndPoint

'get the local IP
l_LocalIP = l_LocalEP.Address.ToString


Catch ex As Exception
'failed to connect do something here

Finally
If l_Socket.Connected Then
l_Socket.Close()
End If
End Try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top