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 Vince_H · Dec 18, 2013 at 11:08 PM · networkrpcsocketondestroy

Unity not releasing ports correctly?

I am building a server with a client application. Everything seems to work, but seemingly at random, unity decides to NOT close a port on stopping the project in the editor or when running (build for windows).

Most of the times i have this problem once or 2ce (on first boot. If it correctly works the first time starting and stopping the editor project, it will work for the rest of the runs.)

However, this is starting to get on my nerves as switching ports on both applciations is a pain in the ass, and the final build should just work without this problem.

Here is my server application (Note that no client has to connect for this to happen):

     public class ServerScript : MonoBehaviour {    
 // Use this for initialization
 void Start () {
     instance = this;
     NetworkConnectionError error = Network.InitializeServer(500,26556,false);
     if(error != NetworkConnectionError.NoError){
         Debug.Log("Server couldn't run: " + error);
     }
     Debug.Log("[Server]Server started: "+GetIP());
 }
 #region connect/disconnect
 void OnPlayerConnected(NetworkPlayer user){
     Debug.Log("[Server]Connected: "+ user.externalIP);
 }
 
 void OnPlayerDisconnected(NetworkPlayer user){
     //remove this players RPC calls, he might have none but just make sure.
     Debug.Log("[Server]Disconnected: "+ user.externalIP);
     Network.RemoveRPCs(user);
     Network.DestroyPlayerObjects(user);
 }
 
     void OnDestroy(){
         Network.Disconnect();
     }
     #endregion
     string GetIP(){
         string strMachineName = Dns.GetHostName();
         IPHostEntry ipHost = Dns.GetHostByName(strMachineName);
         IPAddress IP = null;
         foreach(IPAddress ip in ipHost.AddressList){
             if(ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork){
                 IP = ip;    
             }
         }    
         return IP.ToString();
     }
     
     public void returnDataToUser(string JsonObject, NetworkPlayer user, LoLCall.CallType calltype){
         //make RPC call back to user to deliver data
         Debug.Log("[Server]Returning to "+user.externalIP+": " + JsonObject);
         foreach (NetworkPlayer player in Network.connections) { //make sure this user didn't bugger out in the mean time!
             if(player.Equals(user))networkView.RPC("DataResponse", user, Utils.GetBytes(JsonObject), calltype);
         }
     }
     
 
     #region RPC
     //the user calls this when he wants data, the cache will return data with an RPC back.
     [RPC]
     void DataRequest(string JsonCall, NetworkMessageInfo info){
         Debug.Log("[Server]User made a data request: " + JsonCall);
         //cache.makeRequest(JsonCall, info.sender);
     }
     
     [RPC]
     void DataResponse(byte[] JsonResult){//, string callType){
         //THIS IS A STUB TO MAKE RPC WORK!
     }
     #endregion
 }

And the errors are:

 Failed to initialize network interface. Is the listen port already in use?
 Server couldn't run: CreateSocketOrThreadFailure

Does anyone have any idea as to why this problem keeps popping up? In general there is not a lot of documentation regarding unity's Network.* part, i wish there was more troubleshooting on this subject.

Thanks in advance, Smiley

Comment
Add comment · Show 3
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 · Dec 18, 2013 at 11:31 PM 0
Share

I never had such a problem and i always used the editor as server. Since you start your server in "Start", are you sure you don't have this (stupid) ExecuteInEdit$$anonymous$$ode attribute on your class?

However it's usually good advice to use a port range. For example valve games use 27000 - 27015. That allows to run multiple servers on the same machine / IP. So when a port is in use, just use the next from your port pool.

I'm not sure how you manage your client connection but it seems you don't use the $$anonymous$$asterServer.

It's up to you how you handle this. As i said: i never had this problem. Are you sure the game quits "normally"?

avatar image Vince_H · Dec 18, 2013 at 11:45 PM 0
Share

I have never heard of ExecuteInEdit$$anonymous$$ode, fast read through says i will not need it often.

I will never require more then one server (it would kind of beat the purpose of what i am trying to achieve) and the same goes for not needing a master server. I just connect clients directly to this server the client can make a request, i will then check a cache or access a database, and give it a result to play with. Nothing fancy going on there.

The application ''quits'' by me pressing the Play button in top of unity. No exceptions or errors have been thrown in the entire runtime either.

Is there an easy example or way to make both server and client aware of the port pool? (i can change the server port, but the client still has to know where to connect to)

Thanks for the reply, Smiley

avatar image Bunny83 · Dec 19, 2013 at 12:51 AM 0
Share

Well, just define a "short" range (max 10-20 ports) and try them one by one. On the client, you should require some kind of server handshake on connect. So the client can react to a "timeout" and choose the next port.

3 Replies

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

Answer by Vince_H · Dec 28, 2013 at 05:04 PM

I have found that this error occurs whenever i fail to make connection with my SQL database directly. No idea how to tell unity to fix this though.

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
avatar image
0

Answer by sandbaydev · May 17, 2014 at 08:40 AM

Not a proper solution, but this helped temporarily for me: quit the Unity editor and restart Unity. After that, clicking play & server port should work OK.

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
avatar image
0

Answer by bugmagnet · Jul 06, 2014 at 11:25 PM

I found a solution for my case where I was having the same problem.

It turns out it was my Webroot Antivirus software interfering with my server port. I would be able to start a server, and close it. But once I tried to run it again it would fail with:

"Failed to initialize network interface. Is the listen port already in use?"

Make sure you add your application to your antivirus to tell it not to worry or scan about it. In WebRoot its Identity Protection Settings > Application Protection tab > + Add Application add your Unity application like that.

After adding my .exe to Webroot, my server starts perfectly. Hope this helps.

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Position synchronization problem in RPC Network System 0 Answers

RPC call not sending correct data 0 Answers

Empty RPC functions. 1 Answer

Make an animation play over RPC call? 1 Answer

How can i send a Player list to every Client 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