Connecting to a Socket Server with a Unity3D client.

Hi guys!

First of all: Yes, I’m the typical newbie programmer trying to make an MMO. I know it’s super hard. I know I should make some games first (already did). Now we can move on :slight_smile:

I’d like to set up a C# server programmed in Visual Studio, and a Unity3D (C#) project as a client. I want to start really really small, in fact I already found exactly what I want for Visual Studio:

Asynchronous Server Socket Example: Use Sockets to send and receive data over TCP - .NET | Microsoft Learn

Asynchronous Client Socket Example: Use Sockets to send and receive data over TCP - .NET | Microsoft Learn

I need exactly that, with the difference that the client code should work in a Unity GameObject. I do understand there is a lot to read and learn, but the only way I can really learn something is to play with it and break it and fix it again and again and again.

Could you, please, help me transform the Client’s code to work from inside Unity?

Additional data: I did try Unity’s Networking HLAPI but it feels way too advanced for me. Following the tutorial I created a nice little game and I’m even able to modify it, but not knowing what’s happening behind the scenes makes it very hard for me to take the step by step, scalable aproach.

Thanks for reading!

What error you are getting?

There was one example in the forums for almost exactly same code…couldnt find it right now though,
this is quite similar,
http://wiki.unity3d.com/index.php/Simple_TCP/IP_Client_DLL_Code

You’ll might need this one also (to call unity methods from other thread)

but check also this library,

2 Likes

Important detail I forgot! Unity just freezes when I start the client with the server running.

Thanks for the links! I’m checking them at work and as soon as I get home I’ll give them a try!

probably that while (true) {} loop hangs it if you run that code as is

check this async example