【问题标题】:I can't get the answer exactly我无法得到确切的答案
【发布时间】:2019-07-01 11:08:32
【问题描述】:

返回答案太长,但我等到我看到我的 Thread.Sleep() 有多少。我应该怎么做才能看到它们?而不是 Thread.Sleep(100)

if (NetworkInterface.GetIsNetworkAvailable())
    {
        TcpClient tcpCli = new TcpClient();
        bool connectionStatus = GetConnection(ipAddress, tcpPort, out tcpCli);
        NetworkStream stream = null;
        if (connectionStatus == false)
            return "Could not establish TCP connection";
        else
        {
            try
            {
                //Send data to TCP Client
                Byte[] data = Encoding.ASCII.GetBytes(SendData);
                stream = tcpCli.GetStream();
                stream.Write(data, 0, data.Length);


                //Thread.Sleep(100);


                //Read data from TCP Client
                data = new Byte[tcpCli.ReceiveBufferSize];
                Int32 bytes = stream.Read(data, 0, data.Length);
                string answer = Encoding.ASCII.GetString(data, 0, bytes);

                if (answer.Contains("**"))
                    return answer;
                else
                    return "Panel no answer";
            }
            catch (Exception) { return "COMMUNICATION ERROR"; }
            finally { tcpCli.Close(); }
        }
    }

【问题讨论】:

  • 请多花一点时间来描述您的问题。目前尚不清楚您要在这里实现什么以及您的实际问题是什么。目前对我们来说最好的选择是,您尝试在一个进程中写入和读取 TCP 客户端流,这充其量似乎有点奇怪。
  • 也许Network Send and Reply可以给你一些想法。

标签: tcp asp.net-core-mvc


【解决方案1】:

我就是这样解决的。也许它可以帮助某人。

public static string TcpPanelGetSet(string ipAddress, int tcpPort, string SendData, string finishValue)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                TcpClient tcpCli = new TcpClient();
                bool connectionStatus = GetConnection(ipAddress, tcpPort, out tcpCli);
                NetworkStream stream = null;
                if (connectionStatus == false)
                    return "Could not establish TCP connection";
                else
                {
                    try
                    {
                        //Send data to TCP Client
                        Byte[] data = Encoding.ASCII.GetBytes(SendData);
                        stream = tcpCli.GetStream();
                        stream.Write(data, 0, data.Length);

                        //Read from TCP Client
                        string answer = "";
                        DateTime st = DateTime.Now;
                        DateTime et = DateTime.Now.AddSeconds(300);
                        do
                        {
                            st = DateTime.Now;
                            if (st > et)
                                return "TIME-OUT";
                            data = new Byte[tcpCli.ReceiveBufferSize];
                            Int32 bytes = stream.Read(data, 0, data.Length);
                            string tmpAnswer = Encoding.ASCII.GetString(data, 0, bytes);
                            if (tmpAnswer.Contains(finishValue))
                            {
                                answer += tmpAnswer;
                                break;
                            }
                            answer += tmpAnswer;
                        } while (et > st);


                        if (answer.Contains("**"))
                            return answer;
                        else
                            return "Panel no answer";
                    }
                    catch (Exception) { return "COMMUNICATION ERROR"; }
                    finally { tcpCli.Close(); }
                }
            }
            else
            {
                return "THERE IS NO CONNECTION";
            }
        }

【讨论】:

    猜你喜欢
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多