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 /
  • Help Room /
avatar image
0
Question by Liderluigi · Apr 08, 2017 at 11:12 PM · errorserverclientthreadstcpclient

[HELP] Client slowly handling custom TCP server packets

Hi!

Introduction:

There have been few days since I had this problem and I have not been able to solve it:

I have a custom Java TCP multithreaded server that interacts with my Unity client. For example client sends "login user pass" packet and Server instantly (and I must mention this now) answers with an "ok" or "fail". There are no problems here.

My methods:

Previous "receive" code on Client.cs

 private void Update()
 {
     if (socketReady)
     {
         if (socket.GetStream().DataAvailable)  //the problem was this line
         {
             string recv = encryption(sr.ReadLine());
             if (recv != null)
                 HandleRecv(recv);
         }

     }
 }

HandleRecv() method that interacts with Unity Objects

 private bool HandleRecv(string recv)  //handles different packets received from server
 {
     string[] cmd = recv.Split(' ');
     switch (cmd[0])
     {
     
         case "fail":
             GameManager.Instance.BackButton(); //Here I change scene
             Debug.Log("Wrong credentials or logged in already!");
             break;

        // etc, i will just post this as example

     }

     return true;
 }

The problem:

The first code I used for Client was a lot simpler and seemed quicker. I could interact with server without any problems. However, once I created the "move" packets (which are a continuous fluctuation of packets almost instantly) I noticed that I could not receive 2 or more packets in a row because of the socket.getStream().DataAvailable line.

I deleted that line, ok. But then, as the Update() method is the main class thread, when the server didn't send any packets to the client, the game freezed waiting for one.

So I had to change the code: I removed Update() and created a Recv() thread to act the same way. As this thread was not the main one, the game did not freeze.

But what happened then? My HandleRecv() method checks the packet that has been received and then INTERACTS WITH UNITY OBJECTS (changes scene, creates game objects, etc.). As this new Recv() thread is not the main thread, I CANNOT INTERACT with those objects.

What can I do?

I have thought of multiple solutions, the one I thought for example (but doesnt work well): Using the Update() thread again and creating public variables that are accessed between the 2 threads but this SLOWS A LOT the packet handling! My player movement seems lagged...

Actual "receive" code on Client.cs (UGLY share of public variable between 2 threads, SLOWS A LOT the game, seems lagged)

 public string recv = null;
 public bool handled = true;
 

 public void Recv()  //Thread that reads line from server stream
 {
     if (socketReady)
         while (true)
         {
             if (handled)
             {
                 handled = false;
                 recv = encryption(sr.ReadLine());
             }
         }
 }

 private void Update()  //once public variable gets updated, handle it
 {
     if (socketReady)
         if (recv != null)
             if (HandleRecv(recv))
             {
                 handled = true;
                 recv = null;
             }
 }


I would really appreciate your help!! Thank you in advance!!

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

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

Related Questions

Spawning Dynamically Created Object (Network) 1 Answer

Failed to spawn server object, assetid=... 1 Answer

Need help spawning a gameObject on the server UNET 1 Answer

Destroy only player after disconnecting from server 0 Answers

Networking - Function runs on server but not on client? 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