I wrote a console application that creates a socket and listen for incoming packets on a specific port, extract them and run some sql commands for each packet. The packets are coming from over 400 devices (Data Loggers) over a private APN. so basically this program is a TCP listener, note that there are a lotta data coming, devices send data every 5 minutes plus any kinda event that needs to be sent, more than 135000 per day to be more precise.
My problem is that after a while the program crashes without any errors (program stopped working error!). This happens once or twice a month. now I wanna ask is there a way that I can reopen this console application after it crashes ?! or any other advice would be appreciated. I'm using windows server 2016
here's a bit of the code:
program.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Net;
using System.IO;
/*
* declaring some functions...
*/
// main method
static void Main(string[] args)
{
/*
* some date configuration and variable declaration codes...
*/
TcpListener serverSocket = new TcpListener(System.Net.IPAddress.Any, 9191);
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
Console.WriteLine(" >> " + "Server Started @ " + ymd_out[0].ToString() + "-" + ymd_out[1].ToString() + "-" +
ymd_out[2].ToString() + " " + m_hour + ":" + m_min + ":" + m_sec + " -> v4.4 Sakhaei 19.11.03");
/*
* database configuraion codes...
*/
while (true)
{
try
{
clientSocket = serverSocket.AcceptTcpClient();
ip_address = clientSocket.Client.RemoteEndPoint.ToString().Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).ToArray()[0];
/*
* some code to basically validate the sender...
*/
handleClinet client = new handleClinet();
client.startClient(clientSocket, 0, clientSocket.Client.RemoteEndPoint.ToString().Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).ToArray()[0], !false);
if (num_conn > 50)
{
Console.Clear();
num_conn = 0;
}
}
catch (Exception e)
{
/*
* closing the port, and again openning it whenever an exception happens
*/
clientSocket.Close();
serverSocket.Stop();
serverSocket = new TcpListener(System.Net.IPAddress.Any, 9191);
clientSocket = default(TcpClient);
serverSocket.Start();
}
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> " + "exit");
Console.ReadLine();
}
hanldeClient.cs
class handleClinet
{
TcpClient clientSocket;
private UInt32 crc_update(UInt32 crc, byte a)
{
/*
* some code...
*/
}
public void startClient(TcpClient inClientSocket, int _pm, string _caption, bool _def)
{
/*
* some boring code...
*/
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
bool crc_Check(UInt16 _low, UInt16 _high, int _size, byte[] _rab)
{
/*
* some other boring code...
*/
}
public static int get_detail_id_keyfi(string ip, string stmt)
{
MySqlConnection connection = null;
int output = 0;
try
{
/*
* database query
*/
}
catch (MySqlException ex)
{
Console.WriteLine("\n FAILED to establish connection: {0}", ex.ToString());
}
finally
{
connection.Close();
}
return output;
}
public static string update_keyfi(some parameters)
{
/*
* some intert into db queries
*/
}
public static int get_detail_id_ipFiltered(string ip, string stmt)
{
/*
* some code including a db query
*/
}
public static int get_type(int uid, string stmt)
{
/*
* some code including a db query
*/
}
public static string get_name(int uid, string stmt)
{
/*
* some code including a db query
*/
}
public static UInt16 check_pressure(int uid, float pressure, string stmt)
{
/*
* some code including a db query
*/
}
public static UInt16 check_deactive(int uid, string stmt)
{
/*
* some code including a db query
*/
}
public static UInt16 get_tur_chan(int uid, string stmt)
{
/*
* some code including a db query
*/
}
public static UInt16 get_prs_chan(int uid, string stmt)
{
/*
* some code including a db query
*/
}
public static UInt16 check_off(int uid, string stmt)
{
/*
* some code including a db query
*/
}
public static string send_to_db(a lotta parameters)
{
MySqlConnection connection = null;
string feedback = string.Empty;
try
{
/*
* something like 500 lines of code including some insert/update query
*/
}
catch (Exception ex)
{
Console.WriteLine("\n FAILED to establish connection, let alone executing sql commands: {0}", ex.ToString());
}
finally
{
connection.Close();
}
return feedback;
}
static int[] gregorian_to_jalali(int gy, int gm, int gd, int[] @out)
{
/*
* some code...
*/
}
private void doChat()
{
/*
* some code...
*/
while (clientSocket.Connected)
{
try
{
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, 2048);
UInt16 station = System.BitConverter.ToUInt16(bytesFrom, 0);
UInt16 function = System.BitConverter.ToUInt16(bytesFrom, 2);
/*
* some other code like the above lines...
*/
if (crc_Check(The parameters))
{
sendBytes = Encoding.ASCII.GetBytes("CRC_OK");
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
if (point_type > 0)
{
/*
* something like 150 lines of boring code
*/
networkStream.Dispose();
}
}
else
{
sendBytes = Encoding.ASCII.GetBytes("CRC_ERROR");
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
networkStream.Dispose();
Console.WriteLine(" >> " + ip_address + " : CRC Error ");
}
}
catch (Exception ex)
{
var st = new StackTrace(ex, true);
Console.WriteLine(this.CName + " >> " + ex.Message);
if (!clientSocket.Connected)
{
Console.WriteLine(" >> Client is disconected");
break;
}
Console.WriteLine(" >> " + ex.ToString());
Console.WriteLine(dataFromClient);
break;
}
}
clientSocket.Close();
}
}
thank you