How do applications connect to servers without the need for port migration?

I just created a simple chat client, and it only works when all clients / server are sent to the same port.

How to make my application (it is in C # and uses .net btw sockets) to work without the need for port forwarding for clients (I don’t care if the port needs to be redirected).

he uses udp, by the way.

+4
source share
4 answers

You need a server somewhere in the middle, to which all clients connect. You cannot have 1 client behind the nat box to connect to another client behind the nat field. They both need to connect to the server and open this connection. Client A then sends a message to the server, which forwards the message to client B.

+1
source

I believe that you incorrectly named your question. You are talking about connecting a server to a client, aren't you?

If you work directly with sockets, the short answer is you cannot. The long answer is that the client must register with the server so that the client port is open so that the server can reach it.

Instead of writing it yourself, consider a library that focuses on it, such as SignalR .

In addition, UDP is a terrible choice for a chat client. There are many jokes about UDP packets, but trust me, you won’t get them all.

+2
source

If there is NAT and / or a firewall between the two endpoints, this hardware decides whether the two endpoints can communicate, not your program.

However, NAT and firewall rules often allow port 80 and other ports <1024 inbound. Often any outbound port can be reached. You can use this to minimize the likelihood that the network topology will block the connection. In fact, if you look at the "Advanced / Connection" tab of Skype, you will see that there is a flag indicating whether Skype can use ports 80 and 443 for incoming connections (this parameter sometimes interferes with the web server on the developer's machine ..).

+1
source

If you want to communicate through a NAT router, you can configure port migration using UPnP from your application. So, for example, torrent programs can communicate without having to configure port transfer.

In .net, you can use the NATUPnP 1.0 Type Library (NATUPNP.DLL) COM component that is included with Windows (starting with Windows, XP).

Add link to the lump

adding com component

Get a list of all existing port mappings

NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass(); NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection; 

Iterate through all mappings

 foreach(NATUPNPLib.IStaticPortMapping portMapping in mappings) { // do something with the port mapping, such as displaying it in a listbox } 

Add new port mapping

 // Here an example of opening up TCP Port 80 to forward to a specific Computer on the Private Network mappings.Add(80, "TCP", 80, "192.168.1.100", true, "Local Web Server"); // Here an example of forwarding the UDP traffic of Internet Port 80 to Port 8080 on a Computer on the Private Network mappings.Add(80, "UDP", 8080, "192.168.1.100", true, "Local Web Server"); 

Remove Port Mapping

 // Remove TCP forwarding for Port 80 mappings.Remove(80, "TCP"); // Remove UDP forwarding for Port 8080 mappings.Remove(8080, "UDP"); 

Source: http: //pietschsoft.com / ...

0
source

Source: https://habr.com/ru/post/1436243/


All Articles