Sometimes you want to have your server behind an internet router (NAT router).
That’s probably when you run your server on a development device behind an internet router. There are occasions when your server has to operate in a server device behind an L4 switch. Also there are occasions when your server must operate in a cloud server, which requires port forwarding.
At this time you will configure the port forwarding of your router. And you have to do additional configuration in CNetServer. If there is no such configuration, unreliable messaging will always use TCP, which will lead to problems. Also, there will be the problem that P2P messaging always uses TCP relay.
Let’s look at the example below. The Server1 and Server2 are below the router. Each server has the private addresses 100.10.0.1 and 100.10.0.2. The router performs port forwarding to 11.22.33.44:5555 and 11.22.33.44:5556 to each server.
Enter the parameter Proud.CStartServerParameter.m_serverAddrAtClient when you call CNetServer.Start. In the case of the picture above, you have to enter 11.22.33.44, which is the public address of NAT router.
You will also do UPD port forwarding to the NAT router. You probably want to configure the port forwarding as shown below.
Since you have set port forwarding in the 6000~6500 port range, you need to configure CNetServer to use UDP ports 6000~6500. For this, configure the UDP port assign mode as static. And make sure that the port that CNetServer will use is between 6000 and 6500, as shown in the code below.
////////////////// // C++CNetServer* s; ...; CStartServerParameter p; ...; p.m_serverAddrAtClient = "11.22.33.44"; p.m_udpAssignMode = ServerUdpAssignMode_Static; for(int i=6000;i<=6500;i++) p.m_udpPorts.Add(i); s->Start(p); /////////////////// // C# NetServer s; ...; StartServerParameter p; ...; p.serverAddrAtClient = "11.22.33.44"; p.udpAssignMode = ServerUdpAssignMode.Static; for(int i=6000;i<=6500;i++) p.udpPorts.Add(i); s.Start(p);
Let’s see whether it’s working well. Let’s check whether the two clients form a P2P group and when they receive and send messages, that they are transmitted via P2P. Considering the characteristics of ProudNet, messages transmitted via P2P are switched to direct P2P after a few seconds.
When you receive a message via P2P, check if the value of parameter RmiContext is switched to false. Or, check if the value of m_relayed in the parameter RmiContext is changed to false. Or, check if OnChangeP2PRelayState is called and OK is in the parameter.
If you do not achieve the result you want even if you have done so, use CNetServer.EnableLog. You can find the cause of the problem from the log generated from here.