I have a remote desktop project, need to connect using DNS server
I have an error saying that the format of ip and invalid
private static void ConnectToServer()
{
int attempts = 0;
while (!clientSocket.Connected)
{
try
{
attempts++;
Console.WriteLine("Connection attempt " + attempts);
// clientSocket.Connect(IPAddress.Parse(Dns.GetHostAddresses("kamikazehc.ddns.net")), port);
clientSocket.Connect(IPAddress.Parse("kamikazehc.ddns.net"), port);
Thread.Sleep(100);
}
catch (SocketException)
{
Console.Clear();
}
}
Console.Clear();
Console.WriteLine("Connected");
}
Edit:
This code stays in the external user, my computer works as administrator.
When the user runs the console he should connect with kamikazehc.ddns.net, (my computer)
Kamikazehc.ddns.net resolves on 189.6.26.203.
I can only connect locally Using:
clientSocket.Connect(IPAddress.Parse("192.168.1.102"), port);
When the user connects to my computer it should appear in this list:
private static Socket clientSocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private const int port = 100; // same as server port
You should be using Dns.GetHostEntry to get resolve the domain name to IP address.
IPAddress.Parse only converts an IP address string to an IPAddress instance.
try
Note: Here the first IP Address returned in host entry is used to connect.
Edit:
From the comments: Your problem is because you don't clearly understand the way TCP IP network works. you need to resolve the name to your local IP. normally Public IPs won't work inside the local network. You can do the following.
Add a host entry in system32/etc/drivers/hosts file
Better is if you can modify your DNS server host entries to do a different resolve for internal network. But i don't recommend it to you at your level now. If you have an administrator for the network you can ask him/her to do it for you. then you don't need the host entry.
If you are unable to connect with the above code from external network, then you need to check the firewall settings.