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
1
Question by Anonymous-User524 · Sep 05, 2013 at 01:32 PM · serverconnection

Networking Issue: Cannot connect to server

Overview

I am trying to make a multi-player game and wish to connect the Client to the Server. However, even though I am not getting any errors when coding, the Client is unable to connect to the Server and returns this error "The connection request to [IP ADDRESS] failed. Are you sure the server can be connected to?"

I have tried looking up many different threads with the same problem but I still cannot find the answer to the issue.


Code for Server

 //Variable(s)
 var IP : String = "42.60.8.21";
 var port : int = 28000;
 var maxConnections : int = 10;
 
 //Function(s)
 function OnGUI() {
 
 if (Network.peerType == NetworkPeerType.Disconnected) {
 
 if (GUILayout.Button("Initialise Server")) {
 Network.InitializeServer(maxConnections, port, false);
 Debug.Log("Server Initialised");
 }
 
 if (GUILayout.Button("Quit")) {
 Application.Quit();
 }
 
 
 } else {
 if (GUILayout.Button("Terminate Server")) {
 Network.Disconnect();
 }
 
 GUILayout.Label("Current Connections: " + Network.connections.Length);
 
 }
 
 }
 
 function OnPlayerDisconnected(client : NetworkPlayer) {
 
 Debug.Log("Disconnecting Clients");
 Network.RemoveRPCs(client);
 
 }
 
 function OnDisconnectedFromServer() {
 
 Debug.Log("Server Terminated");
 
 }


Code for Client

 //Variable(s)
 var ServerIP : String = "42.60.8.21";
 var ServerPort : int = 28000;
 var myIP : String = "42.60.8.21";
 var myPort : int = 28001;
 
 //Function(s)
 function OnGUI() {
 
 if(Network.peerType == NetworkPeerType.Disconnected) {
 
 if (GUILayout.Button("Quit")) {
 Application.Quit();
 }
 
 if (GUILayout.Button("Start")) {
 Network.Connect(ServerIP, ServerPort);
 }
 
 } else {
 if (GUILayout.Button("Disconnect")) {
 Network.Disconnect();
 }
 
 }
 
 }
 
 function OnConnectedToServer() {
 
 print("Connected");
 
 }


Thanks in advance!

I have been stuck at this for weeks and I cannot seem to figure out a solution it. Any help would be greatly aprreciated!

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

1 Reply

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

Answer by EssyTech · Sep 05, 2013 at 04:06 PM

In order for a connection to work between your server and client the following must happen:

  1. If your network is on a router then you need to set a port foward to the local ip address of the machine the server is running on. ex. 192.168.0.3 ports 28000 - 29000; (I found out that some unity games need more than 1 port forwarded to work, so I just port forward a series of ports to be sure.)

  2. You can find the local ip of machine by cmd and Ipconfig.

  3. If your client and server our on the same machine (for testing) you can use 127.0.0.1.

  4. Make sure your firewall allows your program to communicate through the port or ports you are communicating through.

  5. Make sure you use RPC and the networkview classes correctly to communicate the information between server and client.

  6. Make sure your IP address hasn't change. google "What is my IP" and it should quickly give you it.

Comment
Add comment · Show 5 · 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 Anonymous-User524 · Sep 06, 2013 at 01:12 PM 0
Share

THAN$$anonymous$$ YOU SOOOOO $$anonymous$$UCH! IT WOR$$anonymous$$ED!!!!! :D

avatar image EssyTech · Sep 08, 2013 at 04:16 AM 0
Share

Np. Glad I could help :)

avatar image Unity Ninja · Jul 13, 2014 at 08:08 AM 0
Share

What worked? Not working for me... What did you mean in your 3rd point. Where do I use it?

avatar image BlackPanda · Feb 05, 2015 at 05:21 PM 0
Share

How to set a port forward to the local IP address??

avatar image jaxx0rr · Oct 01, 2016 at 05:18 PM 1
Share

ALSO make sure if you test on ur local machine (using 2 instances) that the server instance has "run in background" checked in build settings

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

16 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

Related Questions

connection request failed 1 Answer

How connect client to server when ip is private ? 2 Answers

Master / Client - When to close connection ? 1 Answer

Online chat in unity 1 Answer

Unity Network make Server and Connect 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