1

I'm creating a chat program. I guess i'm a bit confused as to how MSN and other chats work? Currently, i'm putting in the localhost ip address and just connecting to myself. I'm able to get my client and server programs communicating back and forth. My question is, is there a way where i can NOT ask the user for an IP address? I also manually enter in the port number of which i want both programs to connect to. Is there a way to avoid this too?

I would much rather ask the user for just a User name and password!

After a bit of research i came to know about the following:

IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

However, the line of code above returns all the machine's IP addresses. How would i know which one to use?

I'm not that knowledgeable about IP addresses, so any background information and help would be awesome.

BigBug
  • 6,202
  • 23
  • 87
  • 138

3 Answers3

2

Chat programs use servers to find clients. When you login - you connect to this server and pass your IP address. At the same time you get addresses of other clients.

This is 2 mile view but I guess this is what you needed to know

You can use DNS or Host Names in your application.

To get your PC's DNS name:

System.Net.Dns.GetHostName()

To get your PC's IP address use something like this:

var ips = System.Net.Dns.GetHostEntry(hostName);
                for (var i = 0; i <= ips.AddressList.Length - 1; i++)
                {
                    if (ips.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                        return ips.AddressList[i].ToString();
                }
katit
  • 17,375
  • 35
  • 128
  • 256
  • 1
    Thanks for the response. Okay, but how would i get the client to know of that machine's specific IP address to pass over to the server? How would i get this value? – BigBug Dec 03 '11 at 05:35
  • Point taken - I just answered what he asked. My code works for internal networks but no good for internet. – katit Dec 03 '11 at 05:50
  • Thanks :)... okay, i understand. It's a good starting point though. – BigBug Dec 03 '11 at 06:14
0

You seem a bit confused, maybe this will clear it up for you. The server is the only one who's IP needs to somehow be known (usually determined via a dns name, like chat.mydomain.com or something). Once the client has that IP address, the client will connect to the server. THE CLIENT DOESN'T NEED TO KNOW ITS OWN IP in order to connect. This is determined automatically and is beyond your control. Once the client has successfully connected, then and only then can you start sending things like a username and password.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
0

Currently, i'm putting in the localhost ip address and just connecting to myself. I'm able to get my client and server programs communicating back and forth. My question is, is there a way where i can NOT ask the user for an IP address?

Chat software works by running all traffic through a central KNOWN server. So, you want to put up a chat program on a website the user then eithe connects to the server he enters (IRC) or a hardcoded server (MSN). In the first part, no, the user has to decide where to connect, in the second YOU hardcode the value, so asking how to do that makes you look not too smart - this is basic programming.

You do NOT find out your IP address because this is a ridiculous approach - your IP is not even in most cases reachable. You use a way where the CLIENT connects to the server ALWAYS and the server sends the data to the client using this connection - because most people are behind NAT devices so their computer is not easily reachable from the internet.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
TomTom
  • 61,059
  • 10
  • 88
  • 148