Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi !
I'm developing a project in which i connect two computers with a tcplistener class and a tcpclient class, but the tcpclient requires an ip , since it changes a lot , i would like to access the computer with the tcpserver class using the name of the computer or any other method, is this possible? I want to connect them without any wire and it is not necessary to use those classes, i just point the way I'm doing it.
any recommendation is accepted =)
Thanks
Posted
Updated 30-May-11 21:14pm
v2

You can also construct TpcClient by DNS name of the remote computer running TcpListener. See the constructor System.Net.Sockets.TcpClient(string hostname, int port). A computer can have more then one IP, and only one of them is associated with a given DNS entry, so this is the only reliable association.

—SA
 
Share this answer
 
v2
Comments
OriginalGriff 31-May-11 2:43am    
Univote countered!
Sergey Alexandrovich Kryukov 31-May-11 2:43am    
Thank you, Griff.
--SA
BobJanova 31-May-11 5:36am    
True, and, on a Windows network, DNS resolution includes the computer name as seen in net view.
Sergey Alexandrovich Kryukov 31-May-11 18:36pm    
Agree. Thank you, Bob.
--SA
Albert Holguin 13-Jul-11 13:55pm    
my 5
If you know the PC name, then:
using System.Net;
...
      string strHostName = "MYPCNAME";
      IPHostEntry ipEntry = Dns.GetHostByName (strHostName);
      IPAddress [] addr = ipEntry.AddressList;
      IPAddress ip = addr[0];
      Console.WriteLine("{0}", ip);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-May-11 2:29am    
This is correct, but redundant, because TcpClient can also accept Dns name instead of IP. Please see my answer.
--SA
OriginalGriff 31-May-11 2:43am    
Doh! It's early, and the cat kept us awake most of the night - I'm not fully awake yet...
Sergey Alexandrovich Kryukov 31-May-11 18:36pm    
Understood. Regards to your cat.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900