• 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 minisurma · May 20, 2015 at 05:16 PM · javascriptclientconnectionthreadssocket

JavaScript TcpClient().Connect freezes

I'm making an MMORPG using Unity. I'm using the following system to connect to Java server.

 import System;
 import System.Threading;
 import System.Net;
 import System.Net.Sockets;
 
 private var WorkerThread : Thread;
 private var client : TcpClient;
 private var ns : NetworkStream;
 
 function Start () {
     WorkerThread = new Thread(ThreadListener);
     WorkerThread.Start();
 }
 
 private function ThreadListener(){
     client = new TcpClient();
     var port = 16304;
     
     try{
         print("running..");
         client.Connect(IPEndPoint(IPAddress.Parse("127.0.0.1"), port));
         
         print("Connected..");
         
         ns = client.GetStream();
         
          var data = new byte[1024];
          
          var waitCounter = 0;
          print("connection made");
          
          var read = 0;
          
          while(true){
              do{
                  try{
                      read = ns.Read(data, 0, data.Length);
                      var word = System.Text.Encoding.ASCII.GetString(data, 0, read);
                      Action(word);
                  }
                  catch(err){
                   print(err.Message);
                  }
              } while(ns.CanRead);
          }
     }      
     catch (InvOpEx : InvalidOperationException) {
         Debug.Log("TCP exception: " + InvOpEx.Message);
     }
           
     catch (SockEx : SocketException) {
         Debug.Log("Socket exception: " + SockEx.Message);
     }
     finally {
         if(ns != null)
               ns.Close();
         client.Close();
     }
 }

When I first run the game, it connects to server just fine and everything works as it should. When I then close the program and try to run it again, the whole Unity freezes. I'm sure the problem isn't on the server side (had to make a java coded client to make sure). If I then close the server when Unity is frozen, after a couple of seconds it just gives an error message saying that it couldn't connect to server. Any help is greatly appreciated.

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
1

Answer by YoungDeveloper · May 20, 2015 at 05:21 PM

I believe the WorkerThread will continue to work even if you close unity thread? Add OnDestroy and close the thread there, by setting some flag false, also add debugs to check do you even get to finally.

Comment
Add comment · Show 3 · 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 minisurma · May 20, 2015 at 06:41 PM 0
Share

OnDestroy didn't seem to work but I succeeded using OnApplicationQuit ins$$anonymous$$d. Thank you for your help.

avatar image YoungDeveloper · May 20, 2015 at 06:44 PM 1
Share

I wasn't sure the OnApplicationQuit works in editor. edit: i just tested OnDestroy, it works on editor stop, shouldn't be a problem.

avatar image minisurma · May 20, 2015 at 07:19 PM 0
Share

Okay, well I think I'll still stick to OnApplicationQuit. It also seems to work on editor too :)

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

20 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

Related Questions

How to stream audio from unity app to the web browser? 0 Answers

Performing "transform.Rotate" in it's own thread 1 Answer

Client connects to server, but Network.peerType returns Disconnected until next click 2 Answers

Is there a way to notify to the app once client has connected to server? 2 Answers

How to acquire an associative array from the 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