Search Unity

OnConnectedToServer is not being called

Discussion in 'Multiplayer' started by jason, Oct 14, 2007.

  1. jason

    jason

    Joined:
    Oct 12, 2007
    Posts:
    19
    I am try to connect to a server with the following script, but neither OnConnectedToServer nor OnFailedToConnect is being called.


    Code (csharp):
    1.  
    2. function Start () {
    3.     ConnectToServer();
    4. }
    5.  
    6. function ConnectToServer () {
    7.     Network.Connect("127.0.0.1", 25000);
    8.     GameObject.Find("GUI Text").guiText.text = "Connecting";
    9. }
    10.  
    11. function OnConnectedToServer() {
    12.     Debug.Log("Connected to server");
    13.     GameObject.Find("GUI Text").guiText.text = "Connected";
    14. }
    15.  
    16. function OnFailedToConnect(error: NetworkConnectionError)
    17. {
    18.     Debug.Log("Failed to connect to server");
    19.    GameObject.Find("GUI Text").guiText.text = "Failed";
    20. }
    21.  
     
  2. jason

    jason

    Joined:
    Oct 12, 2007
    Posts:
    19
    Apparently, it doesn't work if you have the server and client on the same computer.

    I moved the server game to my dedicated server and I can now connect. Why does unity not return a failed response?
     
  3. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    280
  4. pdsull

    pdsull

    Joined:
    Sep 16, 2007
    Posts:
    8
    I'm experiencing this same problem using a build of the server (as an independent app) and the client in Unity on the same computer at the same time. I don't think that counts as the same instance. However, the same combination works fine if I run the server on a different computer.
     
  5. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    280
    There shouldn't be any problems with running server and client on the same machine.

    What is displayed in the server and client logs (when it fails)? Does it change anything if you use the IP address of the machine directly instead of the loopback address (in case loopback is for some reason broken)?
     
  6. pdsull

    pdsull

    Joined:
    Sep 16, 2007
    Posts:
    8
    Here's what happens at least for me. The result is the same if I connect through the master server or enter the local IP address (127.0.0.1)

    When connecting, the console gives me the following messages:
    1) Connecting directly to host
    2) Running as client. No player ID set yet.
    3) Connected to (IP address and port number)

    The client's peerType never changes, and OnConnectedToServer() is never called. At least while the server is running. If I then go and quit the server at this point, the following messages appear in the console:

    4) a Debug.Log message I have in OnConnectedToServer() to let me know it is being called.
    5) Set player ID to 1
    6) The server has disconnected from the client. Most likely the server has shut down.
     
  7. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    280
    What about the server log? And the IP address, is it the localhost address, 127.0.0.1 or a private/public IP (I guess its a real address since its not printed)?

    Looks like the server loop is stalled or something, does the client not get the failed to connect callback after a while? What the server is saying during this transaction could be interesting.
     
  8. pdsull

    pdsull

    Joined:
    Sep 16, 2007
    Posts:
    8
    OK, I appear to be losing my mind. Using the localhost connection (127.0.0.1) and running the server in Unity and the client as a stand alone I was just able to connect. Thinking back, I believe it's possible that's not a combination I'd tried before because I just switched to using the localhost address recently.

    However, I also tried the same connecting through the master-server (server in unity and client as stand alone) and that worked too. Now I swear to god I tried that over and over before and it never worked. But for whatever reason its working now.

    The problem still exists when running the client in Unity and the server as a stand alone (both with localhost address and through the master-server). To answer your other question, the client does not receive the failed to connect call back. I swear to god I'm not crazy, but I am mighty confused.

    Thanks for all your help
     
  9. penslayer

    penslayer

    Joined:
    Apr 10, 2008
    Posts:
    2
    I'm having the exact same problem as you have described. Do you know what you did to fix it?
     
  10. penslayer

    penslayer

    Joined:
    Apr 10, 2008
    Posts:
    2
    The previous attempt was using the Car network demo. I tried running the application inside the editor as a client and connecting to a separately build app and web version of the demo acting as a server. Also tried it in reverse and it didn't work. (??)

    Now I just tried running the TPS-Auth as a dedicated server and connecting to that instead and the networking seems to work ok.

    What would be the difference between each attempt (besides the obvious)? Has anyone had any problems with the Car network demo working.

    BTW I'm running on MacOSX(Leopard) and using 127.0.0.1 and port 25001 for all attempts.
     
  11. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,000
    Necropost alert ;)
    but maybe it helps you or others.

    According to your explainations i guess you just forget to set the "Run In Background" option in Edit / ProjectSettings / Player.
    If that option is not set, your standalone server will freeze in background and it's not responding to any messages until you bring it back to front.
    When running in Editor-mode Unity maybe ignores this option (not tested) and when you bring up a standalone client, the server in the Unity-Editor keeps on running.

    Always keep that option in mind when doing network stuff ;)
    ps. same problem can occur with the "Animate Only If Visible" option on animated characters.
    (happend to me ... )
     
  12. early

    early

    Joined:
    Jun 21, 2013
    Posts:
    2
    Thanks Bunny, your last alert helps me, even though this thread was created a long time ago.
     
  13. Sargon_of_Akkad

    Sargon_of_Akkad

    Joined:
    Jan 16, 2011
    Posts:
    147
    I, too, would like to offer my undying thanks. I have no idea why, but I have been having the exact problem using network scripts on builds I know and use regularly, so for some reason the "run in background" option must have turned off (I copied a skeleton build project and used that) and it just didn't work. It was a nightmare, and I felt like I was losing my damned mind too, I went back and completely rebuilt it from the very basics and it was driving me mad!