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);
}
}