Click here to Skip to main content
11,920,156 members (57,806 online)
Rate this:
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 =)
Posted 30-May-11 20:40pm
Edited 30-May-11 22:14pm
Rate this: bad
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.

OriginalGriff 31-May-11 2:43am
Univote countered!
SAKryukov 31-May-11 2:43am
Thank you, Griff.
BobJanova 31-May-11 5:36am
True, and, on a Windows network, DNS resolution includes the computer name as seen in net view.
SAKryukov 31-May-11 18:36pm
Agree. Thank you, Bob.
Albert Holguin 13-Jul-11 13:55pm
my 5
SAKryukov 13-Jul-11 14:40pm
Thank you, Albert.
Rate this: bad
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);
SAKryukov 31-May-11 2:29am
This is correct, but redundant, because TcpClient can also accept Dns name instead of IP. Please see my answer.
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...
SAKryukov 31-May-11 18:36pm
Understood. Regards to your cat.

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

  Print Answers RSS
0 OriginalGriff 455
1 phil.o 170
2 CPallini 150
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,153
3 George Jonsson 2,810
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web04 | 2.8.151120.1 | Last Updated 31 May 2011
Copyright © CodeProject, 1999-2015
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