- Home /
How to port forward via script in unity
Trying to make a multiplayer game so my friends and I (and other people that wants to download it) can play together, but the problem is that we don't live under the same router, which gave me the problem of port forwarding. A lot of my friends don't understand this stuff (including me) so I want to make it so they don't have to do it. I've seen some forums talking about UPnP, but many of the guides/tutorials are vague or have nothing to do with my problem.
If you know a guide/tutorial bout port forwarding via script in unity please tell me, because that will help a lot!
I know that if you are using System.Net.Sockets to establish your connection, you can open a port by initializing a TCPSocket instance, and allow it to listen for any inco$$anonymous$$g IP addresses. There are also UDP connection options, but that protocol works a different way.
If you're looking for a simple TCP connection solution, I would recommend having a look at my repo on TCP networking. There are example scripts and the source code, if you want to look at how a server and a client can communicate over a TCP socket connection.
This is the basic necessary code for your port-forwarding issue:
// Opens up port and accepts any pending connection requests
var port = 1234;
var listener = new TcpListener(IPAddress.Any, port);
listener.Start();
The rest are in the Source folder. Hope that's what you asked for.