How do I get the Local Network IP address of a computer programmatically?

后端 未结 5 842
萌比男神i
萌比男神i 2020-11-28 06:10

I need to get the actual local network IP address of the computer (e.g. 192.168.0.220) from my program using C# and .NET 3.5. I can\'t just use 127.0.0.1 in this case.

<
5条回答
  •  感情败类
    2020-11-28 06:58

    As a machine can have multiple ip addresses, the correct way to figure out your ip address that you're going to be using to route to the general internet is to open a socket to a host on the internet, then inspect the socket connection to see what the local address that is being used in that connection is.

    By inspecting the socket connection, you will be able to take into account weird routing tables, multiple ip addresses and whacky hostnames. The trick with the hostname above can work, but I wouldn't consider it entirely reliable.

提交回复
热议问题