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)
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
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.:
But where did 25.240.1.0 come from? Try the following:
127001, then switch to Hex0001F01900 01 F0 1919 F0 01 0025 240 1 025.240.1.0Why 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.Reversing the steps above, the correct value for loopback in the
IPAddress( Int64 )constructor would be&H0100007F(hex) or16777343(decimal).The
IPAddress( Byte[4] )constructor takes the byte array in network order, so that would beNew Byte() { 127, 0, 0, 1 }