Hi, guys. I'm making a game for a capstone project. It's a board game, that requires two human players to play over LAN. Since I am the only person on this project, I decided not to include singleplayer vs. AI (I might not finish it in time).

So, I researched up a way to have two players connect without relying on a database (I once considered using SQL Server, which would be a huge pain for the gamers), and found out about TCP / IP stuff that C# is capable of.

On the list of things I have yet to understand and do for myself, this is close to the top.

The very first thing I did was to have the host display it's IP Address, ala Diablo II, where the client must type in the host's IP address and press connect. I used the following code to retrieve the IP Address:

string myHost = System.Net.Dns.GetHostName();
            string myIP = null;

            for (int i = 0; i <= System.Net.Dns.GetHostEntry(myHost).AddressList.Length - 1; i++)
                if (System.Net.Dns.GetHostEntry(myHost).AddressList[i].IsIPv6LinkLocal == false)
                    myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[i].ToString();
            txtIP.Text = myIP;

Which then returns a jumble of letters, numbers and in some cases, even signs. A far cry from the simple 192.168.XXX.XX I was expecting. I've researched in this, and know that it has something to do with the IPv4 or IPv6.

I've also seen somewhere that having a router might screw things up and IPv4 and IPv6 is OS dependent or something, which is horrible, because I'm using Windows 7, my test platform is Windows Vista, and my target client is Windows XP

Is there some way to retrieve that IP address?

In addition to the above, how does a client connect to the host using the host's IP Address?

Edited by zack_falcon: n/a

7 Years
Discussion Span
Last Post by zack_falcon

firstly, its a great foul to have such differences in OSs.. at the very least, your test server has to simulate your production environment.

Re client server question, you need to use sockets:

on server, open a socket (using System.Net.Sockets;), and begin listening.
on client, open socket and connect to server

search for sockets in c#, im sure a ton of examples will appear.

Re your IP question, try another method (although i have tried yours on ipv4 and it worked just fine)

IPHostEntry IPHost = Dns.Resolve(Dns.GetHostName());
IPAddress[] addr = IPHost.AddressList;


Sorry for the late reply, my PC went kaboom, and I had to get it fixed.

Yes, actually, now that you mention it, the code works fine on Windows XP, and returns the IP address I expect. My only concern is that when my software is used on something else, the client will have to type that loooooong IPv6 that Windows 7 gives. That pretty much also means a Windows 7 to XP might be difficult.

But if I may, I would like to ask if there is perhaps a better way to connect the PCs to each other. I've tried my hand at retrieving the Computer Name, could it be used instead? Any thoughts?


if its within the same domain, you can use the computer name.. same code i posted earlier.

But if you;re app has to work outside your network via the internet, then an IP or a domain name (not host) is needed

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.