Search Unity

Working script for UDP Broadcast over LAN (to find servers)

Discussion in 'Multiplayer' started by Jordos, Nov 12, 2009.

  1. Jordos

    Jordos

    Joined:
    Sep 16, 2009
    Posts:
    84
    I thought to post a script I made that allows to do UDP broadcasting work over LAN. This can help to find servers on a LAN without knowing their IP.

    The script itself is designed to search for players and determine which player must start a server. This is because of the particular project I made it for. Anyway, it can easily be adapted to support standard create/join server functionality (see comment in script).

    I tried to comment it as good as I can, but if things are unclear, you can post here.

    Cheers,
    Jordin
     

    Attached Files:

  2. Jordos

    Jordos

    Joined:
    Sep 16, 2009
    Posts:
    84
    I found a small, but pretty fatal bug in the script that is fixed now. I recommend everybody who uses this script to download the new version.
     
  3. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Thanks for sharing!
     
  4. haxoan87

    haxoan87

    Joined:
    Oct 13, 2009
    Posts:
    11
    thank you very much! :)
     
  5. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Sweet awesome sauce of doom. This is fantastic.
     
  6. rellikt

    rellikt

    Joined:
    Aug 10, 2010
    Posts:
    1
    Good Job. Thanks for sharing.
     
  7. Mid

    Mid

    Joined:
    Nov 3, 2009
    Posts:
    27
    I've got several errors, could someone help me please? I don't understand what's going on.

    Code (csharp):
    1.  
    2. (82,118): error CS0103: The name `scrConnectionManager' does not exist in the current context
    3. (82,154): error CS1502: The best overloaded method match for `System.Net.IPEndPoint.IPEndPoint(System.Net.IPAddress, int)' has some invalid arguments
    4. (82,154): error CS1503: Argument `#2' cannot convert `object' expression to type `int'
    5. (82,26): error CS1502: The best overloaded method match for `System.Net.Sockets.UdpClient.Send(byte[], int, System.Net.IPEndPoint)' has some invalid arguments
    6. (82,26): error CS1503: Argument `#3' cannot convert `object' expression to type `System.Net.IPEndPoint'
    7. (242,38): error CS0103: The name `scrConnectionManager' does not exist in the current context
    8. (242,74): error CS1502: The best overloaded method match for `System.Net.Sockets.UdpClient.UdpClient(System.Net.Sockets.AddressFamily)' has some invalid arguments
    9. (242,74): error CS1503: Argument `#1' cannot convert `object' expression to type `System.Net.Sockets.AddressFamily'
    10.  
     
  8. Kevinpaulharper

    Kevinpaulharper

    Joined:
    Sep 6, 2009
    Posts:
    16
    I also get these errors, could it be because we are using a newer version of Unity and this script was made over two years ago?
     
  9. cerebrate

    cerebrate

    Joined:
    Jan 8, 2010
    Posts:
    261
    probably. by the looks of those errors, it's actually due to the sockets libraries changing. I never saw this script, and am now interested in it, so I might look at it sometime this week.
     
  10. sciops

    sciops

    Joined:
    May 18, 2008
    Posts:
    5
    Has anybody been able to port this successfully to Unity 3.4?
     
  11. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
  12. herpderpy

    herpderpy

    Joined:
    Mar 9, 2010
    Posts:
    477
    Thanks killer, checking it now :)
     
  13. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    That only broadcasts on port 22043.

    If you want it to broadcast and pickup multiple ports, you will have to use for loops (and I don't feel like it lol).
     
  14. Saulotti

    Saulotti

    Joined:
    Oct 20, 2009
    Posts:
    19
    Hey guys,

    i'm trying to use the script, but when i receive the callback for Starting a Server, and after i call StartAnnounceBroadCasting(), its says

    SocketException: Address already in use
    System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end)
    System.Net.Sockets.UdpClient.InitSocket (System.Net.EndPoint localEP)
    System.Net.Sockets.UdpClient..ctor (Int32 port)
    LANBroadcastService.StartBroadcastingSession () (at Assets/Scripts/Managers/LANBroadcastService.cs:242)​

    The line 242 is

    objUDPClient = new UdpClient(22043);​

    Does anyone have any clues about this issue?
     
  15. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,553
    Turns out this isn't allowed on a web build. Any ideas how to search for local LAN servers in a web build?

    @saulotti: that port is already in use in your machine. try another one.
     
  16. ThiagoSCF

    ThiagoSCF

    Joined:
    Oct 24, 2012
    Posts:
    14
    I know it's been a while, but I hope someone can help me.

    I am getting this error every time the EndAsyncReceive() is called:

    Internal_GetPlayer can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.


    When I comment all references to objSendersIPEndPoint the error stops, so I wonder if it is called from a different thread or something. I really don't know what's going on.

    My Unity version is 3.5.6f4.

    I appreciate any help
     
  17. superbriggs2

    superbriggs2

    Joined:
    Dec 28, 2012
    Posts:
    1
    The answer to the problem is here http://answers.unity3d.com/questions/175942/error-in-udp-connection-for-multiplayer-tutorial.html

    It happens because the code is calling Network.player.ipAddress and it can't because of thread issues. To fix it, add a member variable to the class and in the Start method, assign the value of Network.player.ipAddress (which you can access) into it. Now change the 3 time Network.player.ipAddress is being called and use the member variable instead.

    That works for me
     
  18. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    Due to the sandbox nature of web player from 3.0 and higher it's not possible to do it. Web player games can not be hosted by clients anymore without a proxy to relay messages.
    You should either use 2.6.1 or get rid of it.
     
  19. lunacy1207

    lunacy1207

    Joined:
    Dec 15, 2012
    Posts:
    6
    Hi,

    I got a problem of calling "StartSearchBroadCasting" function.
    What value should I fill in "delJoinServer connectToServer" and "delStartServer startServer"?

    Thanks,
     
  20. mylittlepony

    mylittlepony

    Joined:
    Jul 15, 2014
    Posts:
    3
    Unfortunately example does not work.
    Due to the fact that not all operations are performed in the threads.

    Network.player.ipAddress
    Time.time

    Cause errors.
     
  21. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052