Client.Send not working as i expected.

Hello there, following Unity docs, i would like to send message from client to server, both is separate differently.
It’s working like client-dedicated server.
Now i’m hanging on send a custom message from client to server, it throwing an error without my understanding.
Error always show at :
m_Client.Send(MessageType.Login,loginMsg);
object reference not set to an instance of …
I try to add m_Client = new NetworkClient(); it’s show error that send without connect to server
and when i add m_Client.Connect(serverIp,serverPort); and check m_Client.isConnected, it’s never true !
I pretty sure that i’m wrong and would like to understand my fault.
And i just want to create Client-Server without inherit NetworkManager from Unity, just for study.
Here is my script :

Client

using UnityEngine;
using System.Net;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.Networking.Types;

public class ZNetworkConnection : NetworkBehaviour
{
    [SerializeField]private string serverURL;
    [SerializeField]private string serverIP;
    [SerializeField]private int serverPort;

    NetworkClient m_Client;

    private static ZNetworkConnection instance;

    public static ZNetworkConnection Instance { get { return instance; } }

    void Awake()
    {
        instance = this;
    }

    void Start ()
    {
        ConnectToServer();
    }
       
    protected void ConnectToServer()
    {
        Network.Connect(serverIP,serverPort);
    }

    protected void LoginToServer(string username,string password)
    {
        ZMessageTransfer.Instance.SendLoginMessage(username,password);
    }

    protected void OnConnectedToServer()
    {
        Debug.Log("Connect to server successfull");
        LoginToServer("Gun","12345");
    }
}

Server

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

public class ZMatchServer : NetworkManager
{
    public int listenPort;
    public int maxConnection;
    public bool enableNAT;

    public Text currentPlayer;
    public Text currentLog;

    private int onlinePlayer;

    void Start ()
    {
        Network.InitializeSecurity();
        Network.InitializeServer(maxConnection,listenPort,enableNAT);
        NetworkServer.RegisterHandler(ZMessageTransfer.MessageType.Login,OnPlayerLogin);
    }

    void OnPlayerLogin(NetworkMessage netMsg)
    {
        var loginMsg = netMsg.ReadMessage<ZMessageTransfer.LoginMessage>();
        string username = loginMsg.username;
        string password = loginMsg.password;
        currentLog.text = "Player send login " + username + " " + password;
    }

    void OnServerInitialized()
    {
        Debug.Log("Server Initialize completed! ");
        currentLog.text = "Server Initialize completed! ";
    }

    void OnPlayerConnected(NetworkPlayer player)
    {
        onlinePlayer += 1;
        currentPlayer.text = "Current player online : " + onlinePlayer.ToString();
        currentLog.text = "Player connected";
        Debug.Log("Player connected " );
    }

    void OnPlayerDisconnected(NetworkPlayer player)
    {
        onlinePlayer -= 1;
        currentPlayer.text = "Current player online : " + onlinePlayer.ToString();
        currentLog.text = "Player disconnected";
    }
}

Transfer message class

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class ZMessageTransfer : NetworkManager
{   
    private static ZMessageTransfer instance;

    public static ZMessageTransfer Instance { get { return instance; } }

    NetworkClient m_Client;

    public class MessageType
    {
        public static short Login = 1001;
    };

    public class LoginMessage : MessageBase
    {
        public string username;
        public string password;
    };

    void Awake ()
    {
        instance = this;
    }

    public void SendLoginMessage(string username, string password)
    {
        LoginMessage loginMsg = new LoginMessage();
        loginMsg.username = username;
        loginMsg.password = password;
        m_Client.Send(MessageType.Login,loginMsg);
    }
}

Well you are mixing old multiplayer and the new one. On the server use NetworkServer.Listen(port) to start a server and it returns a bool about being successful. The listen for client connections by using NetworkServer.RegisterHandler(MsgType.Connect,OnClientConnected);

Hope it helps. There is no security or NAT traversal functionality in uNet yet AFAIK at least.

1 Like

Thanks for help,
I was figure out exactly problem, part of it like what your said above.
The client setup must be
m_Client = new NetworkClient();
m_Client.RegisterHandler


m_Client.Connect(serverIP,serverPort);

and the Server same as docs given to us.
Look like i was execute LoginToServer too early, when the connection still not success and given the error.
But following this way i’m curious about NAT and Security issue, cause i’m not understand Network enough.

Well the thing is that security can be implemented by you. Simply create a custom NetworkConnectionClass and assign it to NetworkClient and NetworkServer then override send and receive methods and encrypt/decrypt the bytes send using an algorithm in System.Cryptography. The safest is to exchange keys of an AES setup using a RSA then use keys in AES to encrypt/decrypt data, in this way an RSA key should be known to both parties (client and server) and then server can send its AES public key in RSA encoded format to client to be used for actual encryption/decryption.

Be careful to not put the RSA key as a simple string in your app. at least xor it or something like that.