Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Blink · Apr 30, 2015 at 05:01 PM · multiplayerserverhostconnect

Connecting to server through another computer (old network system Raknet)

http://www.paladinstudios.com/2013/07/10/how-to-create-an-online-multiplayer-game-with-unity/

This tutorial has shown me the basics of setting up a multiplayer game. I can run two instances of my game on my own computer that are able to connect to one another however when trying to connect to a server hosted by myself on a different computer they are unable to join. Can someone point me in the right direction in fixing this issue?

This is my script that controls the networking..

 using UnityEngine;
 using System.Collections;
 
 public class NetworkManager : MonoBehaviour {
 
     private const string typeName = "2468";
     private const string gameName = "Server1";
     
     private void StartServer()
     {
         Network.InitializeServer(4, 25000, !Network.HavePublicAddress());
         MasterServer.RegisterHost(typeName, gameName);
     }
 
     void OnGUI()
     {
         if (!Network.isClient && !Network.isServer)
         {
             if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server"))
                 StartServer();
         }
 
         if (!Network.isClient && !Network.isServer)
         {
             if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server"))
                 StartServer();
             
             if (GUI.Button(new Rect(100, 250, 250, 100), "Refresh Hosts"))
                 RefreshHostList();
             
             if (hostList != null)
             {
                 for (int i = 0; i < hostList.Length; i++)
                 {
                     if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList[i].gameName))
                         JoinServer(hostList[i]);
                 }
             }
         }
     }
 
     private HostData[] hostList;
     
     private void RefreshHostList()
     {
         MasterServer.RequestHostList(typeName);
     }
     
     void OnMasterServerEvent(MasterServerEvent msEvent)
     {
         if (msEvent == MasterServerEvent.HostListReceived)
             hostList = MasterServer.PollHostList();
     }
 
     private void JoinServer(HostData hostData)
     {
         Network.Connect(hostData);
     }
     
     public GameObject playerPrefab;
     
     void OnServerInitialized()
     {
         SpawnPlayer();
     }
     
     void OnConnectedToServer()
     {
         SpawnPlayer();
     }
     
     private void SpawnPlayer()
     {
         GameObject myPlayerGO = (GameObject)Network.Instantiate(playerPrefab, new Vector3(218f, 48f, 166f), Quaternion.identity, 0);
 
         ((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
         ((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
         ((MonoBehaviour)myPlayerGO.GetComponent("CharacterMotor")).enabled = true;
         myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
     }
 
 
 
 }

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 aclee · May 02, 2015 at 09:39 AM 0
Share

please add your code so we can help, without it we really don't know whats the issue.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by engdahl · Oct 02, 2017 at 08:56 AM

Juste do a a match if you have unity network

Comment
Add comment · Show 1 · 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
avatar image Bunny83 · Oct 02, 2017 at 10:10 AM 0
Share

This is

  • not an answer

  • a necro post for no reason (question date 2015).

  • a question about the old networking system (Raknet) which can still be used but is not supported on all platforms. Also Unity's development-$$anonymous$$asterServer has been switched off AFAI$$anonymous$$.

avatar image
0

Answer by Bunny83 · May 02, 2015 at 12:14 PM

Well, you use the MasterServer functionality to let the players join the server. If the server is behind a NAT router it depends on if the server's router "supports" NAT punchthrough. If not you can't join the server.

The only solution for such a player that wants to host a server is, to setup a port forwarding rule in his router according to your used port. So the player that hosts the server need to forward the port "2468" to his local pc so others from outside can join on this port.

I've once made a simply Network Example (webplayer) if you want to try if connecting works. (I started a server at the moment)

There's also another possibility. Some routers (like mine :/) allow NAT punchthrough, but don't allow a local loopback connection. That means you can talk to the whole internet and the internet can talk to you but you can not talk to yourself. So if you have two PCs in the same local network, they can't join each other when using the public router IP. Using local addresses of course works.

Whether NAT punchthrough is successful or not depends on both routers. Some combinations don't work. See the documentation over here.

What you can try is replacing your InitializeServer like with this one:

 Network.InitializeServer(4, 25000, true);

to force using NAT.

Also be sure you understand what a MasterServer is and how this all works. See my internet crash course over here.

Comment
Add comment · Show 2 · 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
avatar image Blink · May 05, 2015 at 06:26 PM 0
Share

Okay I think I understand, how do I find out if my router allows NAT punchthrough? And can there be a fix to this issue through Unity? Im trying to allow anyone using my game to create their own server for their friends and them to play on without doing anything more, is this possible or does it entirely depend on their router?

avatar image Bunny83 · May 05, 2015 at 09:57 PM 0
Share

@Blink: It depend entirely on their router (or on all routers in charge). The problem is that the "NAT technology" is actually a quick hack of existing structutes and they "misuse" the port number in TCP and UDP packets to identify a local user. There is no specification on which those routers where developed at the time. Actually those routers with the worst security are those which work best for NAT punchthrough ^^.

NAT punchthrough is actually a technology that tries to trick or cheat on the router to let a packet from the outside in.

As you can read in the documentation of RakNet (which i already linked in my answer) they write:

In the old days your users would have to go to their router configuration screen and setup a mapping. However, in modern applications users are not usually required to do this, thanks to NatPunchthrough.

Note the word "usually" in that statement ^^. If the hardware of your user doesn't allow NAT punchthrough (or he has a firewall or any other blocking service) there's nothing you can do about that except inform the user that it doesn't work and suggest he should open the appropriate port.

If you're behind a NAT router even "modern games" like $$anonymous$$ecraft requires you to open the right port in your router or nobody on the internet can join your server.

If you want provide a 100% connectability for your users, you have to host the servers on a public server yourself which isn't located behind a NAT router.

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

22 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 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

Network.Connect failed. 1 Answer

MasterServer.RegisterHost Issue 0 Answers

How can I transfer hosts once the current host leaves? 0 Answers

Multiplayer Server Networking- Player Server Hosting 2 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