2

I am using unet for client-server communication, when i broadcast a message from server(on PC) to client(on PC) it works fine but when i try the same from server(PC) to client(Hololens emulator) unet doesn't work.

NetClient.cs

public override void OnReceivedBroadcast(string fromAddress, string data)
    {
        fromAddress1 = fromAddress;
        portnum = data;
        Connect_with_server();
    }
 void Connect_with_server()
    {
            myclient = new NetworkClient();
            myclient.RegisterHandler(MsgType.Connect, OnConnected);
            myclient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
            myclient.RegisterHandler(ChatMsg.PlayPauseID, redgreen_val_received);
            myclient.Connect(fromAddress1, Convert.ToInt32(portnum));
    }

Server.cs

void Start()
    {
        startServer();
    }
 public void startServer()
    {
        int serverPort = createServer();
        if (serverPort != -1)
        {
            Debug.Log("Server created on port : " + serverPort);

            discovery.broadcastData = serverPort.ToString();
            discovery.Initialize();
            discovery.StartAsServer();
        }
        else
        {
            Debug.Log("Failed to create Server");
        }
    }


    int minPort = 10000;
    int maxPort = 10010;
    int defaultPort = 10000;
    private int createServer()
    {
        int serverPort = -1;
        bool serverCreated = NetworkServer.Listen(defaultPort);
        if (serverCreated)
        {
            serverPort = defaultPort;
        }
        else
        {
            Debug.Log("Failed to create with the default port");
            for (int tempPort = minPort; tempPort <= maxPort; tempPort++)
            {
                if (tempPort != defaultPort)
                {
                    if (NetworkServer.Listen(tempPort))
                    {
                        serverPort = tempPort;
                        break;
                    }
                    if (tempPort == maxPort)
                    {
                        Debug.LogError("Failed to create server");
                    }
                }
            }
        }
        return serverPort;
    }

    void OnClientConnected(NetworkMessage netMessage)
    {
        MyNetworkMessage messageContainer = new MyNetworkMessage();
        messageContainer.message = "Thanks for joining Mr.Client!";
        Debug.Log("Client connected: ");
    }

PS: I have turned on Private network client-server in UWP capability settings and also turned on PrivateNetworkClientServer and InternetClientServer in Player settings.

Suraksha Ajith
  • 872
  • 2
  • 12
  • 23

2 Answers2

1

May not be a direct solution to your problem, but when I use network broadcasting from Unity's UNET, HoloLens will often suddenly stop discovering the broadcast for no apparent reason. I simply turn the HoloLens off and on again (long pressing the power button) and the problem resolves.

Stepan Stulov
  • 161
  • 1
  • 5
0

Please confirm if you can ping the HoloLens emulator from your computer. If it unable to connect, follow the this case to fix the issue: HoloLens emulator connect to server on LAN

Besides, as for the NetworkManagerHUD, to my knowledge does not work on the HoloLens.

Hernando - MSFT
  • 2,895
  • 1
  • 5
  • 11