Search Unity

Timeout causes game to freeze.

Discussion in 'Multiplayer' started by evan_ohara, Dec 6, 2016.

  1. evan_ohara

    evan_ohara

    Joined:
    Oct 28, 2016
    Posts:
    35
    My unity client has a menu with a "Login" button. Above that button I would like to keep the user informed as to what is going on. When he clicks the login button the status text says "Logging in...".

    If the server software is not running the error I get is something like "connection actively refused". Makes sense, the port isn't open, so with that error (code 10061), the client knows the server is down and can update the status text accordingly.

    I don't know all of the intricacies of networking, but I want to plan for what happens in the case of a request being timed out. I changed the IP used to access the server to something random that I couldn't get a response from while pinging. This time when I try, the game freezes for 20 seconds while it waits for a response.

    The connection is setup with the following:
    Code (CSharp):
    1.  
    2.  
    3.     private static string serverAddress = "222.212.222.222";
    4.     private static int serverPort = 32211;
    5.  
    6.     private TcpClient _client;
    7.     private NetworkStream _stream;
    8.     private Thread _thread;
    9.  
    10.     bool isConnected = false;
    11.  
    12.     void Awake(){
    13.         SetupConnection();
    14.     }
    15.     public void SetupConnection() {
    16.         try {
    17.             _thread = new Thread(ReceiveData);
    18.             _thread.IsBackground = true;
    19.             _client = new TcpClient(serverAddress, serverPort);
    20.             _stream = _client.GetStream();
    21.             _thread.Start();
    22.             isConnected = true;
    23.         }
    24.         catch (SocketException e){
    25.             CloseConnection();
    26.         }
    27.     }
    28.  
    29.     private void ReceiveData(){
    30.         if (!isConnected){
    31.             return;
    32.         }
    33.  
    34.         int numberOfBytesRead = 0;
    35.         while (isConnected && _stream.CanRead)
    36.             try{
    37.                 numberOfBytesRead = _stream.Read(_buffer, 0, _buffer.Length);
    38.                 receiveMsg = Encoding.ASCII.GetString(_buffer, 0, numberOfBytesRead);
    39.                 _stream.Flush();
    40.                 HandlePacket(receiveMsg);
    41.                 receiveMsg = "";
    42.             }
    43.             catch (Exception e){
    44.                 CloseConnection();
    45.             }
    46.     }
    47.  
    48.     private void CloseConnection(){
    49.         if (isConnected) {
    50.             Debug.Log("Connection closed.");
    51.             _thread.Interrupt();
    52.             _stream.Close();
    53.             _client.Close();
    54.             isConnected = false;
    55.             receiveMsg = "";
    56.         }
    57.     }
    58.  
    I've tried launching the whole thing in a coroutine to no avail.

    Thanks!
     
    Last edited: Dec 6, 2016