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?

Dani AI

Generated

Short summary for and follow-ups to : the long addresses you saw on Windows 7 are IPv6 addresses (modern OSes assign both IPv4 and IPv6). For a simple LAN game it's usually easiest to pick an IPv4 address and a fixed port, or use simple UDP broadcast discovery so the client doesn't have to type anything.

Practical IPv4 detection (avoid listing IPv6 link-local/temporary addresses):

using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;

string GetLocalIPv4()
{
    foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (ni.OperationalStatus != OperationalStatus.Up) continue;
        if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;
        foreach (var ua in ni.GetIPProperties().UnicastAddresses)
            if (ua.Address.AddressFamily == AddressFamily.InterNetwork)
                return ua.Address.ToString();
    }
    return null;
}

This reliably returns an IPv4 on the active LAN interface.

Simple connection pattern (server accepts, client connects):

// server
var listener = new TcpListener(IPAddress.Any, port);
listener.Start();
var tcpClient = listener.AcceptTcpClient();

// client
var client = new TcpClient();
client.Connect(serverIp, port);

Auto-discovery (optional): have the host listen on a small UDP discovery port and reply to a broadcast. Client sends a short "DISCOVER" packet to the LAN broadcast address and the host replies with its IP and game port. This avoids manual typing.

Troubleshooting checklist: pick a consistent port (>1024) and open it in Windows Firewall; test on the same network first; computer names only work if name resolution (NetBIOS/mDNS/DNS) is available on that LAN; across the Internet you’ll need port forwarding, a relay/matchmaking server, or NAT traversal (STUN/TURN). As said, sockets are the right tool — combine the IPv4 filter above with a small UDP discovery step for best UX.

Recommended Answers

All 4 Replies

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

Thanks. Will do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.