• 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 /
avatar image
0
Question by kevinseligmann · Sep 14, 2012 at 05:54 PM · errormultiplayernetworkserverconnect

Network.Connect failed.

Hello everyonw, yesterday I started designing the network features my game would use, and I encountered a problem when trying to make a basic connection between 2 clients.

Whenever I create the server, automatically I get disconnected from it. The console shows the following:

 Server initialized and ready on port: 9999.
 Local server connection disconnected

That's for the server part. I said 'well, let's check if a client can connect to be sure' and when a client tries to connect I get:

 The connection request to 127.0.0.1:9999 failed. Are you sure the server can be connected to?
 Could not connect to server: ConnectionFailed

So, I believe t$$anonymous$$s last error is because the server shutted down the second it started as the first warnings say. Anyone has any idea what could be causing t$$anonymous$$s? I have direct connection, my firewall turned off, and the script to check if a connection can be made as the docs say, it returned perfect.

T$$anonymous$$s is the code I'm using to create the server and later connect as a Client. (Note that I already tried to make 2 builds, one server and one client, to check if that was the problem and its the same):

 using UnityEngine;
 using System.Collections;
 
 public class _Server : MonoBehaviour {
     
     private int playerCount;
     
     // Server
     void Start(){
         PlayerPrefs.SetInt("ServerPort", 9999);
         LaunchServer();
     }
     
     public void LaunchServer() {
         bool useNat = !Network.HavePublicAddress();
         Network.InitializeServer(32, PlayerPrefs.GetInt("ServerPort"), useNat);
     }
     
     void OnServerInitialized() {
         Debug.Log("Server initialized and ready on port: " + PlayerPrefs.GetInt("ServerPort"));
         ConnectToServerFromLocal();
     }
     
     
     // Client
     void ConnectToServerFromLocal() {
         Network.Connect("127.0.0.1", PlayerPrefs.GetInt("ServerPort"));
     }
     
     void OnFailedToConnect(NetworkConnectionError error) {
         Debug.Log("Could not connect to server: " + error);
     }
     
     void OnDisconnectedFromServer(NetworkDisconnection info) {
         if (Network.isServer)
             Debug.Log("Local server connection disconnected");
         else
             if (info == NetworkDisconnection.LostConnection)
                 Debug.Log("Lost connection to the server");
             else
                 Debug.Log("Successfully diconnected from the server");
     }
 }

Thanks very much for taking the time to read my problem, it is much appreciated.

Comment
Add comment · Show 1
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 Benproductions1 · Nov 10, 2012 at 07:03 AM 1
Share

Are you trying to connect and host on the same client/version of the game. You can't host and connect with the same program. Try building the game and connect with the build, while host with the editor...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by giulio-pierucci · Mar 19, 2013 at 01:40 PM

I t$$anonymous$$nk you cannot call "ConnectToServerFromLocal" from "OnServerInitialized". the first is a "client side method" and the second is a "Server Side Event".

Maybe that "ConnectToServerFromLocal" close the server listen connections that you open from "LaunchServer".

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Players can't connect to my server 0 Answers

Help with multiplayer connection 0 Answers

My network server doesn't work. 0 Answers

How to implement a turn-based mobile game that allows the game to be closed until other player takes their turn? 1 Answer


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