- Home /
Need help on LAN server discovery via UDP Broadcast
I am trying to create a script that can create a server that sends out UDP broadcasts to help clients on the local network discover and connect to it. I used several sources to guide me in writing this code. It compiles without errors, however when I instantiate a gameobject with the script as a server, the editor instantly freezes and I have to kill it from task manager. It doesn't even generate any of the debug logs (Not even "before parse"). If anyone sees an issue in the code I would love to hear it, I have no clue what is wrong with it.
It also may be worth noting that when I put in an IP outside of the acceptable multicast range, it will make it as far as the "JoinMulticastGroup" as a server
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class Connection : MonoBehaviour{
public string stationID;
private int serverPort = 5000;
private string serverIp;
private int startupPort = 5100;
private IPAddress groupAddress;
private UdpClient udpClient;
private IPEndPoint remoteEnd;
void Start () {
Debug.Log ("Before Parse");
groupAddress = IPAddress.Parse("224.100.0.1");
Debug.Log (groupAddress);
if (stationID == "GameServer")
StartGameServer ();
else
StartGameClient ();
}
public void StartGameServer(){
Debug.Log ("StartGameServer");
NetworkConnectionError initStatus = Network.InitializeServer (6, serverPort, false);
Debug.Log ("status: " + initStatus);
StartBroadcast ();
}
public void StartGameClient(){
Debug.Log ("StartGameClient");
remoteEnd = new IPEndPoint(IPAddress.Any, startupPort);
udpClient = new UdpClient (remoteEnd);
udpClient.JoinMulticastGroup (groupAddress);
udpClient.BeginReceive (new System.AsyncCallback(ServerLookup), null);
MakeConnection ();
}
public IEnumerator MakeConnection(){
while (serverIp == null) {
yield return null;
}
while (Network.peerType == NetworkPeerType.Disconnected){
Debug.Log ("connecting: " + serverIp + ":" + serverPort);
NetworkConnectionError error;
error = Network.Connect(serverIp, serverPort);
Debug.Log("status: " + error);
WaitOne();
}
}
public void ServerLookup (System.IAsyncResult ar){
byte[] receiveBytes = udpClient.EndReceive (ar, ref remoteEnd);
serverIp = remoteEnd.Address.ToString ();
Debug.Log ("Server: " + serverIp);
}
public void StartBroadcast(){
udpClient = new UdpClient ();
udpClient.JoinMulticastGroup (groupAddress);
remoteEnd = new IPEndPoint (groupAddress, startupPort);
while (true) {
byte[] buffer = System.Text.Encoding.ASCII.GetBytes("GameServer");
udpClient.Send(buffer, buffer.Length, remoteEnd);
WaitOne ();
}
}
IEnumerator WaitOne() {
yield return new WaitForSeconds(1);
}
}
Never $$anonymous$$d, I got something much simpler working
Would you be able to share your solution? Cheers
Having a very similar problem. Could you share how you solved it? Thank you!
He was last seen in June 2015, so it's unlikely to get any response. The code in question does some very dangerous things since it uses multiple threads and use some shared variables. Though the main problem is that "StartBroadcast" is not a coroutine and the while loop freezes the game. Pretty much everything that looks like a coroutine is used wrong in this code as coroutines have to be started with StartCoroutine.
Also this code sample doesn't use a broadcast but a multicast.
If you have a similar problem, ask a seperate question with your code. If you just search for a ready.to-use solution, UnityAnswers is not the right place for that.