使用 VB.Net 代码的 IP 地址
Whatismyip.com 提供什么样的 IP 地址?
如何使用 VB.Net 代码获取它?
还有IP端口是什么?
谢谢弗尔坎
What kind of IP address does whatismyip.com provide?
How can I get it using VB.Net code?
Also what is IP port?
Thanks
Furqan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我还需要使用命令行的外部 IP,但因为我没有找到它,所以我使用 vb.net 编写了小应用程序。您可以对源代码使用反射或在应用程序上询问 主页。基本上,应用程序会打开提供您的 IP 的网页并使用正则表达式对其进行解析,但因为是为此目的而设计的,因此使用了许多“技巧”(可以一次使用更多网页,使用快速页面等)。检查来源了解详细信息。
I also needed external IP using command line, but because I didn't find it I wrote small application using vb.net. You can use reflection for source code or ask on app home page for it. Basically application opens web page that provide your IP and parse it using regular expression, but because is designed with this purpose uses many "tricks" for this (can use more web pages at once, uses fastes page, etc). Check source for details.
第 1 部分
好吧,让我们假设您家里有一个路由器,并且您家里有多台计算机都通过路由器连接到互联网。
为了让路由器了解网络上的流量,它会为家庭网络上的所有计算机分配唯一的 IP 地址(通常以 192.168.xx 开头)。这些 IP 地址是本地 IP 地址,这意味着只有您的路由器和家中连接到它的计算机/设备知道它们。如果您打开命令提示符并执行命令 IPConfig,您将看到路由器为您的计算机分配的 IP 地址。
那么 WhatIsMyIP.com 向您显示的 IP 地址是什么?与您的路由器为网络上的所有计算机分配地址的方式大致相同,您的互联网服务提供商向其所有客户分发唯一的 IP 地址。现在,因为您有一个路由器,所以 ISP 在您的网络上唯一能看到的就是该路由器和您的 ISP 为其分配的 IP 地址。这就是为什么路由器也被称为硬件防火墙,因为在它的另一边的人无法知道有多少计算机或设备连接到它。
这意味着,当您访问互联网上的网站时,他们看到的唯一 IP 地址是您的路由器的外部 IP 地址(由 ISP 分配的地址)。因此,无论您使用家里的哪台计算机,该网站都不会知道其中的差异,因为它所能看到的只是您路由器的 IP 地址。来吧,尝试一下吧;在您家中的多台不同计算机上访问 www.WhatIsMyIP.com。您将看到它们都显示相同的 IP 地址。但是,如果您在每台计算机上的命令提示符中执行了 IPConfig,则会显示路由器分配的本地地址,并且该地址在您家中的每台计算机上都会有所不同。
那么,既然您了解了本地 IP 地址和外部 IP 地址之间的区别,那么如何在 PC 上运行的 VB 或 C# .net 代码中检索外部 IP 地址呢?那么,您的计算机实际上知道的唯一 IP 地址是我们讨论的本地 IP。查看外部 IP 地址的唯一方法是访问一个网站,该网站会告诉您请求来自哪个地址(这将是您的路由器的 IP 地址)。
您需要做的是在 VB.net 程序中编写一些代码,导航到 WhatIsMyIP.com(或可以为您提供 IP 地址的其他网站)并告诉代码获取它。我编写了一个网络服务,位于 http://www.u413.com/test/terminal/myip 仅返回您的 IP 地址作为整个 HTTP 响应。不过,为您的应用程序找到类似的东西,因为这个小样本不会永远留在那里;我只是将其作为我已经拥有的域的临时示例。
访问 http://www.vbdotnetheaven.com/UploadFile/kbawala/WebRequestClass04182005054320AM/WebRequestClass。 aspx 查看如何从计算机上运行的代码发出 Web 请求。
注意:如果您不了解 IP 地址的工作原理,您可能也不知道 DNS 是什么。网络上的所有东西都有一个 IP 地址,包括提供网站页面的服务器。但是,尝试记住所有您喜爱的网站的最多 12 位 IP 地址是一件多么痛苦的事情。这就是 DNS 服务器的发明目的。 DNS 服务器获取域名(例如 www.facebook.com)并将其转换为正确的 IP 地址。这样,您只需记住 facbook.com,而不是 69.63.181.12(这是 facebook 的 IP 地址。来吧,尝试一下!将该 IP 放入浏览器的地址栏中,您就会看到 facebook。),域名就容易多了记住!
如果您想查看与网站关联的 IP 地址,请打开命令提示符。提示打开后,输入 PING [网站域名](例如 PING Facebook.com),您的计算机将向为您显示的地址发送 4 个测试请求。
第 2 部分
让我们假设您的 IP 地址就像公寓大楼的地址。披萨送货员需要知道公寓楼的地址才能送货给您。但当他到了那里之后他要做什么呢?有数百间门/公寓可供选择。他需要知道公寓号码(计算机上的端口号)。
您的计算机有数千个端口,程序可以在其中任何一个端口上侦听来自外界的请求。当您访问网站时,几乎所有网站都在端口 80 上提供服务。端口 80 是网页的默认端口。当您访问 facebook.com 时,您实际上是访问 facebook.com:80,只是看不到 :80,因为它是默认值,因此是隐含的。如果我建立一个网络服务器,我可以决定为网站提供服务的任何端口。如果我在端口 80 以外的端口上提供网页,那么您必须将其包含在您的 URL 中。 http://www.SomeDudesCustomWebServer.com:1337。
传出请求也使用端口,但该端口通常并不重要,您的计算机只会选择可用的端口。因此,当您访问 Facebook.com 时,Facebook 网络服务器都通过端口 80 提供页面,但您的计算机打开用于发送请求的端口不必是端口 80,因为它会选择一个可用端口,然后发送端口与请求。然后,当 facebook 发送响应时,它会将回复发送回发出请求的 IP 地址和端口。
传出端口仅在请求期间使用。必须侦听连接的端口必须保持不变,否则发出请求的计算机将不知道将请求发送到哪个端口。
容易啊!
希望能帮助您更好地理解。
编辑:
端口转发
好吧,根据您想要使用/创建的聊天应用程序,如果您希望它通过网络进行通信,您必须了解端口转发。基本上,因为您所能看到的朋友网络就是他的外部 IP 地址,所以您必须使用该地址连接到他的聊天服务器(反之亦然,如果他连接到您的聊天服务器,那么它将是您的外部 IP )。因此,连接请求只会到达具有外部 IP 的路由器,但它不知道将请求转发到网络上的哪台计算机。
您将需要访问路由器的固件并设置端口转发,以便路由器知道将特定端口上的请求转发到网络上的特定计算机。有关如何设置端口转发的更多详细信息,请访问 http://portforward.com/。
编辑 2:
防火墙
当使用 PC 设置与计算机通信的内容时,您可能会因为无法连接而感到沮丧。阻止您的可能是您的防火墙。默认情况下,您 PC 上的大多数端口都被 Windows 防火墙完全阻止。对于您想要进行通信的每个端口,您需要进入防火墙并创建一条打开该端口的规则。转到这里 http://www.top-windows-tutorials.com/windows -7-firewall.html 有关如何使用 Windows 防火墙的视频。我没有看,但这是谷歌搜索中首先出现的内容。
不要简单地禁用防火墙。尽管这是打开所有端口的简单快捷的解决方案,但您却容易受到攻击。如果可以的话,病毒喜欢在您的计算机中安装自己,并在开放端口上侦听来自其心爱的创建者的连接,以便他能够访问您的电脑。只打开您需要的端口。
UDP 与 TCP
打开和转发端口时,您可能会注意到它要求 UDP(用户数据报协议)或 TCP(传输控制协议)。它们代表的内容可能没有意义,但您需要知道的是:UDP 用于单数据包传输,这意味着 PC 发送的两个数据包可能彼此相关,也可能不相关。这些类型的数据包通常用于本地网络上的广播。我会使用的一个例子是局域网游戏。当您在 LAN 上托管游戏时,其他计算机/设备可以看到游戏的名称并加入。这是因为托管游戏的计算机正在整个 LAN 上传输 UDP 广播,以便任何设备都可以看到游戏。这些 UDP 传输通常包含游戏名称以及连接游戏所需的连接信息。
TCP 用于连续数据包传输。 TCP 需要一个已建立的连接,在此连接上传输的任何数据包始终与该连接/请求相关。继续我上一段的示例,一旦您在 LAN 游戏上单击“连接”,您的计算机就会与主机建立 TCP 连接,并在游戏期间使用该连接。 TCP 是最常用的连接类型,您的聊天程序可能会通过 TCP 进行通信,尤其是当您通过网络连接时,因为 UDP 广播在互联网上毫无用处。 UDP 仅在 LAN 上真正有用。
您应该仅安全转发和解锁 TCP 端口,但有时当我不确定时,我只是为了安全而同时执行 UDP 和 TCP。事实上,许多路由器和防火墙有 3 个选项:TCP、UDP 或两者,这使您不必为两种类型的端口创建两个规则。
如有疑问,请打开/转发两者。
PART 1
Okay, let's pretend you have a router in your house and that you have several computers in your house all connected to the internet through your router.
In order for the router to know where traffic goes on your network, it assigns unique IP Addresses to all computers on your home network (Usually beginning with 192.168.x.x). These IP addresses are local ip addresses, meaning only your router and computers/devices connected to it in your house knows about them. If you open a command prompt and do command IPConfig you will see the IP address that your router has assigned your computer.
So what is the IP address that WhatIsMyIP.com showing you? In much the same way that your router assigns addresses to all the computers on your network, your internet service provider hands out unique IP addresses to all of their customers. Now, because you have a router, the only thing the ISP can see on your network is that router and your ISP assigns an IP address to it. This is why routers are also called hardware firewalls, because people on the other side of it, can't tell how many computers or devices are connected to it.
What this means is, when you are visiting websites on the internet, the only IP address they see is your routers external IP address (the one assigned by your ISP). So no matter which computer in your house you use, the website wouldn't know the difference because all it can see is your router's IP address. Go ahead and try it; go to www.WhatIsMyIP.com on several different computers in your house. You will see that they all show the same IP address. However, if you did IPConfig in your command prompt on each computer, that shows you the local address your router assigned and it would be different on every computer in your home.
So, now that you understand the difference between local and external IP addresses, how would you retrieve your external IP address in VB or C# .net code that is running on your PC? Well the only IP address your computer is actually aware of is that local IP that we talked about. The only way you can see your external IP address is to go to a website that tells you what address the request came from (which would be your router's IP address).
What you would need to do is write up some code in your VB.net program that would navigate out to WhatIsMyIP.com (or some other website that can give you your IP address) and tell the code to grab it. I have written a web service located at http://www.u413.com/test/terminal/myip that returns only your IP address as the entire HTTP response. Find something similar though for your application because this little sample will not stay there forever; I only put it up there as a temporary example on a domain I already own.
Visit http://www.vbdotnetheaven.com/UploadFile/kbawala/WebRequestClass04182005054320AM/WebRequestClass.aspx to see how to make web requests from code running on your computer.
NOTE: You may not be aware of what DNS is either if you are unaware of how IP addresses work. Everything on the net has an IP address, including the servers that serve up website pages. But what a pain that would be, trying to remember up to 12 digit IP addresses for all your favorite websites. That is what DNS servers were invented for. DNS servers take a domain name (e.g. www.facebook.com) and translates it into the correct IP address. That way all you need to remember is facbook.com instead of 69.63.181.12 (this is facebook's IP address. Go ahead, try it! Put that IP in your browser's address bar and you will see facebook.), domain names are much easier to remember!
If you want to see the IP address associated with a website, open up a command prompt. Once the prompt is open type PING [websitedomain] (e.g. PING Facebook.com) and your computer will send 4 test requests to the address which is displayed for you.
PART 2
Let's pretend your IP address is like the address of an apartment buliding. The pizza delivery boy needs to know the address to the apartment building in order to deliver your pizza. But what is he going to do when he gets there? There are hundreds of doors/apartments to choose from. He needs to know the apartment number (port number on your computer).
Your computer has thousands of ports, and programs can listen on any one of them for requests from the outside world. When you go to a website almost all websites are served on port 80. Port 80 is the default port for web pages. When you go to facebook.com you are actually going to facebook.com:80, you just don't see the :80 because it is implied since it is the default. If I put up a web server, I could decide any port to serve websites on. If I served web pages on a different port than port 80, then you would have to include it in your URL. http://www.SomeDudesCustomWebServer.com:1337.
Outgoing requests use a port too, but that one is usually unimportant and your computer just picks one that is available. So when you go to Facebook.com, the facebook web servers are all serving up pages over port 80, but the port your computer opened up to send the request does not have to be port 80 because it picks an available port and then sends the port with the request. Then when facebook sends its response, it sends the reply back to the ip address and port that made the request.
Outgoing ports are only used for the duration of the request. Ports that must listen for connections must stay the same otherwise the computers making requests would have no idea what port to send the request to.
Easy huh!
Hope that helps you understand a bit better.
EDIT:
Port Forwarding
Okay, in light of the chat application you want to use/create, if you want it to communicate over the net you'll have to learn about port forwarding. Basically, because all you could see of your friend's network would be his external ip address, you will have to use that address to connect to his chat server (or vice versa if he is connecting to your chat server then it will be your external IP). Because of this, the connection request would only get as far as the router that has the external IP, but it would not know what computer on the network to forward the request to.
You will need to access your router's firmware and set up port forwarding so that the router knows to forward requests on a specific port, to a specific computer on the network. Visit http://portforward.com/ for more detail on how to setup port forwarding.
EDIT 2:
Firewall
When setting up stuff to communicate with your computer using your PC, you may start getting frustrated that it just won't connect. What is likely stopping you is your firewall. By default, most ports on your PC are completely blocked by the windows firewall. For each port that you want to communicate on you will want to go into the firewall and create a rule that will open up the port. Go here http://www.top-windows-tutorials.com/windows-7-firewall.html for a video on how to use the windows firewall. I did not watch it, but it is what came up first on a google search.
Do not simply disable the firewall. Even though this is an easy and quick solution to open up all your ports, you are leaving yourself open to attack. Viruses love to set themselves up in your computer if they can and listen on an open port for a connection from their beloved creator so he can obtain access to your PC. Only open the ports you need.
UDP vs TCP
When opening and forwarding ports you may notice that it asks for UDP (User Datagram Protocol) or TCP (Transmission Control Protocol). What they stand for may not make sense but all you need to know is this: UDP is for single packet transmissions which means that two packets sent by a pc may or may not be related to each other. These types of data packets are usually used for broadcasts on a local network. An example I would use is LAN games. When you host a game on a LAN the other computers/devices can see the name of the game and join it. That is because the computer hosting the game is transmitting a UDP broadcast across the entire LAN so that any devices can see the game. Those UDP transmissions usually contain the name of the game and the connection info required to connect to the game.
TCP is for continuous packet transmission. TCP requires an established connection, any packets transmitted on this connection are always related to that one connection/request. To continue my example from the last paragraph, once you click connect on the LAN game, your computer then establishes a TCP connection with the host and uses that connection for the duration of the game or games. TCP is the most commonly used connection type and your chat program would likely communicate over TCP, especially if you are connecting across the net because UDP broadcasts are useless across the internet. UDP is only really useful on a LAN.
You should be safe forwarding and unblocking only the TCP ports, but sometimes when I'm unsure I just do both UDP and TCP just to be safe. In fact, many routers and firewalls have 3 options: TCP, UDP, or Both which saves you from having to create two rules for both types of the port.
When in doubt, open/forward both.
What's my ip 提供您的 IP v4 公共地址。
检索它非常容易,本主题解释如何继续:如何获取运行我的 C# 应用程序的服务器的 IP 地址?
代码只有几行长,因此语言(本例中为 c#)并不重要。
What's my ip provides your IP v4 public address.
It's really easy to retrieve it, this topic explain how to proceed : How to get the IP address of the server on which my C# application is running on?
The code is only a few lines long, so the language (c# in this example) does'nt matter.
他们提供您面向外部互联网的 IP。
该 IP 将取决于您连接互联网的方式。如果您直接从计算机连接到 ISP,中间没有任何类型的路由器或防火墙,则它可能与您的内部 IP 相同,但在大多数情况下情况并非如此。
如果您在家并且通过某种路由器进行连接,那么您也许可以查询 IP,但没有标准的方法可以做到这一点。
没有标准方法可以从客户端本身获取外部 IP。如果您可以访问互联网上的服务器,您可以在其中部署一些代码,您可以从客户端 PC 连接到该服务器,并询问它您从哪个 IP 进行连接。
IP 端口号
They provide your external internet facing IP.
This IP will depend on how you connect to the internet. If you connect straight from your computer to your ISP without any kind of router or firewall in between, it might be the same as your internal IP, but in most circumstances this will not be the case.
If you're at home and you've connected via a router of some kind, then you might be able to query it for the IP, but there is no standard way of doing this.
There is no standard way of getting hold of your external IP from the client it self. If you've got access to a server on the internet where you could deploy some code you could connect to that server from your client PC and ask it what IP you're connecting from.
IP Port Numbers