Tcp 我不能得到确切的答案
返回的答案太长了,但我会等到我看到我的线程睡眠时间有多长。我该怎么做才能看到他们?而不是线程Tcp 我不能得到确切的答案,tcp,asp.net-core-mvc,Tcp,Asp.net Core Mvc,返回的答案太长了,但我会等到我看到我的线程睡眠时间有多长。我该怎么做才能看到他们?而不是线程 if (NetworkInterface.GetIsNetworkAvailable()) { TcpClient tcpCli = new TcpClient(); bool connectionStatus = GetConnection(ipAddress, tcpPort, out tcpCli); NetworkStream stream
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客户端流,这充其量也有点奇怪。也许可以给您一些想法。
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";
}
}