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 /
avatar image
0
Question by gegonut · Feb 27, 2016 at 03:36 PM · c#unity5socketstcp

C# TCP server -- cannot call methods or set variables

I am working with C# in Unity 5. I ported some TCP code over from an app I built using .NET 4.5, based heavily on this:

Asynchronous Socket Communication

I am unable to call any methods upon receiving data in the server app.

I receive TCP communication on an event:

 public void OnReceivedData(IAsyncResult ar)
 {
     SocketChatClient client = (SocketChatClient)ar.AsyncState;
     byte[] aryRet = client.GetRecievedData(ar);
 
     string aryRetString = System.Text.Encoding.ASCII.GetString(aryRet);
     ParseCommand(aryRetString);
 
     if (aryRet.Length < 1)
     {
         client.Sock.Close();
         tcpClients.Remove(client);
         return;
     }
     client.SetupReceiveCallback(this);
 }

ParseCommand() is very simple (for testing):

 private void ParseCommand(string cmdToParse)
     {
         string[] splitInput = cmdToParse.Split(',');
         testValue++;
         Debug.Log(testValue.ToString());
 
         int playerStation = 0;
     }

When I use Debug.Log() to output testValue from within ParseCommand, it increments properly each time I call ParseCommand. However, if I continuously output testValue in Update(), it never increments. Also, if I call a method inside of ParseCommand, the method does not fire, and it behaves as though it has exceptioned although no exception is thrown -- i.e., the method stops on that line and the calling method will not fire again.

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
0

Answer by Bunny83 · Feb 29, 2016 at 03:35 AM

You are aware of the facts that:

  • OnReceivedData of an asynchronous Socket connection uses a seperate thread to invoke those callbacks.

  • Unity isn't threadsafe and you can't call any Unity API method from a different thread then the main thread, event with proper locking since Unity does a thread check inside most API methods which will throw an exception.

  • Exceptions in other threads might not be caught by Unity or that other thread catches the exception itself.

You also said:

I am unable to call any methods upon receiving data in the server app.

What happens in the server app is completely different from what happens inside Unity unless you use your Unity application as server. If you have two applications communicate through sockets (TCP or whatever) those two applications don't share anything. Neither variables nor methods. They live in two seperate worlds.

From the given information it's not possible to understand who is communicating with what and where you use that snippet of code actually.

If you want a better answer, you should add more details to your question. At the moment it can't be answered.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

TCP Socket waiting for ipnut to continiue reading data 0 Answers

tcp socket error message (C#) 1 Answer

NetworkTransport.Receive() on a single socket? 1 Answer


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