K
K
kos_dev2017-03-03 12:21:50
C++ / C#
kos_dev, 2017-03-03 12:21:50

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

5 answer(s)
A
Artem, 2017-03-03
@devspec

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.

K
Konstantin Malyarov, 2017-03-03
@Konstantin18ko

Forward ports. Forward port, if the machine is behind a router, then NAT must also be forwarded.

J
John_Nash, 2017-03-03
@John_Nash

string hostName = Dns.GetHostName(); // Имя машины на которой запущен сервер.
            IPHostEntry ipEntry = Dns.GetHostByName(hostName);  // IP адреса доступные на данной машине.
            IPAddress[] ipAdresses = ipEntry.AddressList; // Преобразую в список всех адресов, у меня доступен 1.
            IPAddress serverIP = ipAdresses[0]; // Получаю данный IP адресс
                    // Данный IP заносится в БД, а клиент получает его при запуске приложения и пытается к нему подключиться.

get an address like "::1" - analogous to 127.0.0.1 for ipv6
No wonder clients can't reach the server

R
Rou1997, 2017-03-03
@Rou1997

if the client is launched by someone else, from a different IP, then he cannot connect to my server

Is this other connected to the same router as your computer?
If not, then this is already a global network, here, first of all, the server needs a static and most importantly "white" IP, either get it from the provider for an additional fee or buy a VPS, and in this case the IP is needed not 192.168.xx, but "global", with renting a VPS, you will have it because access via protocols like SSH works on the same principle and is impossible without it, you can programmatically find it out only by making a request through the global network to a service like 2ip.ru look for such services, in extreme cases, write your own script in PHP in a couple of lines, uploaded to some hosting, and make a request there throughSystem.Net.WebClient

L
LiptonOlolo, 2017-03-14
@LiptonOlolo

IPAddress.Any

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question