Just to help out new comers. If you don't want to use the default gui, you'll have to override OnReceivedBroadcast() and parse the data string. You then can join a game directly or add the game to a server browser. @seanr Is it always Code (CSharp): NetworkManager:adr:port ?
I'd like to have my android phone act as a client and my desktop machine act as server but I've been unable to get this example working for this scenario. If I run this example on my desktop (Initilalise Broadcast > Start Broadcasting) and laptop (Initialize Broadcast > Listen for Broadcast) the laptop successfully reports the desktop IP. But if i export the scene and run it on my android phone (Initialize Broadcast > Listen for Broadcast) it fails to find the server (doesn't report game IP). All machines are connected to the same wifi network. Does anyone have an idea of what I may be doing wrong or what I need to look at to get the android client to see the server? (I can provide additional information about my setup if necessary of course)
Hi, seanr, I came across some problem when trying to change broadcastData in NetworkDiscovery, here is my post:http://forum.unity3d.com/threads/networkdiscovery-broadcastdata-can-not-change.368308/ can you help me?
Hi seanr (@seanr ) When I initialize the broadcast as server, the other device(client) is connected to the server (connectivity successful) IP. But the packets are not being sent from the game port. I keep getting this error message : " UNet Client Disconnect Error: Timeout UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate() site:forum.unity3d.com". But works completely fine when I run the server and client on the same system.
@seanr Hi. Tested this sample in editor (5.2.3f1). On 3rd host restart (Lan host, stop, lan host, stop, lan host) I get several errors: Broadcast discovery has been already running. Stop discovery first before repeat this call UnityEngine.Networking.NetworkDiscovery:StartAsServer() MyNetManager:OnStartHost() (at Assets/MyNetManager.cs:13) UnityEngine.Networking.NetworkManagerHUD:OnGUI() NetworkDiscovery StartBroadcast failed err: 8 UnityEngine.Networking.NetworkDiscovery:StartAsServer() MyNetManager:OnStartHost() (at Assets/MyNetManager.cs:13) UnityEngine.Networking.NetworkManagerHUD:OnGUI() Cannot open socket on ip {*} and port {7777}; check please your network, most probably port has been already occupied UnityEngine.Networking.NetworkManagerHUD:OnGUI() StartServer listen failed. UnityEngine.Networking.NetworkManagerHUD:OnGUI()
Hi @seanr Sorry for my bad English, I am trying to run your example of Network Discovery. If I run server and client both on my PC its working fine. If I run server on other PC within same network and I run client on different PC. Its is finding server(IP) after click on 'Listen for Broadcast' but after click on IP button, my Player does not create clone. I am working with Unity 5.3.
Hey - did you manage to get this resolved? I am trying to do exactly the same thing - have the PC as server and android phone as client
Can this class be used to have two phones discover each other without knowing which one will be server/client? The only way to know that is to have the user select an option, but we'd like a more seamless, no-interaction type experience for our casual audience. I've posted more detail about my question in the link below, but I thought I'd mention it here since it's very related to the capabilities of NetworkDiscovery. http://forum.unity3d.com/threads/co...action-over-local-wifi-is-it-possible.375724/
Oh weird, i thought i posted a reply to this already but it looks like it got eaten somewhere. Short version: Not all android phones accept multicast messages. The ones that don't (e.g. Motorola Moto G and moto E) won't 'auto discover' the server with the approach used in this script. Others will (e.g. Nexus 5). I ended up exposing an input field for non-auto-discovery phone users so they could manually enter the IP of the server if auto-connect wasn't happening.
Hi - I'm a beginner with the whole multiplayer side of things and just trying to get my head around NetworkDiscovery. When I run this example the sphere gets spawned when I use the LAN Host and client. But when I use broadcast and and connect to a game nothing gets spawned, how can I spawn from the NetworkManager once connected through the Broadcast? Many Thanks!
I am trying to get this working without the GUIs, both the Discovery GUI and the Network Manager GUI, but I can't figure out how. What I want to achieve is similar to @bitbutter 's question: connect my android phone to my desktop PC. So in high level code what I want is: #if UNITY_EDITOR || UNITY_STANDALONE Initialize() StartAsServer() #elif UNITY_ANDROID Initialize() StartAsClient() JoinGame() #endif That last JoinGame obviosuly does not exist. For that I would need to override OnReceivedBroadcast() as @HugoZandel mentioned. So I tried to make my own NetworkDiscovery class, see below, using the example OnBroadcastReceived from the Unity documentation. I then replaced @seanr 's NetworkManager with the default NetworkManager, but when I start the app first in the editor and then on my phone I don't see a playerSphere appear. I do get the debug message "Started as server" in the editor. Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.Networking; public class MyNetDiscovery : NetworkDiscovery { void Start () { #if UNITY_EDITOR Initialize(); StartAsServer(); Debug.Log("Started as server"); #elif UNITY_ANDROID Initialize(); StartAsClient(); #endif } public override void OnReceivedBroadcast(string fromAddress, string data) { NetworkManager.singleton.networkAddress = fromAddress; NetworkManager.singleton.StartClient(); } } Thanks! Edit 2016-01-20: I did some more debugging and added a second scene to start when the connection is established (added MyOne as offline scene and two as online scene in the NetworkManager). I found that I do get to the end of the OnReceivedBroadcast, but the online scene never loads, neither on the server nor on the client. The same happens if I use the example's scene one. Also in that case I do not get to scene two. Although I am not completely sure I did everything right because it is very, very hard to see the GUI on my mobile phone, it is incredibly small. But I think I saw a button with connect to and an IP-address. And when I tapped on it the discovery buttons disappeared, although the network manager GUI was still on screen. I then tried to use a standalone version as client and the editor as server. Now I could read the button which said Game on ffff: and my PC's IP-address, but after clicking on it the same result. The broadcast GUI disappears but the Network Manager GUI remains on screen and still no second scene. Do I need to call something like NetworkManager.OnClientReady?? I would expect that this is handled by the Discovery component.
After some more searching, cursing and retrying I found that I needed to start the NetworkManager on the server side as well. I added NetworkManager.singleton.StartServer() in the #if UNITY_EDITOR section after StartAsServer().
Oops there seems some bug with NetworkDiscovery. When NetworkDiscovery.StartAsServer is invoked. The Object will become don't destroy on load Code (CSharp): public bool StartAsServer() { if (this.m_HostId != -1 || this.m_Running) { if (LogFilter.logWarn) { Debug.LogWarning("NetworkDiscovery StartAsServer already started"); } return false; } this.m_HostId = NetworkTransport.AddHost(this.m_DefaultTopology, 0); if (this.m_HostId == -1) { if (LogFilter.logError) { Debug.LogError("NetworkDiscovery StartAsServer - addHost failed"); } return false; } byte b; if (!NetworkTransport.StartBroadcastDiscovery(this.m_HostId, this.m_BroadcastPort, this.m_BroadcastKey, this.m_BroadcastVersion, this.m_BroadcastSubVersion, this.m_MsgOutBuffer, this.m_MsgOutBuffer.Length, this.m_BroadcastInterval, out b)) { if (LogFilter.logError) { Debug.LogError("NetworkDiscovery StartBroadcast failed err: " + b); } return false; } this.m_Running = true; this.m_IsServer = true; if (LogFilter.logDebug) { Debug.Log("StartAsServer Discovery broadcasting"); } Object.DontDestroyOnLoad(base.gameObject); return true; } I know that's necessary when enter game scene, we need a component to control lan game. But If I have two Scenes : LanGame, Online If I go back from Online scene to LanGame scene, NetworkDicovery object will become duplicated. My current workAround is write another script inherit NetworkDiscovery, and make it as singleton. When I enter LanGame scene, stop NetworkDiscovery if it's running. Does official expect we manually destroy the old NetworkDiscoery?
Hi,the following question "Cannot open socket on ip {*} and port {7777}; check please your network, most probably port has been already occupied " ,do you have to solve the question?
Could someone help with the following situation for me? I am able to get the discovery working fine, but after the initial connections and either the client or host drops, I would like to know how to handle this. Right now if the host quits the app and then restarts, the application is not able to broadcast on the same connection again and there seems to be a stale connection somewhere meaning the client is unable to see the host anymore. Basically I would like to be able to handle the following: 1. Host starts broadcasting 2. Client finds broadcast connects 3. At some point host disconnects (app quit for example) 4. Host restarts the app, needs to start broadcasting again so client can return and find broadcast again. Right now the host cant seem to be able to connect on the same port again, its like its being used and was not closed correctly when the host quit. Is there some method I need to call to ensure the connection is severed properly so on next start it will be able to connect it again?
I had the same issue. Locally on my laptop, I would run the server and client just fine in two separate applications (editor + standalone) but it would fail to work when running the server on my laptop and a client on my android phone. As it turns out, the default NetworkDiscovery implementation does not work correctly out of the box. While it will show you the correct broadcasted IP when you listen in (with their debug GUI), when you attempt to join it, it always tries to join with 'localhost' as the network address. The solution for me was simply to override the default implementation and set the NetworkManager.singleton.networkAddress with the received broadcast address, more or less exactly as documented here: http://docs.unity3d.com/ScriptReference/Networking.NetworkDiscovery.html Note that you'll likely want to stop the broadcasting after your connection attempt to avoid reconnecting every time you receive a new broadcast.
Hello every one i run the project i see the movement of server in client but the movement in client does not show in server?? any idea?? i use network transform and what is the difference between Lan host and Lan server i want to make a 2players game
You're a life saver! This saved me from having to rewrite a lot of my networking to not use UNet THANK YOU!
BUMP @Qwertyuiop1234 @seanr I am struggling with the same issue. Cannot open socket on ip {*} and port {7777}; check please your network, most probably port has been already occupied UnityEngine.Networking.NetworkManagerHUD:OnGUI() StartServer listen failed. Once a client disconnects, the server cannot start broadcasting again as it says the port is already being used. How do I close the port before disconnecting the current game or starting a new game.
To get a better understanding of how it works, you can have a look into the Network Discovery source code. I remember that I had issues when I tried to call the StopBroadcast function from within my network manager and it was followed by something like StopHost afterwards. In this case, the StopHost function interrupted the proper completion of the StopBroadcast function. So, as a result, discovery was still broadcasting after quitting the game and it was not possible to start a new broadcast. Also, a related issue had been reported which is marked as being fixed for 5.5.
hello, had a similar issue... here is how i fixed that... Code (CSharp): public override void OnStopHost() { if(myNetworkDiscovery.running){ //myNetworkDiscovery.Initialize(); myNetworkDiscovery.StopBroadcast(); } if (NetworkTransport.IsBroadcastDiscoveryRunning()) NetworkTransport.StopBroadcastDiscovery(); NetworkTransport.RemoveHost (0); isRunning = false; // Debug.Log("OnStopHost"); } NetworkTransport.RemoveHost (0); did the trick... M.
Hi @seanr, i'm not sure if it's the good place to report that but i think i may have found a problem in the "NetworkDiscovery.cs" source code. The problem is located in the "Update" function when "OnReceivedBroadcast" is called ( here ), there "you" are passing the "m_MsgInBuffer" as parameter while, i think, it should be "recv.broadcastData" to be passed, else the data size is not consistent between the sender/receiver (sender = broadcastData size, receiver = m_MsgInBuffer size). I noticed this while trying to encrypt/serialize a string to be used as broadcastData, the server will serialize the string making say 200 bytes in length, the client will receive the string but with a 512 bytes in lenght (m_MsgInBuffer size instead receivedSize). I hope i'm clear and this will be fixed soon so i can use stock sources ! Thanks, Cpasjuste.
@cpasjuste: Unet devs do not consequently check forum threads for bug reports. Also, I guess seanR is not an official member of the UNET staff since more than a year. So, you should really use the issue tracker to report the bug (I think you need to raise the ticket directly from the Unity editor). This will drastically increase chances that someone looks into it and that it actually gets fixed. The past has shown that this works quite well. While there are exceptions, describing potential bugs in the forum, however, usually does not work well.
Hey @Qwertyuiop1234 @seanr @Muckel @eltronix do any of you have a simple example of restarting Broadcast Discovery on a server as I am having the same problems on 5.5.0f3. I tried Muckel's fix of adding NetworkTransport.RemoveHost (0) to OnStopHost() but this didn't fix anything. Thanks!
[ WHY I CANT CLICK "LISTEN FOR BROADCAST" MORE THAN ONE CLIENT ] Hi all.. I just tried the project file you provided. i have a small problem. when i run 3 instances of the output exe...let say A,B,C 1.in A, i clicked lan Host 2.in B, i clicked lan Client 3.in C,i clicked lan Cleint works perfect...but 1.when i click Initialize broadcasting in A ,B,C 2. and i clicked Start broadcasting in A 3. and i clicked Listen for broadcasting in B, and it shows the A's Game... but the problem is why i can click Listen for broadcasting in C... it's not getting connected... or i understand totally wrong... Please help me in this. Thanks
hey @Arulbabu I think It is a bit late (toooooooooooooooooooo late) but if anyone came across a similar issue not being able to open multiple "Listen for broadcasting" on same machine the problem will be that the first time you will reserve the port of listening for the broadcast so the next time, you won't be able to listen on the same port (I don't know if there is a way to fix it like to use another port; and I don't there will be one too)
hey @Arulbabu @Xpoint It's been too late but I feel it might help you for network discovery. go here.
In the screenshot, it shows a simple game. in this game P1 is the user and P2 acts like a bot. P1 has the option to choose 3 buttons at the bottom and when he selects any one of those, it is compared with P2’s choice(P2’s option is chosen automatically in the script using Random()).So as of now its like playing with CPU opponent and not with a real player. So i wanted to make this a multiplayer, where the opponent will choose one of the options and player will also choose and a winner will be decided based on a logic. I have gone through the UNET docs, completed the multiplayer sample its connecting well and fine. I tested it on the two phones and its working too. So can anyone help me in creating this multiplayer in unity? It would be really helpful . The network connectivity I’m prefering is just local Wifi hotspot and not internet.
Hello @cpasjuste, do ya know if the problem is fixed? and can you post plz a example code how to fix it? ,many many thx M.
I ran an instance of the game using the file attached here on my laptop and other instance on my android phone. I turn on hotspot, connect my laptop to phone's hotspot, start broadcasting on phone and listen on laptop, I see "game available at 192.168.43.1", when I click it, it says "connecting". The discovery is working, but the game scene (online scene) never starts, the clients doesn't connect. What do I do?