System.Net.IPAddress返回奇怪的地址 [英] System.Net.IPAddress returning weird addresses
问题描述
我正在编写一个(相当简单的:)网络应用程序,并使用localhost:27488
(127.0.0.1:27488
)对其进行测试.
我正在使用System.Net.Sockets.TcpClient
进行连接,需要一个System.Net.IPAddress
来指定主机...唯一的是,我不知道如何使用正确的IP地址初始化该类.我查看了MSDN文档,并说该地址需要Byte(4)
或Int64
(long
).
当我像这样初始化IPAddress
时,探查是:
I am writing a (rather simple :) networking application, and am testing it using localhost:27488
(127.0.0.1:27488
).
I am using a System.Net.Sockets.TcpClient
for the connection, which takes a System.Net.IPAddress
to specify the host... the only thing is, I can't figure out how to initialize the class with the right IP address. I went over the MSDN docs and it says it takes either a Byte(4)
or an Int64
(long
) for the address.
The probelm is, when I initialize the IPAddress
like this:
Dim ipAddr As New System.Net.IPAddress(127001)
它将地址返回为25.240.1.0
.根据我对文档的了解,127001
应该返回127.0.0.1
...也许我在那儿错过了什么? http://msdn.microsoft.com/en-us/library/13180abx.aspx
it returns the address as 25.240.1.0
. From what I understand from the docs, 127001
should return 127.0.0.1
... Maybe I missed something there? http://msdn.microsoft.com/en-us/library/13180abx.aspx
推荐答案
简短的答案:请改用TcpClient.Connect(String,Int);它接受IPv4/IPv6地址或主机名/别名,因此您不仅限于IP连接.例如:
Short answer: use TcpClient.Connect( String, Int ) instead; it accepts an IPv4/IPv6 address or hostname/alias, so you are not limited to connecting by IP. e.g.:
Dim client As TcpClient
client.Connect( "localhost", 27488 )
client.Connect( "127.0.0.1", 27488 )
但是25.240.1.0是从哪里来的呢?请尝试以下操作:
But where did 25.240.1.0 come from? Try the following:
- 打开Calc,切换到Programmer视图,然后选择Dec
- 键入
127001
,然后切换到十六进制 - 写出结果,在左边加零以填充到4个字节/32位:
0001F019
- 将该数字分成单个字节:
00 01 F0 19
- 颠倒字节顺序:
19 F0 01 00
- 将每个字节转换回十进制:
25 240 1 0
- 带点:
25.240.1.0
- Open Calc, switch to Programmer view, select Dec
- Type in
127001
, then switch to Hex - Write out the result, adding zeroes on the left to pad to 4 bytes/32 bits:
0001F019
- Separate that number into individual bytes:
00 01 F0 19
- Reverse the byte order:
19 F0 01 00
- Convert each byte back to decimal:
25 240 1 0
- With dots:
25.240.1.0
为什么要反转字节?您的处理器架构为低端字节序;数字以最低有效字节在前的形式表示在内存中. IPv4地址已标准化为big-endian格式(最高有效字节在前;也称为网络顺序). IPAddress( Int64 )
构造函数正在反转字节,以从LE转换为BE.
Why reverse the bytes? Your processor architecture is little-endian; numbers are represented in memory with the least significant byte first. IPv4 addresses are standardized to big-endian format (most significant byte first; a.k.a. network order). The IPAddress( Int64 )
constructor is reversing the bytes to convert from LE to BE.
与上述步骤相反,在IPAddress( Int64 )
构造函数中,回送的正确值应为&H0100007F
(十六进制)或16777343
(十进制).
Reversing the steps above, the correct value for loopback in the IPAddress( Int64 )
constructor would be &H0100007F
(hex) or 16777343
(decimal).
IPAddress( Byte[4] )
构造函数按网络顺序获取字节数组,因此将为New Byte() { 127, 0, 0, 1 }
The IPAddress( Byte[4] )
constructor takes the byte array in network order, so that would be New Byte() { 127, 0, 0, 1 }
这篇关于System.Net.IPAddress返回奇怪的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!