Reputation: 1253
I just created a simple chat client and it only works when all clients / server are portforwarded on the same port.
How do i make my application (its in c# and uses .net sockets btw) work without the need of port forwarding for clients (i dont care if server needs to port forward).
it uses udp by the way.
Upvotes: 5
Views: 6112
Reputation: 12766
If you want to communicate through a NAT router, you can setup your portforwarding using UPnP from within your application. This is how for example torrent programs can communicate without having you to setup portforwarding.
In .net you can use the NATUPnP 1.0 Type Library (NATUPNP.DLL) COM Component, which is part of Windows (since Windows XP).
Add a reference to the 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 a new Port Mapping
// Here's 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's 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/...
Upvotes: 0
Reputation: 3049
You need a server somewhere in the middle that all the clients connect to. You cannot have 1 client behind a nat box connect to another client behind a nat box. They both need to connect to a server and keep that connection open. Client A then sends a message to the server which forwards the message onto client B.
Upvotes: 0
Reputation: 241525
I believe you titled your question wrong. You are talking about the server connecting to the client, right?
If you are working directly with sockets, the short answer is - you can't. The long answer is that the client has to register with server in such a way that the client port is held open so the server can reach it.
Rather than writing this yourself, consider a library that is focused on this, such as SignalR.
Besides - UDP is a horrible choice for a chat client anyway. There are plenty of jokes about UDP packets, but trust me - you won't get them all.
Upvotes: 5
Reputation: 150108
If there is NAT and/or a firewall between the two endpoints, that hardware decides if the two endpoints can communicate, not your program.
However, NAT and firewall rules frequently allow Port 80 and other ports < 1024 inbound. Often, any outbound port can be reached. You can leverage this to minimize the likelihood that the network topology will block communication. In fact, if you look at the Advanced / Connection tab of Skype, you can see that there is a checkbox indicating whether Skype can use ports 80 and 443 for incoming connections (this setting sometimes interferes with a web server on a developer machine...).
Upvotes: 2