Example #1
0
    private void HandleClientComm(object client)
    {
        tcp_client = (TcpClient)client;
        NetworkStream client_stream = tcp_client.GetStream();


        byte[] message = new byte[2048];
        int    bytes_read;

        while (isTrue == true)
        {
            bytes_read = 0;

            try
            {
                //blocks until a client sends a message
                bytes_read = client_stream.Read(message, 0, 2048);
                //Debug.Log(message);
            }
            catch (Exception e)
            {
                //a socket error has occured
                Debug.Log(e.Message);
                break;
            }

            if (bytes_read == 0)
            {
                //client has disconnected
                Debug.Log("Disconnected");
                tcp_client.Close();
                break;
            }


            ASCIIEncoding encoder = new ASCIIEncoding();
            String        msg     = encoder.GetString(message, 0, bytes_read);
            //Debug.Log(msg);
            String[] nums = msg.Split('!');
            float    x, y, z;
            if (nums.Length == 3)
            {
                x = float.Parse(nums[0], CultureInfo.InvariantCulture.NumberFormat);
                y = float.Parse(nums[1], CultureInfo.InvariantCulture.NumberFormat);
                z = float.Parse(nums[2], CultureInfo.InvariantCulture.NumberFormat);
                Vector3 newPos = new Vector3(x, y, z);
                player.changePos(newPos);
            }
            else
            {
                tcp_client.GetStream().Close();
                tcp_client.Close();
            }
        }

        if (isTrue == false)
        {
            tcp_client.Close();
            Debug.Log("closing tcp client");
        }
    }