1

I am developing LAN multiplayer game using UNET .

I have used UDP multicast to broadcast server ip and other devices can listen it.

I have extended network manger for some UI customization required.

The device which make server can broadcast its ip and other device automatically gets the server ip(it can be game name as well) in the list

On selecting server the client can join the game. I have avoided situation where user type ip.

I am happy because it works successfully when all player plays through router.

Worst part start when i make any device as Wifi Hotspot and other device joins it.

Even Server creation fails.

I can share my code as well if someone going to help me.

Is it issue with UNET ?

Is something specific solution needed to be used?

In wifi hotspot do we have to use different ports than normal router?

I can attach my code if someone has experienced with similar.

If this was the case then how default network manager provided by unity worked? It is working if we enter ip .

I have added few extra script sao user do not need to write ip and listen the server message.

I think my code is breaking due to udp multicast functionality or port issue.

I am attaching my code.

Server Code:

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using System.Text;
using UnityEngine.UI;
using UnityEngine.Networking; // added for test

public class ServerDemoSample : MonoBehaviour 
{
    public static ServerDemoSample instance;

    //public byte[] buffer;
    UdpClient serverOriginator;
    public string serverIP;
    public int broadcastPort = 8080; //THIS IS ORIGINAL
    //int broadcastPort = 7777;
    public IPAddress groupIP = IPAddress.Parse ("239.0.0.222");
    //public IPAddress groupIP = IPAddress.Parse ("224.0.1.0");

    public IPEndPoint remoteEP;

    public Text myIpText;
    public bool isRunning;

    public Button startGameButton;
    public Button creatServerButton;
    public bool iAmServer = false;

    void Start () 
    {
        //DontDestroyOnLoad (gameObject);
        myIpText.text = Network.player.ipAddress;
    }

    void Update()
    {
    }

    public void BroadcastServerIP() 
    {
        Debug.Log(Time.realtimeSinceStartup + ": Broadcasting IP:" + serverIP);
        byte[] buffer = ASCIIEncoding.ASCII.GetBytes(serverIP);
        serverOriginator.Send(buffer, buffer.Length, remoteEP);
        GLOBALS.instance.serverIpAddress = serverIP;
        isRunning = true;
    }

    public void StartServer()
    {
        serverIP = Network.player.ipAddress;
        iAmServer = true;
        GLOBALS.instance.iAmGlobalServer = true;
        //Create UDP Client for broadcasting the server
        serverOriginator = new UdpClient();

        serverOriginator.JoinMulticastGroup(groupIP);

        remoteEP = new IPEndPoint(groupIP, broadcastPort);
        GLOBALS.instance.serverIpAddress = serverIP;
        creatServerButton.transform.FindChild("Text").GetComponent<Text>().text = "Server Created!";
        //Broadcast IP
        InvokeRepeating("BroadcastServerIP", 0, 1f);
        print ("Server IP (SERVER_SCRIPT) :  " + serverIP);
    }
}

Client Code:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
using System;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Collections;
using System.Collections.Generic;

public class ClientDemoScript : MonoBehaviour 
{
    public static ClientDemoScript instance;
    UdpClient client;
    int receivePort = 8080; //THIS IS ORIGINAL
    public string serverIP_;
    IPAddress groupIP_ = IPAddress.Parse("239.0.0.222");
    public string ipFound;
    byte[] receivedBytes = {0,0,0,0}; // made private for all 

    IPEndPoint remoteEP_;
    public Text serverIp2Text;
    public string results;
    public Text displayServerList;

    public List<string> receivedIpList = new List<string>();

    void Start () 
    {
        instance = this;
        JoinServer();
    }

    void Update()
    {
        serverIp2Text.text = results;
    }

    public void ReceiveServerInfo(IAsyncResult result) 
    {   
        Debug.Log("Received Server Info");

        remoteEP_ = new IPEndPoint(IPAddress.Any, receivePort);

        if (client != null) 
        {
            if (result != null) 
            {
                receivedBytes = client.EndReceive (result, ref remoteEP_);
            } 
            else 
            {
                return;
            }
        }

        ipFound = Encoding.ASCII.GetString (receivedBytes); //made public var

        if (!receivedIpList.Contains (ipFound)) 
        {
            receivedIpList.Add (ipFound);
        }



        Debug.Log("ip: "+ ipFound);

        client.BeginReceive (new AsyncCallback (ReceiveServerInfo), null);
        GLOBALS.instance.serverIpAddress = ipFound;
    }

    public void JoinServer()
    {
        if (client == null) 
        {
            client = new UdpClient(receivePort);
            client.JoinMulticastGroup(groupIP_);
        }

        client.BeginReceive (new AsyncCallback (ReceiveServerInfo), null);
    }

}
mayur bhagat
  • 209
  • 3
  • 12
  • It is very likely that the Wifi Hotspot device disabled UPnP or does not support it at-all. You can check if UPnP is supported or enabled by going to the settings. It is usually 192.168.1.1 but each Wifi Hotspot is different so this might be different on yours. – Programmer May 25 '16 at 13:16
  • I am editing my question. If this was the case how the default network manager work in unity. I guess some issue with the code I have used. Code Functionlaity: UDP Multicast to share server IP. Procedure For Server: create IP END POINT and start Broadcasting on particular PORT and IP. For Client : create IP END POINT and start listening on particular PORT and IP. – mayur bhagat May 25 '16 at 13:29
  • I think the issue is present only for the device which acts as wifi hotspot server. So the device is not able to communicate with other. Here is similar question for native android. http://stackoverflow.com/questions/14080573/getting-wifi-broadcast-address-in-android-wifi-hotspot?rq=1 Now question is how to overcome this issue in unity. – mayur bhagat May 25 '16 at 13:54
  • Just looked at your code. Please change port 8080 to something else on both server and client. Port 8080 is used for HTTP server and not this. You can't randomly choose a port. You have to make sure that the port chosen is not a reserved port. You can use something like 7732. – Programmer May 25 '16 at 14:01
  • Also, you said it works but fails when you make any device as Wifi Hotspot...What does that mean? How do you make a device a Wifi Hotpspot? First time I read this I thought you said it doesn't work when you use a Wifi hotspot but I think I read it wrong now. Anyways, I will be out now and will reply to you in 5 hours. While I am out, please use this time to describe what you mean by making a device a hotspot. The steps you followed to do this and how each device connect to each other on the network... – Programmer May 25 '16 at 14:05
  • Port 8080 was last try. I have used 7778 7777 and in similar range but no luck>. Wifi hot spot means I active hot spot(tethering) in one mobile device. Other mobile connects to that mobile instead of router. here is beautiful game which is doing it correctly. This functionality is helpful when friend gather and no router around. Any one device can turn on tethering and other joins it. They create lan group using this facility. – mayur bhagat May 26 '16 at 07:17

0 Answers0