0

I have been trying to do simple networking in which i can send a message from one device to another. My first thoughts were 'Would I have to pay to use unity multiplayer functionality?' so I found this article which seemed clear and directly explained how I would need to do networking in order to maintain zero costs on my behalf. After further searching I discovered this bit of unity documentation which seems to explain how to do networking simply in the same way described in the previous article and eluded to simple low level API message sending which I desired. [Important Note: I work in C#]

But I have seemed to ground to a halt in this journey of discovery as I have found no further articles or videos to guide me and answer my questions which i still have about how this works.

Hence today i am asking :

A - Do you know of any further articles or videos which would guide me through this?

B - Am i going totally in the wrong direction?

C - Could you answer any of my key questions as below?

  1. When creating a server, i.e below, what port should i listen on and should I worry about 'port forwarding' which is a phrase I see often chucked about.

    NetworkServer.Listen(4444);

  2. When creating a client, i.e below, what i.p address should i use? I presume it is that of the client's device but how do i know what it is?

    myClient.Connect("127.0.0.1", 4444);

  3. How do i get 2 devices to connect, my idea was to make any device by default become a client and then somehow 'check for servers' and if not it would create its own server with local client for a client to connect to, this way there would be no excess servers... ?

Thanks you very much for reading my question and I hope it seems clear, please check the links so that this will make sense to you. Any answers are greatly appreciated so that i can continue on my journey of understanding.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
g_l
  • 201
  • 1
  • 6
  • 15

2 Answers2

0

First of all no you don't have to pay to get multiplayer functionality in your game with Unity (At least up to a certain point). Eventually you will obviously need to either buy or rent a server to host the server portion of the game should you require one.

The Unity Networking API has been improved a lot since last time I made a multiplayer game and so has the documentation.

  • I would suggest start reading through and follow along with: The multiplayer tutorial.
  • Additionally you could take a look at: Unity's own network lobby example This is a working multiplayer lobby example that you could have a look at. Just create a new Unity project and import it to get started.

To answer your "key questions":

  1. You shouldn't have to worry about port forwarding. Unity's networking API will work without requiring your players to forward ports or any such thing.
  2. This is also not something you should worry about. Unity will take care of that, just read through the documentation and you will see.
  3. Again read through the documentation links that I have provided and it should become clear. In your case since you wanted to avoid extra costs to yourself you will probably go with the option of 1 player becoming the "Server" while also being a client. I could go into it here but the tutorials will provide a much better explanation than I could give.

Good luck with your project, I hope it works out for you.

  • AE
A.E
  • 125
  • 3
  • 11
  • Thank you very much for your reply but having read the first article which i have linked i am hesitant as it clearly says "You can use Unity HLAPI without paying. Just don't use the Unity match making API, then you won't need to pay for anything." and "You should NOT use NetworkManager because most of its function depends on Unity match making API. To create a network game that is free from Unity match making API, you just need the NetworkServer and the NetworkClient class." Can you persuade me that using unity API is worth it? – g_l Jul 30 '16 at 13:18
  • Also one of the features which i liked with the network server and class was how it involved sending simple messages which i felt was more compatible with my game, I would have thought that the unity API includes that but can you confirm it @A.E – g_l Jul 30 '16 at 13:24
0
  1. what port should i listen on and should I worry about 'port forwarding' which is a phrase I see often chucked about.

That's up to you. You can use any port you want. The correct way to do this is to select about 10 ports for your game then chose 1 port as a default port. For example, let's chose from port 10000 to 10010. Let's make port 10000 your default game port.

If you try to connect to the default (10000) with NetworkServer.Listen(10000) but there is another software using the port or the port is not available to be used, NetworkServer.Listen will return false and throw the following exception:

Cannot open socket on ip {*} and port {10000}; check please your network, most probably port has been already occupied

When this happen, you can try the next port, another next port and another one until you reach your max game port which is 10010.

Also, in your game instruction, you can instruct players to open port 10000 to 10010 when they are having problems connecting to another player.

Implementation(Read comments in code):

int minPort = 10000;
int maxPort = 10010;
int defaultPort = 10000;

//Creates a server then returns the port the server is created with. Returns -1 if server is not created
int createServer()
{
    int serverPort = -1;
    //Connect to default port
    bool serverCreated = NetworkServer.Listen(defaultPort);
    if (serverCreated)
    {
        serverPort = defaultPort;
        Debug.Log("Server Created with deafault port");
    }
    else
    {
        Debug.Log("Failed to create with the default port");
        //Try to create server with other port from min to max except the default port which we trid already
        for (int tempPort = minPort; tempPort <= maxPort; tempPort++)
        {
            //Skip the default port since we have already tried it
            if (tempPort != defaultPort)
            {
                //Exit loop if successfully create a server
                if (NetworkServer.Listen(tempPort))
                {
                    serverPort = tempPort;
                    break;
                }

                //If this is the max port and server is not still created, show, failed to create server error
                if (tempPort == maxPort)
                {
                    Debug.LogError("Failed to create server");
                }
            }
        }
    }
    return serverPort;
}

Usage:

void Start()
{
    int serverPort = createServer();
    if (serverPort != -1)
    {
        Debug.Log("Server created on port : " + serverPort);
    }
    else
    {
        Debug.Log("Failed to create Server");
    }
}
  1. When creating a client, i.e below, what i.p address should i use? I presume it is that of the client's device but how do i know what it is?

In the old days, you had to broadcast your IP to the network with UDP. To make it short, you get your IP Address from your computer then broadcast it to 255.255.255.255.

You can also broadcast to the modified version of your IP Address. Let's assume that your ip is 192.168.1.13, you should remove the 13(last octet) and replace it with 255 then broadcast your ip to 192.168.1.255. I explained this more here.

It is important to understand what broadcasting is but Unity made it simpler by creating a broadcasting API NetworkDiscovery.

To know which IP Address to connect to, simply call NetworkDiscovery.StartAsServer() on the server side in the Start() function.

On your client side, call NetworkDiscovery.StartAsClient() then implement the OnReceivedBroadcast(string fromAddress, string data); function. When server is found OnReceivedBroadcast(string fromAddress, string data); will be called and you can then use the returned fromAddress value to connect to your server.

This should also answer your #3 question. Also, I am glad you found my other answer useful.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thanks this sounds very good, I understand it takes a lot of time and effort to write answers and I hope this can be used as reference for those who want to follow the server client networking method in the future as suggested by your previously answered question. I'm currently implementing your code I'll get back to you to see if it works perfectly. – g_l Jul 30 '16 at 14:21
  • I have added the code which I am using to test implantation to the question which is currently not working (in the way that I have set it up, most likely). @Programmer – g_l Jul 30 '16 at 15:56
  • @g_l This question is not actually about code. It's about how to do stuff which I think I provided enough info to get you started. You can ask a **new** question about why your code is not working. I will then then try to fix your code. Editing the question will be longer. Put those code in a new question. – Programmer Jul 30 '16 at 16:00
  • Thanks @Programmer – g_l Jul 30 '16 at 16:05
  • Np. Waiting for your new question. – Programmer Jul 30 '16 at 16:08