Search Unity

How to Build a Network Layer in Unity

Discussion in 'General Discussion' started by alexddhuang, Aug 6, 2019.

  1. alexddhuang

    alexddhuang

    Joined:
    Jul 30, 2019
    Posts:
    20
  2. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    You shouldnt use tcp for networking in a game.
     
  3. alexddhuang

    alexddhuang

    Joined:
    Jul 30, 2019
    Posts:
    20
    I know UDP is better, but the new networking package (based on UDP) of Unity is still developing and unstable. Besides, my game will not have a heavy dependence on the speed of networking, so TCP is enough for me.
     
  4. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358

    We use the steam low level API, its also stand alone these days.

    https://github.com/ValveSoftware/GameNetworkingSockets
     
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Depends the game. Fast-paced games would typically use UDP while slow paced strategy games would use TCP.
     
  6. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Depends on the number of units etc.
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Certain types of games TCP is perfectly fine for, and it solves a lot of issues you have to handle yourself in UDP.
     
    Antypodish likes this.
  8. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    3,021
    While I personally strongly prefer UDP (because it is lightweight and flexible), I can see some cases where a game could use TCP. Obviously TCP would not be right for fast paced FPS games. TCP could work with games that are slower paced, though.

    The advantage of TCP is the developer does not need to worry about making sure each packet got there and in order. If you have a lot of stuff to send, you need it delivered in order, and you don't care about performance, then TCP may be an option.

    If performance matters more than ensuring packet delivery, use UDP. Also, developers have the option to implement their own lightweight packet ordering code and packet confirmation code into their network layers (if needed) with UDP.

    Here is a link to Sockets for anybody interested in building a network layer from scratch. The Socket class can use TCP or UDP.
    https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket?view=netframework-4.8
     
    alexddhuang and Joe-Censored like this.
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This is a good explanation. I prefer UDP generally, but it was a non-trivial amount of work to on top of UDP implement connections, optional reliability, message fragmentation, small message combining, packet ordering, and dropping duplicate packets. That's stuff you mostly get for free with TCP.
     
  10. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Library I linked above allready have reliable UDP impemented. Difference from TCP is that you can choose how to group your reliable channels, so that entities that do not relate do not need to wait for a resend.