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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Networking (Not Multiplayer)

Discussion in 'Multiplayer' started by BrienKing, Aug 31, 2018.

  1. BrienKing

    BrienKing

    Joined:
    Oct 11, 2015
    Posts:
    35
    I need my game to act as a simple server and listen from commands over a specific port.

    Typically in a Windows app I would use the stuff built into .NET, but I have a feeling that wouldn't work as well in Unity.

    Basically I need the game to listen on Port X, and receive/send text.

    Some of the commands will be to change the scene being displayed, or to change a variable of the game.
    Would also like it to survive scene changes if possible, but if not it's easy to add a game object/script to each scene.

    Should I use the built in networking with Unity or should I just use the .NET networking?
     
  2. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    What kind of client is going to connect to this server? Like, do you need to support connecting via telnet, or are you also using another Unity app to connect? If using another Unity app then I'd probably use Unet/LLAPI. If it is a non-Unity client, then you might want to look at using a more native solution. YMMV.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The .NET socket class works just fine in Unity.

    The Unet networking API built into Unity is deprecated and its replacement has not yet been released. So use it as you would any deprecated API.

    There's plenty of 3rd party networking API's to choose from as well.
     
  4. BrienKing

    BrienKing

    Joined:
    Oct 11, 2015
    Posts:
    35
    It's a windows App, I've written that side already, pretty simple stuff. Basically similar to Telnet. Just wanting to make sure I'm not screwing up the game loop or blocking it from running properly. (i.e. frame drops, lag input, etc..).
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Not really possible to tell if your are doing any of that without the code you are using.
     
  6. BrienKing

    BrienKing

    Joined:
    Oct 11, 2015
    Posts:
    35
    So my solution, which works for my particular situation is to use TcpListener and I await an AcceptTcpClientAsync(). When I get a connection, I get the stream, and await a ReadAysync() to get the data. I then process what I need to, then await a WriteAsync...

    After that, I close the connection and wait for another connection.

    I start all this on a script that is attached to an Empty GameObject in the very first scene as this is preserved between scene loads.

    Code (CSharp):
    1.  
    2.     private static TcpListener m_tcpListener = null;
    3.  
    4.     // Use this for initialization
    5.     void Start()
    6.     {
    7.         if (m_tcpListener == null)
    8.         {
    9.             StartListener();
    10.         }
    11.     }
    12.  
    13.     private async void StartListener()
    14.     {
    15.         TcpClient tcpClient = null;
    16.         byte[] buffer = new byte[4096];
    17.         byte[] responseBuffer = null;
    18.         int byteCount;
    19.         string request;
    20.         string response = "OK!";
    21.  
    22.         m_tcpListener = new TcpListener(IPAddress.Any, 2688);
    23.         m_tcpListener.Start();
    24.  
    25.         while (true)
    26.         {
    27.             response = "OK!";
    28.  
    29.             tcpClient = await m_tcpListener.AcceptTcpClientAsync();
    30.  
    31.             using (var networkStream = tcpClient.GetStream())
    32.             {
    33.  
    34.                 byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);
    35.                 request = System.Text.Encoding.UTF8.GetString(buffer, 0, byteCount);
    36.  
    37.                 // Do processing in here...
    38.              
    39.                 responseBuffer = System.Text.Encoding.UTF8.GetBytes(response);
    40.                 await networkStream.WriteAsync(responseBuffer, 0, responseBuffer.Length);
    41.             }
    42.  
    43.             tcpClient.Close();
    44.         }
    45.     }