Socket programming tip: Localhost != local ip address


I’ve been writing some socket-based code for an upcoming column, and it worked really
nice when I used localhost for the client and server on another machine, but broke
when I tried to use the machine name.

Just something else I didn’t know about networks. localhost is not the same thing
as the local ip address, but is rather a totally separate address.

If you want to create a TcpListener, you should use IPAddress.Any as your parameter
for the IP address. That will do what you want.

Comments (5)

  1. Jeff Julian says:

    Do you mean ‘localhost’ != ‘127.0.0.1’ || ‘localhost’ != ’10.x.x.102′? Sockets are tons of fun. If you want to check out a IM client/server I built in C#, you can find it on GDN at http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=c733fdfa-5db2-4eb4-9545-4100ed31b61c. Please blog when your article is finished.

  2. Eric says:

    I meant localhost != whatever real IP I have.

  3. What .NET method will give me the list of IP addresses configured for the local machine?

    mwherman@parallelspace.net.nospam

  4. Mary Jeykle says:

    I’m kinda dion some socket programming project… I’ve observed that localhost != to any real IP Address except 127.0.0.1 –> this is the default IP Address for localhost. Also, localhost == hostname.

  5. Mary Jeykle says:

    I’m kinda doin some socket programming project… I’ve observed that localhost != to any real IP Address except 127.0.0.1 –> this is the default IP Address for localhost. Also, localhost == hostname.

Skip to main content