Multiplayer with sockets

Hi guys! I’m i’m going to graduate this year and I’ll have to make a graduation project so I was thinking to use unity, which I already more-or-less know (I have already made some games). This will be a two-players multiplayer game, and I was wondering how could I make the networking part work. Since this is a school project I’d rather not use networking tools which Unity provides, I would prefer to write myself lower level networking functions (maybe with sockets). I will have to sync positions of game objects in real-time (like one would do in a online fps). Should I use UDP? On which interval should I sync the objects’ position? And how should I design the message passed between the clients?
I’m completely new to networking programming (I only made a chat program with c# and TCP sockets, just for testing), so if you could answer those questions or if you had some advice about multiplayer programming patterns I would be very grateful :slight_smile:
Anyway, thank you for your time!

Hi Henry

There’s quite a bit of detail there. To answer a few bits:

  • UDP will give you quite a few headaches, as it doesn’t guarantee a message will arrive, or in what order messages will arrive. You will likely have some essential messages, and if relying solely on UDP you would have to implement reliable message schemes which are pretty tricky
  • TCP unfortunately can create a lot of latency. It can be a helpful way to get started, however for real time syncing of object positions it probably won’t end up suitable
  • It’s not unusual (especially in prototypes) to open 2 sockets - 1 TCP and 1 UDP, then use TCP for critical, not time dependent messages, and UDP for ones that you can afford to lose!

However! Given this is a graduation project in Unity, and you want to give yourself some degree of lower-level networking challenge, can I suggest you go half way and use Unity’s Network Transport Layer:

It’s a very low level wrapper around Unity’s internal network code. The high level UNet stuff uses the transport layer for communication, but you can access it too without using UNet.

You’d still need to properly handle network messages, think about reliable/unreliable messaging, message formats, timings, bandwidth and synchronise objects and stuff yourself. You just wouldn’t need to deal with the very low level issues of opening a socket, reliable queues or async network events (which I can promise from past experience are serious time sinks - you could probably do an entire thesis just on that!).

For message formats, look into the BitConverter in .net for converting values to/from byte arrays to be sent over the network.

Hope that helps