• 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 rodeowild · Apr 11, 2017 at 05:12 PM · networkingasynccallbacktcptcpclient

Unity TCP async functions

I'm trying to get unity to connect to a TCP server using asynchronous functions. The gist of the call is:

 NetworkStream stream = tcpClient.GetStream ();
 byte[] bytes = Encoding.ASCII.GetBytes ("1");
 stream.BeginWrite (bytes, 0, bytes.Length, new AsyncCallback(sendCallback), stream);

where tcpClient is, as one would expect, a TcpClient. The sendCallback looks like this:

 public void sendCallback(IAsyncResult ar)
 {
     NetworkStream stream = (NetworkStream)ar.AsyncState;
     stream.EndWrite (ar);
 }


The server does not receive the data until I close the connection. In other words, it doesn't receive the data unless I add (inside the callback) the line:

 tcpClient.Close ();

Obviously this is undesirable. If I use synchronous send calls everything works fine. am I doing something wrong or is this a unity bug? If this is a unity bug, I guess my other option would be to go with synchronous calls on a separate thread. Any gotchas to expect there?

Comment
Add comment · Show 2
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 rodeowild · Apr 10, 2017 at 08:36 PM 0
Share

Ok I figured it out - somewhat. It was a c# issue. Basically you need to work with the $$anonymous$$anualResetEvent class - see this page. As of now I am just following the example and it seems to work. However there are several things I am still unclear on when it comes to async functions:

1) Do they create new threads? 2) Can they handle concurrent requests safely? For example if you have called beginRead() and two large pieces of data come in at the same time, will I get a jumble of both? 3) What exactly is happening with $$anonymous$$anualResetEvents? Do they block the thread I am on (if indeed, I am implicitly multithreading here)?

I will ask these questions on stack overflow, as they pertain more to c# than to unity.

avatar image rodeowild · Apr 10, 2017 at 09:41 PM 0
Share

Ok I take back that last comment. That didn't fix it. I don't know what is happening.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Async execution with blocking methods 0 Answers

Unity tcp_client only working on localhost 1 Answer

What socket API will work correctly with Unity? 1 Answer

Asynchronous socket hangs intermittently on iOS 1 Answer

Streaming EEG data from Qstreamer to Unity 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