Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by halenknoll123 · Nov 23, 2015 at 12:36 PM · unity 5networking

Unity uNet Connection Problems

I have another window open running the same game so a server has be created and I know this because the registered sign has turned. After the client finds the HostList it connects to the first open server... But in this case it times out. Please Help.

using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.Networking.NetworkSystem; using UnityEngine.Networking;

public class network_manager : MonoBehaviour {

 string registeredGameName = "RockPaperScissorsOnline_randomOpponent";
 public static HostData[] hostData;
 int refreshRequestLength = 3;
 public GameObject connecting;
 public GameObject controller;
 public Text counter;
 public Text server;
 public Text creating;
 public Text created;
 public Text connecttext;
 mm_timer time;
 int timetext = 0;
 public static int i = 0;
 NetworkClient my_client;

 void Start()
 {
     server.text = ("");
     creating.text = ("");
     created.text = ("");
     connecttext.text = ("");
     time = new mm_timer(1f, 0f, true);
     controller.SetActive(false);
     connecting.SetActive(true);
     StartCoroutine("RefreshHostList");
 }

 void Connect()
 {
     string[] tmpip;
     int[] tmpport;
     tmpip = new string[hostData.Length];
     tmpport = new int[hostData.Length];
     int z = 0;

     my_client = new NetworkClient();

     foreach (HostData element in hostData)
     {
         while(z < element.ip.Length)
         {
             tmpport[z] = element.port;
             tmpip[z] = element.ip[z];
             z++;
         }
     }

    if (i == 0)
     {
         connecttext.text = ("Connecting to Server #" + 1);
         Debug.Log("" + tmpip[i]);
         Debug.Log("" + tmpport[i]);
         my_client.RegisterHandler(Inputs.inputs, Oninputs);
         my_client.RegisterHandler(Username.users, Onusername);
         my_client.Connect(tmpip[i], tmpport[i]);
     }
     else if (i > 0)
     {
         int j = i++;
         connecttext.text = ("Connecting to Server #" + j);
         Debug.Log("" + tmpip[i]);
         Debug.Log("" + tmpport[i]);
         my_client.RegisterHandler(Inputs.inputs, Oninputs);
         my_client.RegisterHandler(Username.users, Onusername);
         my_client.Connect(tmpip[i], tmpport[i]);
     }
 }

 public void OnConnected(NetworkMessage netMsg)
 {
     Debug.Log("Connected to server");
 }

 void OnFailedToConnect(NetworkConnectionError error)
 {
     i++;
     Connect();
     Debug.Log("Failed");
 }

 private void StartServer()
 {
     creating.text = ("Creating Server...");
     Network.InitializeServer(2, 25002, false);
     MasterServer.RegisterHost(registeredGameName, "Random Opponent", menu_keepusername.username);
     NetworkServer.RegisterHandler(Inputs.inputs, Oninputs);
     NetworkServer.RegisterHandler(Username.users, Onusername);
 }
 
 void OnMasterServerEvent(MasterServerEvent masterServerEvent)
 {
     if (masterServerEvent == MasterServerEvent.RegistrationSucceeded)
     {
         created.text = ("Registered Under Player " + menu_keepusername.username + ". Waiting for Other Player...");
     }
 }

 public IEnumerator RefreshHostList()
 {
     MasterServer.RequestHostList(registeredGameName);
     float timeEnd = Time.time + refreshRequestLength;

     while (Time.time < timeEnd)
     {
         hostData = MasterServer.PollHostList();
         yield return new WaitForEndOfFrame();
     }

     if (hostData == null || hostData.Length == 0)
     {
         StartServer();
     }
     else
     {
         Connect();
     }
 }

 void Update()
 {
     server.text = (hostData.Length + " Servers Found.");
     counter.text = ("seconds waited: " + timetext);
     if (time.returntik())
     {
         timetext += 1;
     }
     if (Network.isClient || Network.isServer)
     {
         if (Network.connections.Length > 0)
         {
             controller.SetActive(true);
             connecting.SetActive(false);
             SendUsername(menu_keepusername.username);
         }
     }
 }
Comment
Add comment
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Stun and turn server possible with Unity Netowking? 0 Answers

NetworkServer.Destroy(GameObject) disables Client objects instead of destroying them 1 Answer

Using SyncListString/Synchronizing lists 0 Answers

This is giving an error for the other client and cannot hit other client. 1 Answer

Animation over network using rpcs 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