1

So I am creating UNet multiplayer application but i came to a problem while receiving message.

Application is for android but I also built it for computer.

When I run Server through unity editor and connect to it with client from computer I have part of code to test connection and send message and it is working and send message when player connects.

When I connect with phone it doesn't send message.

What I know is on phone it return NetworkTransport.Connect() value as 1 which means that it connected, but I do not think it connected it and i will explain it after code.

Here is my server code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Text;

public class Server : MonoBehaviour
{
    private const int MAX_CONNECTION = 100;

    private int port = 7777;

    private int hostId;
    private int webHostId;

    private int reliableChannel;
    private int unreliableChannel;

    private bool isStarted;

    private byte error;

    private void Start ()
    {
        NetworkTransport.Init();
        ConnectionConfig cc = new ConnectionConfig();

        reliableChannel = cc.AddChannel(QosType.Reliable);
        unreliableChannel = cc.AddChannel(QosType.Unreliable);

        HostTopology topology = new HostTopology(cc, MAX_CONNECTION);

        hostId = NetworkTransport.AddHost(topology, port, null);
        webHostId = NetworkTransport.AddWebsocketHost(topology, port, null);

        isStarted = true;
    }

    private void Update()
    {
        if(!isStarted) { return; }
        int recHostId;
        int connectionId;
        int channelId;
        byte[] recBuffer = new byte[1024];
        int bufferSize = 1024;
        int dataSize;
        byte error;
        NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
        switch(recData)
        {
        case NetworkEventType.Nothing:
            break;
        case NetworkEventType.ConnectEvent:
            Debug.Log("Player " + connectionId + " has connected");
            break;
        case NetworkEventType.DataEvent:
            string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
            Debug.Log("Player " + connectionId + " has sent: " + msg);
            break;
        case NetworkEventType.DisconnectEvent:
            Debug.Log("Player " + connectionId + " has disconnected");
            break;
        }
    }
}

And here is Client Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Client : MonoBehaviour
{

    private const int MAX_CONNECTION = 100;

    private int port = 7777;

    private int hostId;
    private int webHostId;

    private int reliableChannel;
    private int unreliableChannel;

    private int connectionId;

    private float connectionTime;
    private bool isConnected;
    private bool isStarted = false;
    private byte error;

    private string playerName;

    public Text text;
    public void Connect()
    {
        string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
        if(pName == "")
        {
            Debug.Log("You must enter a name!");
            return;
        }

        playerName = pName;

        NetworkTransport.Init();
        ConnectionConfig cc = new ConnectionConfig();

        reliableChannel = cc.AddChannel(QosType.Reliable);
        unreliableChannel = cc.AddChannel(QosType.Unreliable);

        HostTopology topology = new HostTopology(cc, MAX_CONNECTION);

        hostId = NetworkTransport.AddHost(topology, 0);

        connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);

        connectionTime = Time.time;
        isConnected = true;
    }

    private void Update()
    {
        if(!isConnected) { return; }
        int recHostId;
        int connectionId;
        int channelId;
        byte[] recBuffer = new byte[1024];
        int bufferSize = 1024;
        int dataSize;
        byte error;
        NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
        switch(recData)
        {
        case NetworkEventType.Nothing:
            break;
        case NetworkEventType.ConnectEvent:
            break;
        case NetworkEventType.DataEvent:
            break;
        case NetworkEventType.DisconnectEvent:
            break;
        }
    }
}

Here is how i tested application step by step. For this testing i implemented line which change button text to connectionId which is connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error); and it comes right after that line

  1. Build application as .exe (for pc)
  2. Build and run application as .apk (for android) Run application inside editor (android build) - Server scene (which has server script)
  3. Run application on android and pc as clients - Client scene (which has client script)
  4. Input name and press Connect on PC client - inside unity console pops Player 1 has connected
  5. Input name and press Connect on Android client - nothing happens inside unity but button text changes to 1 (which means it connected)
  6. Again press Connect on PC Client (which add another client) and it should be player 3 since on android connection returned positive but instead it returns Player 2 has connected

