Using .NET Core, I wrote a program that communicates with the device through the socket and the device sends its requests. Everything works fine, but after a few minutes (from half an hour to two hours), the connection is disconnected. And it doesn't send any log.
I don't know what's wrong? I want this connection to be permanent. Do I need to make a special adjustment? Or should I make a change?
public async Task StartListening()
{
_logService.CreateLogCmd("StartListening get" + "ip&&port:" + ServerIP + " " + Port, "StartListening");
this.tcp = new TcpListener(IPAddress.Parse(ServerIP), Port);
this.tcp.Start();
_logService.CreateLogCmd("tcpStart" + "ip&&port:" + ServerIP + " " + Port, "StartListening");
Socket mySocket = null;
while (true)
{
mySocket = await tcp.AcceptSocketAsync();
_logService.CreateLogCmd("AcceptSocket" + "ip&&port:" + ServerIP + " " + Port, "StartListening");
mySocket.ReceiveBufferSize = MAXBUFFERSIZE;
mySocket.SendBufferSize = MAXBUFFERSIZE;
if (mySocket.Available <= 0)
continue;
byte[] bReceive = new byte[MAXBUFFERSIZE];
mySocket.Receive(bReceive);
Analysis(bReceive, mySocket);
}
}
This socket connection between the server and the device should remain stable forever and I don't know where the problem is