I've built a server-application for a game that I want general people to use. Now, since I've worked with client/server solutions before, I know how tedious it is to host connections on some computers.
So, I heard about these rumors that I would like to get confirmed.
Using UDP for "hosting" a connection is good, because it is rarely blocked by router-firewalls compared to TCP.
Using UPNP for communicating with a router is good, because it allows you to add port forwarding for the game, making your server reachable no matter if you're using TCP or UDP.
I don't care about the software-firewall people may use. What I care about is the router firewall functionality.
Any help would be appreciated.
Thanks!
For your two rumors,
I don't know, I have not worked with firewalls enough, but I would not call that one true. Most routers block everything (TCP and UDP) unless you explicitly ask it to forward it for inbound connections via hand setup or UPNP. If you don't tell the router what computer wants info coming in to port 12345 how will it know what to do when a user sends a unsolicited packet to it (the definition of being a server).
Yes, learning how to do UPNP will make it a lot easier for your users to host games, however you should still provide instructions for people who do not have UPNP turned on or their router does not support it.
The other thing you may want to look in to is learning how to add your program to the windows firewall client allowed list as that is what most of your users will have. See this SO question for more details.
Related
I want to create a simple messaging app that uses tcp protocol to communicate, including with multiple people. Messages get sent to the server, which distributes them to all other clients. At the moment, I have it fully functioning and it works perfectly - on a local computer and a local network, using the ipv4 address.
After an extensive Google search, I discovered that to communicate from a different network I need to port-forward my server. However, how do I make my server able to communicate with clients without all the clients port-forwarding? As far as I'm aware, not everybody's device is port-forwarded.
So, how do I implement this? Is it possible with just C#? Or did I misunderstand something, and port-forwarding isn't really required?
Thanks for all the help.
Why don't you use SignalR. I think it will be best for your problem.
I wrote messenger in c# with sockets, but i have little problem with ports. To clear comunication I have to open port on router which i use in my messenger. How to resolve this problem? Is method to automatic open default closed ports?
Thanks.
There are a couple things you can do.
The first is to change the programming of your application so that it uses the regular http port (80) for communication. This would allow your app to make outbound calls pretty much anywhere.
Alternatively you could use a high port number from 49152 through 65535. ( See Assigning TCP/IP Ports for In-House Application Use ).
However, depending on where you are deploying your application it is highly likely that all of those ports are blocked via firewalls; and neither will solve your problem
Most messenger type applications can't go direct due to firewall issues. For example, even if you use port 80, its likely the client machines have that port blocked for incoming TCP requests.
Instead they typically connect to a known public server. When one client wishes to connect to a different one, the server will route the message between the two clients. A very simplified look at this is: Client A sends a communication request to the server for Client B. Client B polls the server for messages, sees one and shows it on the desktop.
There are ways to keep the TCP connection alive between the clients and server in order to speed up communications; but that's the basics.
There are even ways for clients to directly talk to each other, when they determine that certain ports are open for communication or that proxy servers aren't going to interfere with the traffic. However, that's a little more advanced than a simple "answer" here can provide.
I am writing a custom p2p program that runs on port 4900. In some cases when the person is behind a router, this port is not accessible from the internet.
Is there an automatic way of enabling the access to the port from the internet. I am not really sure of how other p2p applications work.
Can anyone please throw some light on this?
P2P connectivity in a nutshell. Assume we're talking about UDP here. The steps below can also be applied to TCP with some adjustments.
Enumerate all your local IP addresses (usually only 1). Create a UDP socket on a given port number** for each adapter with an IP address.
For each socket created in step 1, contact a STUN or TURN server with that same socket to discover your external IP address and to discover what the internal port number maps to outside of the NAT (it's not always the same port value). That is, your local address 192.168.1.2:4900 might be 128.11.12.13:8888 to the outside world. And some NATs don't always use the same port mapping when using the same local port to other IP addresses. TURN will also provide you a "relay address". You can also use UPNP to get a port mapped address directly from your router, if it supports that protocol.
Through a rendezvous service (SIP, XMPP, instant message, web service, email, cups with strings), publish your address candidate list to a service or send a notification to the other client that says, "hey, I want to connect with you". This message includes all the "address candidates" (ip and port pairs) collected in steps 1 and 2.
The remote client, upon receiving the invite to connect, performs step 1 and 2 above as well. Then sends back his candidate list through the same channel that he received the inviter's candidate list on.
Hole punching step. Both clients, start sending test messages over UDP to the other side's address candidates and listening for the same messages on their end. Whenever a messages is received, reply back to the address from which it came. Eventually, the clients will discover that they have a pair of addresses that they can reliably send datagrams too. Typically, one endpoint makes the final decision on which address pair (sockets) to communicate with and the protocol facilitates this endpoint telling the other endpoint this decision.
**- usually best to not to rely on a well known port for P2P clients. Because two clients behind the same NAT or firewall would not likely be able to use your software at the same time.
Here is a quick summary of some technologies to explore.
STUN - Is a simple server and protocol for clients behind a NAT/route to discover what their external IP and port mappings are.
TURN is an expansion to STUN, but supports relaying for P2P connectivity scenarios where firewalls and NATs prevent direct connections.
ICE is a set of steps by which STUN and TURN are used for setting up a P2P connection. ICE is a formal protocol for steps 1-5 above. Two excellent set of slides on ICE are here and here.
WebRTC is a variant of the ICE standard as well as a reference library for make P2P sessions with STUN and TURN.
UPNP + Internet Gateway Device Protocol - Some routers support this for hosts to automatically obtain port mappings.
libnice is an open source C library for Linux (and might work on windows) that implements ICE.
libjingle is another ICE implementation (in C++) from Google. For Windows and Linux.
PJNATH is a library within the PJSIP suite of coding libraries. It is a good implementation of an ICE stack (C code) and has been ported to a lot of platforms. (Windows, Linux, Mac, iOS, Symbian, and soon Android).
And finally, I have a blatant plug for you to use my STUN server code base.
There are solutions in some cases, see UPnP: https://en.wikipedia.org/wiki/Universal_Plug_and_Play#NAT_traversal
My home router allows this, basically, the NAT can be configured automatically by the proper request from the computer.
I would not count on this to provide a big improvement in your availability, because not that many routers both support that and have it enabled.
EDIT: #David suggested this SO question for a .NET library for UPnP: Is there a UPnP Library for .NET (C# or VB.NET)?
I would use WebRTC technology as an open source framework for such application.
Official Website
In fact it is an open source project which supports all necessary for peer-to-peer technologies out of the box:
ICE and STUN (NAT traversal)
DTLS and SRTP (security)
AVPF for quality of streaming.
This may be a little more complicated than what you're looking for, but TCP Hole Punching is a technique that should work. http://en.wikipedia.org/wiki/TCP_hole_punching
Alternatively, UPnP works great for routers / firewalls that support it.
You have another option that is NAT Port Mapping Protocol (NAT-PMP)
NAT-PMP is widely used by VoIP applications like Skype or BitTorrent P2P clients.
For the simple beginnig I would recommend to see hole punching technique. Great video here. But be sure it will not always work relating the network topology. This solves the ICE technique which discovers how connection could be established.
I use a basic application that enables me to send a file over a tcp connection over the internet to a different computer. The way I managed to do that was to configure the router at my house and office to port forward all incoming traffic from port X to the corresponding computer. I am in a new office now and things work different in here.
There is a main router where all the offices may connect to. I connect my router to that router in order to have my own private network. I still want to be able to use my small application that I created in order to sync the files from my home computer and office computer. The only problem is that I dont have access to the main router therefore I may not open any ports in order to make my program work. I am new to networking so it will be nice if someone can point me to the right direction of how can I solve this. I think I have to let the router know to send all the traffic from port x to my computer. Moreover there are several routers so I dont know if it is possible. I think it should be possible because I am able to connect to my office computer via log me in for example.
It sill be nice if I can still use TCP protocols instead of a p2p since I already have all the functionality.
It would be much easier to connect from your office to your home computer. In this scenario, you have to setup port forwarding on your personal router (which you have already done!). If you connect from your home to the office, you will need to configure every intermediate office router (which your boss probably won't like).
In order to connect to your home network, I would look into setting up DDNS through someone like DynDNS. This will allow you to connect to me.example.com from wherever and have it resolve to your home address even when it changes IP addresses.
I found a nice page that talks about this in here. I will work on it... I am not sure if it works with the tcp protocol.
I made a remote engine for a game which must be able to works in P2P.
It perfectly works in LAN, but there's a problem when computers are behind router(s) and want to communicate through internet.
Is there any solution to this, which doesn't need to manipulate the router configuration?
Because since most of my gamers may not be very acknowledged in informatic, I'd like to solve this problem as easily as possible, without any intervention from them.
Thanks,
KiTe.
You need the client behind the router to initiate an OUTGOING connection. Once that's established you can have 2 way communication on it. This is why most P2P games have some sort of server to set up matches between clients. You can have each client establish a socket to the server and then connect them to each other.
There was an alternative called 'NAT hole punching' a while back, but I'm not sure how reliable that was.
This is what separates LogMeIn from VNC.
These days almost all home users are behind a form of NAT, macking it impossible in practice to set up real peer-to-peer communication as the application listenning port is unreachable from the net.
In theory there is UPnP which allows applications( running under elevated priviledges) to enable port forwarding dynamically on the home router (via Internet Gateway Device Protocol), but in practice this is so unreliable that I haven't seen any real use of it.
The most reliable solution is to have a central hub (your game server) that forwards packets between clients that initiate the connection from behind the NAT device. But that is a serious cost to you, as you'll need to cash out the cost of provisioning and operating this hubs which can be serious money, even with dynamic just-in-time solutions like EC2.
Update
Perhaps you can use the Codeplex UPnP NAT traveral project.