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 oli_faeria · Dec 26, 2013 at 11:52 AM · sockettcpthread

Client TCP - State of the art

Hi,

I created a easy small TCP client in Unity.

During all the game this client will receive tcp command async.

Here is the function to receive the data :

 public void ReceiveData(IAsyncResult iar)
 {
     Socket remote = (Socket)iar.AsyncState;
     int recv = remote.EndReceive(iar);
     if (recv > 0)
     {
     treatOutput(System.Text.Encoding.ASCII.GetString(data, 0, recv));
     mySocket.BeginReceive(data,0,data.Length,0,new AsyncCallback(ReceiveData), mySocket);
     }
     else
     {
         Debug.Log("No more data - the socket should be closed");
     }
 }

My Question is related to the function treatOutput(myCommand) :

If I execute this command in the main thread of Unity and refresh the UI, it won't work ('cause you can not modify the UI outside the main thread)

So I pass the command via a static variable

 private static Queue qDataCommand ; 
 private void treatOutput (string myCommand)
 {
     qDataCommand.Enqueue(myCommand);
 }
 
 public static string getData()
 {
     if (qDataCommand.Count > 0) return (string)qDataCommand.Dequeue();
     else return "";
 }

And call getData() every frame in the UI :

 public class MyUI: MonoBehaviour {
 
     public MyUI() {}
 
     void Start () { 
     
     void Update () {
         string command = FrontController.getData() ;
         if (command != "")
         {
             Label myText = (Label)(GameObject.Find("myLabel")).GetComponent<Label>();
             myText.Text = command;
         }
     }
 }

Is there another way ? I feel it's extremely costly and not really nice.

Thanks in advance for any advice

Oli

Comment
Add comment · Show 4
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 oli_faeria · Dec 27, 2013 at 11:26 PM -1
Share

I don't know if my question is tricky, but it seems that people don't use async tcp connection.

If I ask my question differently :

When you check your socket do you make it directly or indireclty always via the update() method ?

avatar image Benproductions1 oli_faeria · Dec 27, 2013 at 11:26 PM 0
Share

Please post comments as CO$$anonymous$$$$anonymous$$ENTS NOT as answers!

avatar image Benproductions1 · Dec 27, 2013 at 11:31 PM 0
Share

On the third "script" you posted (which has nothing to do with the question) you shouldn't be using Find at runtime, only during initialization.

For asynchronous connections you should be using a callback function that does all the work for you (calls the appropriate functions etc)

avatar image oli_faeria · Dec 28, 2013 at 09:08 AM 0
Share

Thanks for your return.

And thanks for pointing out "the find" that has to be declared in the start function.

Well my question was not clear enough :

  1. I make the callback in the receiveData

  2. I add the data in a queue (I am in the callback thread)

  3. I read the queue from the update gameobject (and dequeue it). And it's obviously done in the main thread.

How can I directly modify the gameobject from the callback ?

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

19 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

Related Questions

Unity editor stalling on second attempt to play 0 Answers

TCP and Sockets in unity 0 Answers

Is it possible to control a game with an Android phone without Unity? 1 Answer

TCP responses are not receiving properly on client side. 1 Answer

Webplayer connecting to TCP server: No connection could be made because the target machine actively refused it 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