Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by lizard4455 · May 17, 2015 at 04:07 AM · networkingserverfreezeudplan

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);
     }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lizard4455 · May 30, 2015 at 04:08 PM 0
Share

Never $$anonymous$$d, I got something much simpler working

avatar image menkari lizard4455 · Sep 10, 2015 at 05:47 AM 1
Share

Would you be able to share your solution? Cheers

avatar image AttentionHorse lizard4455 · Feb 03, 2017 at 01:39 PM 0
Share

Having a very similar problem. Could you share how you solved it? Thank you!

avatar image Bunny83 AttentionHorse · Feb 03, 2017 at 02:02 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Unity UDP freezes if disconnected from server 2 Answers

What's the best approach for creating a secure authorative server? 0 Answers

Server controlling Client- No errors in Console- LAN 0 Answers

Receiving UDP data from a master server behind a router. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges