Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Is my method a reliable way to update player positions in multiplayer?

Discussion in 'Scripting' started by kennethcarrington2018, Aug 28, 2018.

  1. kennethcarrington2018

    kennethcarrington2018

    Joined:
    Apr 12, 2018
    Posts:
    88
    I created a console application on visual basic that acts as a server.
    When a client (on unity) sends data to the server (the console application) it goes in the format:

    username, data type, information

    An example would be:

    TestPlayer1/PositionUpdate/131,24,3

    This means that Testplayer1 has had a position update and has moved to coordinates: x:131, y:25, z:3.

    This data would then be sent back from the server to the clients and the game object representing TestPlayer1 would move to those coordinates on all the clients.

    Is this a reliable way to send real-time position data back and forth between the server and clients? If not what would be a better way?
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    It depends on what you are wanting to achieve.

    If you are doing this as a learning exercise to get a feel for networking and how things fundamentally hang together, then yes, it looks fine. It is nice and easy to read and, therefore, to see what is going on.

    If you are wanting a performant, reliable, fault-tolerant, serious network game connection, then you will almost certainly not want to be using nice, easy to read strings. Every single byte sent over a network connection is a big overhead to have. Also, if this is the case, then seriously consider using a package that is already available. You are entering into a minefield, wielding a heavy hammer that has a head which is only losely fastened to its handle - tread very carefully. :)
     
    Antypodish likes this.
  3. kennethcarrington2018

    kennethcarrington2018

    Joined:
    Apr 12, 2018
    Posts:
    88
    The strings will be encrypted in later versions but for now, I am just wanting to host a server that can maybe fit up to 8 players. Here is the script to the server so far:

    Code (CSharp):
    1. using System;
    2. using System.Net;
    3. using System.Net.Sockets;
    4. using System.Text;
    5.  
    6. namespace UnityServerTest1
    7. {
    8.     class Program
    9.     {
    10.         static void Main(string[] args)
    11.         {
    12.             int receivedDataLength;
    13.             byte[] data = new byte[1024];
    14.             IPEndPoint ip = new IPEndPoint(IPAddress.Any, 5555);
    15.             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    16.             socket.Bind(ip);
    17.             IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    18.             EndPoint Remote = (EndPoint)(sender);
    19.             while (true)
    20.             {
    21.                 data = new byte[1024];
    22.                 receivedDataLength = socket.ReceiveFrom(data, ref Remote);
    23.                 Console.WriteLine(Encoding.ASCII.GetString(data, 0, receivedDataLength));
    24.             }
    25.         }
    26.     }
    27. }
    And here is the code for the client:

    Code (CSharp):
    1. using System;
    2. using System.Net;
    3. using System.Net.Sockets;
    4. using System.Text;
    5. using UnityEngine;
    6.  
    7. public class ClientTest1 : MonoBehaviour {
    8.     //Set the player's username:
    9.     public string username;
    10.  
    11.     //Create the byte array:
    12.     byte[] data = new byte[1024];
    13.     //Data to send:
    14.     public string dataToSend;
    15.     //Create the network info:
    16.     IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5555);
    17.     Socket server = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    18.     //Create the chat GUI:
    19.     void OnGUI () {
    20.         username = GUI.TextField (new Rect (100, 100, 300, 50), username);
    21.         dataToSend = GUI.TextField (new Rect (100, 200, 300, 50), dataToSend);
    22.         if (GUI.Button (new Rect (100, 300, 100, 50), "Send")) {
    23.             if (dataToSend != "") {
    24.                 string totalData = username + "/Chat/" + dataToSend;
    25.                 Send (totalData);
    26.                 dataToSend = "";
    27.             }
    28.         }
    29.     }
    30.     //Send data to the server:
    31.     void Send (string inputText) {
    32.         data = Encoding.ASCII.GetBytes (inputText);
    33.         server.SendTo (data, data.Length, SocketFlags.None, ip);
    34.     }
    35.     //Receive data from the server:
    36.     void Receive () {
    37.         string receivedData = "";
    38.         if (receivedData != "") {
    39.             Debug.Log (receivedData);
    40.         }
    41.     }
    42.     //Close connection to server on exiting game:
    43.     void OnApplicationQuit () {
    44.         server.Close ();
    45.     }
    46. }
    I have not figured out how to send the data back to the clients so far.

    PS: I am doing this because I want to create my own multiplayer system and not have to rely on pre-made ones. Also, is there a way to limit the number of players that can go on this server?
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    What @Doug_B said. Plus, you may consider sending only player inputs, rather than position. And do on the client movement predictions. Very light way for network. Of course, depends on the requirement.
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    There's often no need to encrypt pure game data.
    It adds overhead on both sides, especially for the server, and it can also increase the number of bytes to send per network message, due to padding and the way most encryption algorithms work.
    Also, the server should have authority and validate the incoming data properly.

    You don't wanna deal with strings anyway when you take it to the next level, because strings require to many bytes...

    That'll be a long road.
    Be prepared to spend lots of time when you delve into the workd of networking (general + game specifics). It may take months or even years to code a proper networking solution, depending on experience, patience and the features you decide to go for.
     
    Doug_B likes this.
  6. kennethcarrington2018

    kennethcarrington2018

    Joined:
    Apr 12, 2018
    Posts:
    88
    I am willing to wait and take the time to do this.
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Consider using the Bitconverter class to convert your various number types, like floats, to byte arrays and sending those instead of string representations of the numbers. Also consider sending an identifier instead of stuff like "PositionUpdate" as a string. Doing those usually reduces the size of your packets, and makes reconstructing the data by the receiver more predictable because you'll use more fixed sizes. Otherwise you're going to end up doing a lot of string splitting or regular expressions to get your data out.

    I'm not sure what networking solution you are using, but they generally get down to byte arrays anyway, since the socket class only sends byte arrays.
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    From what you are saying, I got impression you got little, to none knowledge about networking. This leads me to think, you are biting problem from wrong direction, which will lead to over complicating where not necessary.

    It is nothing wrong about learning new stuff. Is really cool, as long you want get specialist in networking (in your case). But I think you may want focus instead on making actual game. Isn't it? Otherwise, if you not patient enough, it will lead you to exhaustion and eventually give up, over prolong period.

    May I ask, why you don't want to use existing solutions?
     
    Doug_B likes this.