I have no idea what is happening. Is there i need to do for android to connect or maybe because i am connecting with wifi (it was working normally with unity Network Manager but now i can not do it with Lower Api)

Aleksa Ristic
  • 2,394
  • 3
  • 23
  • 54
  • *" it doesn't send message"* There is no send code in the code from your question.... – Programmer Aug 05 '17 at 12:55
  • In server code under `switch`. It is doing ok whit computer client but not with android client. When i connect with computer client in unity console i get message, but when i do it with android i do not – Aleksa Ristic Aug 05 '17 at 12:57
  • Ok, It looks like you meant connection event not send/receive event. In your client code, why didn't you put `Debug.Log` in the `switch` statement as well to see what happens? – Programmer Aug 05 '17 at 13:06
  • I do not get you now. I already have on the server inside switch `Debug.Log`, with that i check if it is connected. – Aleksa Ristic Aug 05 '17 at 13:14
  • @Programmer i have added more info on my question – Aleksa Ristic Aug 05 '17 at 13:30
  • You still have not done what I said in my last comment. Add the `Debug.Log` such as `Debug.Log("Player " + connectionId + " has connected");` to the `switch` statement in your client code and see if that's called on your Android side. It's not hard to understand that. To make this easier you can use the `Text` component to see the log on Android instead of `Debug.Log`. – Programmer Aug 05 '17 at 13:36
  • I am using text one to debug on android. Going to try that now. – Aleksa Ristic Aug 05 '17 at 13:39
  • After 20 seconds i get `Disconnect` message on android client (not on server) – Aleksa Ristic Aug 05 '17 at 13:53
  • Interesting. Make sure to remove `NetworkEventType.Nothing:`. Only have `NetworkEventType.ConnectEvent:`, `NetworkEventType.DataEvent:` and `NetworkEventType.DisconnectEvent:` with their specific logs. Run again and tell me the output – Programmer Aug 05 '17 at 13:55
  • Same. I removed `Nothing` from both server and client, at client (android) it drops me message for disconnect, on server nothing. – Aleksa Ristic Aug 05 '17 at 14:44
  • I want to now if it is connecting at-all. Don't run the server. Just run the client/adnroid and see if the-same behavior is there or another behavior. – Programmer Aug 05 '17 at 15:16
  • Same behaviour, server not running, client drop disconnected message – Aleksa Ristic Aug 05 '17 at 15:21
  • thanks for the code – SadeepDarshana Apr 20 '20 at 22:48

1 Answers1

3

After performing all those tasks in the comment section, below are the possible reasons why this is not working:

1.NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);

If you run this on Android, it will try to connect to your Android device instead of your PC. You need to change "127.0.0.1" to the IP of your PC so that your Android device can connect to that.

Once you get that working, consider using NetworkDiscovery to automatically find the IP of the other device then connect to it.

2.Make sure that both devices(Android) and PC are connected to the-same network/Wi-Fi.

3.Firewall

The firewall on your computer can block incoming connections. You need to add Unity Editor or the built exe program if you are running your server from the standalone program into exceptions in the Firewall program so that it will allow incoming connections. Just Google "Allow programs through firewall" and you will find so many ways to do this.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • First one was the problem. I changed it to local pc address and it is working now. Thank you so much. One more question if you could answer. Is there way to find server on all local ips? I will try to figure it out later just to get little knowledge about LLAPI, but if you know where i could read about it i would be grateful. – Aleksa Ristic Aug 05 '17 at 17:01
  • You can find local ip with `NetworkDiscovery`. I think I have answered this before. I will do a quick search on my account. If I don't reply in 5 hours, create new question about this. – Programmer Aug 05 '17 at 18:16
  • On the server you use `NetworkDiscovery.StartAsServer()` to broadcasting messages to clients. On the client, you listen to the broadcast by calling `NetworkDiscovery.StartAsClient()` then implementing `OnReceivedBroadcast(string fromAddress, string data)` function which is called when it finds a server. You can then use `fromAddress` IP value to connect to the server. See [this](https://stackoverflow.com/a/38676518/3785314) – Programmer Aug 05 '17 at 21:51
  • Thank you so much. – Aleksa Ristic Aug 07 '17 at 11:12