开发者

How to get ip address for the local system

Hi friends. I am developing an application in which I am returning the IP address for a particular system and saving it in a database. When I check the database it is showing the server ip address. I want get the particular ip address who is running that application.

So far I have this code:

开发者_开发知识库  Public Function GetIPAddress() As String
      Dim strHostName As String = System.Net.Dns.GetHostName()
      Dim strHostName1 As String = System.Net.Dns.GetHostName()
      Dim ipHostInfo As System.Net.IPHostEntry = System.Net.Dns.Resolve(System.Net.Dns.GetHostName())
      Dim ipAddress As System.Net.IPAddress = ipHostInfo.AddressList(0)
      Return ipAddress.ToString()
  End Function


Keep in mind that a machine may have multiple IP addresses pointing to it, so this isn't really possible. Well, not in theory anyway - in practice, you may be able to hack something together.


That's undoubtedly because your code is running on the server itself, not the client. Hence, when you ask for the IP address, it faithfully gives it to you.

Any solution running on the server (such as examining the source IP address of the TCP session) has to keep in mind that, with firewalls and network address translation and all the other wonderful features of networking that protect us from the baddies, the address you get may bear little resemblance to the actual IP address of the client machine.

You need to get code running on the client machine that can basically do the same thing. And keep in mind there that a client may have many IP addresses allocated to it.

On my desktop machine alone, I have two physical NICs, each with two addresses and multiple virtual NICs for VPNs and VMWare images. Granted, my desktop box is not your ordinary home PC, but it's not out of the realms of possibility for a box to have two addresses.

And, in fact, a single machine can quite easily change its IP address if on DHCP and it decides to release the lease on its address and get another one.

All in all, the IP address is not a very reliable indicator of which machine is doing things. Perhaps if you step back and give us more details on what you're trying to achieve, we can help further.


Use the code below to get the ip of the client system.

string host=System.Net.Dns.GetHostName();

string ip = System.Net.Dns.GetHostByName(host).AddressList[0].ToString();

Here the ip address give the client ip address.


Perhaps this would help? - https://stackoverflow.com/a/23824592/2647808

It looks like the best way to do it is with System.Net.Dns.GetHostName(). You can then extract either the IPv4 address or IPv6 address with a For loop and If statement. Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