连接丢失后,网络流不再有效

时间:2017-05-26 22:57:00

标签: c# xamarin.android tcpclient networkstream disconnection

我正在使用xamarin中连接到服务器的Android应用程序。服务器应用程序遵循基于客户端连接的进程等。但是,如果我说将设备转换为飞行模式,则将飞机模式从关闭状态转为关闭不再有效。基本上在现实世界中,问题是用户将使用Android设备,这可能会因为wifi干扰而导致连接短暂丢失等。我已经尝试了重新连接"技术(因为我注意到Tcp.Connected值仍然是真的)通过简单地尝试重新获得NetworkStream然而它不起作用。以下是代码:

    public NetworkStream NetworkStream;
    public TcpClient TcpClient;
    public string LastGetData = null;
    public string LastPostData = null;
    public bool Disconnected = true;

    public Stream InitializeStream(IPAddressAndPort ipObject)
    {
        NetworkStream clientStream = null;

        try
        {
            IPAddress address = IPAddress.Parse(ipObject.Address);

            TcpClient client = new TcpClient();

            client.SendTimeout = 3000;

            IPEndPoint serverEndPoint = new IPEndPoint(address, ipObject.Port);

            client.ConnectAsync(serverEndPoint.Address, serverEndPoint.Port).Wait(5000);

            if (client.Connected)
            {
                clientStream = client.GetStream();

                TcpClient = client;
                NetworkStream = clientStream;
                Disconnected = false;

                SendData("CLIENTSOFTWARENAME");
                string initReturnValue = ReadData();

                LastGetData = initReturnValue;
            }
        }
        catch (Exception ex)
        {
            LastGetData = ex.Message;
        }

        return clientStream;
    }

    public void AttemptReconnect()
    {
        if (TcpClient != null)
        {
            try
            {
                if (TcpClient.Connected)
                {
                    NetworkStream clientStream = TcpClient.GetStream();

                    NetworkStream = clientStream;
                    Disconnected = false;
                }
            }
            catch (Exception ex)
            {
                LastGetData = ex.Message;
            }
        }
    }

    public string RunCommand(string dataToSend)
    {
        string returnValue = string.Empty;
        try
        {
            if (Disconnected) AttemptReconnect();

            SendData(dataToSend);
            returnValue = ReadData();

            if (returnValue == string.Empty)
                Disconnected = true;
        }
        catch
        {
            returnValue = "An error occured during the action!  Please logout and try again.";
        }
        return returnValue;
    }

    public void SendData(string dataToSend)
    {
        if (NetworkStream != null)
        {
            var serverStream = NetworkStream;

            byte[] outStream = System.Text.Encoding.UTF8.GetBytes(dataToSend);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            LastPostData = dataToSend;
        }
    }

    public string ReadData()
    {
        if (NetworkStream != null)
        {
            string returnValue = string.Empty;
            byte[] bReads = new byte[1024];
            int readAmount = 0;
            DateTime endProcess = DateTime.Now.AddSeconds(20);

            while (returnValue == string.Empty && endProcess > DateTime.Now)
            {
                while (NetworkStream.DataAvailable)
                {
                    readAmount = NetworkStream.Read(bReads, 0, bReads.Length);
                    returnValue += string.Format("{0}", Encoding.UTF8.GetString(bReads, 0, readAmount));
                }
            }

            LastGetData = returnValue;

            return returnValue;
        }

        return null;
    }

1 个答案:

答案 0 :(得分:0)

解决:一旦NetworkStream或TCPClient出错,它就变得无用了。我们决定存储有关设备的信息,以便重新连接到现有进程并创建新的TCPClient和NetworkStream