- Home /
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 :) Anyway, thank you for your time!
Answer by hrgchris · Feb 24, 2017 at 12:26 PM
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: https://docs.unity3d.com/Manual/UNetUsingTransport.html
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
-Chris
Thank you! Tips like "open two sockets, an udp one and a tcp one" were the ones I was after, I didn't think about that at all. Yes, the main purpose of this project will be to kind of show what I've learned in my school years and how can I apply this knowledge to real world applications so I will probably make a very simple game (like, maybe, Battleship) and implement all of the controls and features. I think I'll try to design an AI and multiplayer. For that I'll use and manage myself the transport layer networking (and all of its problems!), maybe trying with C# sockets and async program$$anonymous$$g and, if I see it's too much work, switch to Unity's Network Transport Layer. Just for my personal enjoyment, if it all works right, I was thinking to make it VR with Google Cardboard, that would be probably a lot of fun. Anyway, thank you for your answer!
Your answer

Follow this Question
Related Questions
How to get string as a response of socket !? 0 Answers
How to set namespace in packet of socketIO using unity3d 1 Answer
Socket programming in unity 3 Answers
IOS socket connection to java socket server 0 Answers
Using mono socket with IPv6 and TCP 1 Answer