Answer the question
In order to leave comments, you need to log in
How to make a server that can be run on any machine?
Hello, I'm trying to write a server-client in C #, using sockets and the Tcp protocol. On the local machine, everything starts correctly, if the client is running on the same machine as the server, then they interact perfectly, if the client is launched by someone else, from a different IP, then it cannot connect to my server. Actually the question is this: how to get the IP address of the Internet connection through which the server works. Below I will post the code that I am trying to get the IP address of the current server.
If there are resources with examples, then throw off as an answer, it will be even better. Thank you all in advance.
Moment with ipconfig:
All adapters except wireless LAN write that the transmission medium is unavailable
string hostName = Dns.GetHostName(); // Имя машины на которой запущен сервер.
IPHostEntry ipEntry = Dns.GetHostByName(hostName); // IP адреса доступные на данной машине.
IPAddress[] ipAdresses = ipEntry.AddressList; // Преобразую в список всех адресов, у меня доступен 1.
IPAddress serverIP = ipAdresses[0]; // Получаю данный IP адресс
// Данный IP заносится в БД, а клиент получает его при запуске приложения и пытается к нему подключиться.
Answer the question
In order to leave comments, you need to log in
There may be several options here.
If the client is on the other side of the router, you need to forward the port from the router to your computer.
If the client is on the same local network, add the required port to your firewall exceptions.
There is also a good chance that you do not have Visual Studio running with administrator rights (even if you are a local admin, Visual Studio still starts with the minimum necessary rights by default).
And finally, you can use the following command to open the port to the outside for the "Everyone" user:
Here port 8765 - you need to change it to your own.
Forward ports. Forward port, if the machine is behind a router, then NAT must also be forwarded.
string hostName = Dns.GetHostName(); // Имя машины на которой запущен сервер.
IPHostEntry ipEntry = Dns.GetHostByName(hostName); // IP адреса доступные на данной машине.
IPAddress[] ipAdresses = ipEntry.AddressList; // Преобразую в список всех адресов, у меня доступен 1.
IPAddress serverIP = ipAdresses[0]; // Получаю данный IP адресс
// Данный IP заносится в БД, а клиент получает его при запуске приложения и пытается к нему подключиться.
if the client is launched by someone else, from a different IP, then he cannot connect to my server
System.Net.WebClient
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question