string DeviceTCPIP = "168.34.15.249";
string BackData;
string TextMessage;
int DevicePort = 5600;
void ReadCard()
{
this.threadListen = new Thread((ThreadStart)(() =>
{
while (true)
{
try
{
using (TcpClient tcpClient = new())
{
tcpClient.BeginConnect(DeviceTCPIP, DevicePort, null, null).AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));
TextMessage = "%DD001**CR";
byte[] bytes = Encoding.ASCII.GetBytes(TextMessage);
bytes[bytes.Length - 1] = (byte)13;
NetworkStream stream = tcpClient.GetStream();
stream.Write(bytes, 0, bytes.Length);
byte[] buffer = new byte[2048];
MemoryStream memoryStream = new();
tcpClient.Client.ReceiveTimeout = 500;
int count;
do
{
try
{
count = stream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
}
catch (IOException ex)
{
if (!(ex.InnerException is SocketException innerException) || innerException.ErrorCode != 10060)
throw ex;
count = 0;
}
}
while (count > 0);
BackData = Encoding.ASCII.GetString(memoryStream.ToArray());
if (BackData.Contains("match string"))
{
Console.WriteLine(BackData);
}
}
}
catch (Exception ex)
{
ErrorData = ex.Message.ToString();
}
Thread.Sleep(100);
}
}));
this.threadListen.Start();
}
I try read data from device for RFID number. But I receive following error. Also some of my tried I can receive RFID data correctly. However frequently throw following error. What can I do to fix the error?

There are many things wrong with your code.
BeginConnectwithout anEndConnect.Asyncwithawaityou wouldn't have had this issue in the first place, as it's much easier to write.WaitOneinstead of using the callback properly.UTF8instead ofASCII.streamneeds ausing.throw;notthrow ex;in acatchotherwise you wipe the stack trace.catch whencondition.10060IO exception on a TCP stream. You need to restart the connection.ToArrayon aMemoryStream, just useGetBufferto get the underlying buffer along with theLength.stream.CopyToAsync.CancellationToken.