Implementing a TCPListener to communicate with other programs

Hey there!

I’m currently trying to start a game project with one specific goal in mind: I’d like the game to communicate with an Android Smartphone via WLAN (not using unity on that). So basically the game sends some information to the smartphone which also sends messages to the game to manipulate the game world. Right now I’m looking into unity to see whether it’s possible to implement something like that efficiently or not.

I already found a pretty good source to let unity act as a TCPClient that sends messages to an external server and immediately gets a response: Unity Buster: Unity 3D Client/Server Socket Connection Sample Code

However, for my game it will be necessary to constantly wait for information and use it when it arrives - in other words the TCPListener part needs to be in unity, not in the other program. So I’d like to know if there’s some kind of typical approach for something like that. I’m not really sure what part should be done in a javascript and what part in a C# script. Neither do I know if using threads is necessary or a good idea in general. Any guidance would be appreciated since I couldn’t find any information about this.

Just to let you know: It seems to work with slight modifications to the example above. I simply started a TCPListener in the s_TCP.cs file on Start() and started a new Thread that waits for a connection via the AcceptTcpClient()-method. Once a connection is accepted a boolean is changed so that the setupSocket()-method can be called in the Update() method. I still don’t know whether this is acceptable in Unity or not, but so far it seems to work just fine. I can constantly exchange messages between my Smartphone and Unity and manipulate the game world that way.