One of the first things I had to do, was figuring out how to connect to an external server via Unity client using a TCP/IP socket. It took a lot of searching to find help on this issue but I did find it eventually. I’ve added links to my files here so you can view/use them.
So to get your Unity application to connect to an external server via TCP/IP socket, you will need the following:
TCPConnection.cs – the connection handler, server communications
socketScript.cs – the script attached to one of the GameObjects in Unity (I used the Main Camera). We will use this script to attach a copy of TCPConnection.cs to the Camera as well.
I’ve added comments to the important things, hopefully they make sense.
I wrote my own very basic C++ windows socket server to test this. When interacting with server, make sure that your server returns the newline command after every response, otherwise your unity client will be left to hang (until the server disconnects). For our server we had to use \n but it depends on what type of server you are running.
If you want to have multiple connections (like for most games, you’ll have the login server and the game/world server), you just got to add another instance of TCPConnection.cs to your socketScript.
I wasn’t gonna bring over both posts separately, so this already includes the updates for multiple messages at once and using the split method to separate them. Also I’m now hosting these files here on this actual server instead of my synamicdsolutions URL.