Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Networking TCP/IP
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 30-May-11 20:40pm
Edited 30-May-11 22:14pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
OriginalGriff at 31-May-11 2:43am
   
Univote countered!
SAKryukov at 31-May-11 2:43am
   
Thank you, Griff.
--SA
BobJanova at 31-May-11 5:36am
   
True, and, on a Windows network, DNS resolution includes the computer name as seen in net view.
SAKryukov at 31-May-11 18:36pm
   
Agree. Thank you, Bob.
--SA
Albert Holguin at 13-Jul-11 13:55pm
   
my 5
SAKryukov at 13-Jul-11 14:40pm
   
Thank you, Albert.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
  Permalink  
Comments
SAKryukov at 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 at 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...
SAKryukov at 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)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Richard MacCutchan 145
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 31 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100