Unable to read data to the transport connection: An existing connection was forcibly closed by the remote host
The search is either closed by the server or has some errors and there are a few ways to fix it but it still doesn’t work
When I call using Packet Sender, it’s still normal
Debug has an error in stream.ReadAsync, the rest is normal
[enter image description here](https://i.sstatic.net/GkcrL0QE.png)
// call TCP
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
byte[] ByteASCII_1 = Encoding.ASCII.GetBytes(AsciiPayload);
TcpClient tcpClient = new TcpClient();
await tcpClient.ConnectAsync(ZgwIP, TCP_DIAG_PORT);
NetworkStream stream = tcpClient.GetStream();
await stream.WriteAsync(ByteASCII_1, 0, ByteASCII_1.Length);
byte[] buffer = new byte[1048];
int countByte = 0;
string stringHex = string.Empty;
while (stream.CanRead)
{
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead <= 0)
{
break;
}
//string hexResponse = CryptoHelper.ByteArrayToHexString(buffer, bytesRead);
//stringHex += hexResponse;
//countByte += bytesRead;
//if (countByte >= 74)
//{
// break;
//}
}
RTB_Logs.AppendText($"n{stringHex}n");
tcpClient.Close();
stream.Close();
I tried with Packet Sender at the same time with that error and it worked without error, which means there is a problem with my code.
I added
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
but not successful
Quang Ngô Văn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.