I am new to C# and .net core. I am building a billing program where PBX send data as client on defined TCP port .
I created project in C# and it is working fine, means I can listen on port and get data from client.
But for some random reason port stop listening and I can confirm this from window server resource monitor Network --> Listening Ports.
I can also confirm there is port overlapping means no other software use that port.
But if I restart service it start to listen on port again.
I want port to never stop listen on that TCP port, and if for any reason it stop , it should automatically start to listen again.
Client software and billing program are installed on same PC.
What I have tried:
<pre lang="C#"><pre>
using CDR.Models;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace CDR.Helpers
{
public class CDRServer
{
public static void Listen()
{
Logger.Log("Starting 3cX Listener");
TcpListener server = null;
try
{
Int32 port = 5015;
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
server = new TcpListener(ipAddress, port);
server.Start();
Byte[] bytes = new Byte[1024];
String data = null;
while (true)
{
Console.WriteLine($"Waiting for a connection... on {ipAddress.ToString()} and {port.ToString()}");
Logger.Log($"Waiting for a connection... on {ipAddress.ToString()} and {port.ToString()}");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
Logger.Log("Connected!");
data = null;
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
Logger.Log($"Received: {data}");
try
{
CDRModel cdr = new CDRModel();
var datasplit = data.Split(',');
cdr.historyidofthecall = datasplit[0];
cdr.callid = datasplit[1];
cdr.duration = datasplit[2];
Logger.Log($"Before Date: {DateTime.Parse(datasplit[3]).ToString()}");
cdr.timestart = DateTime.Parse(datasplit[3]);
Logger.Log($"After Date: {cdr.timestart.ToString()}");
if (!string.IsNullOrEmpty(datasplit[4]))
{
cdr.timeanswered = DateTime.Parse(datasplit[4]);
}
cdr.timeend = DateTime.Parse(datasplit[5]);
cdr.reason_termination = datasplit[6];
cdr.from_no = datasplit[7];
cdr.to_no = datasplit[8];
cdr.from_dn = datasplit[9];
cdr.to_dn = datasplit[10];
cdr.dial_no = datasplit[11];
cdr.reason_changed = datasplit[12];
cdr.final_number = datasplit[13];
cdr.final_dn = datasplit[14];
cdr.from_type = datasplit[15];
cdr.missed_queue_calls = datasplit[16];
cdr.chain = datasplit[17];
Logger.Log(JsonConvert.SerializeObject(cdr));
if (!File.Exists("Db.txt"))
Console.WriteLine("Db.txt file not found");
var url = File.ReadAllText(@"Db.txt");
Logger.Log(url);
RestClient client2 = new RestClient(url);
RestRequest request = new RestRequest(Method.POST);
request.AddParameter("application/json", JsonConvert.SerializeObject(cdr), ParameterType.RequestBody);
var response = client2.Execute(request);
}
catch (Exception ex)
{
Logger.Log(ex);
}
}
client.Close();
Logger.Log("Client Closed");
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
Logger.Log(e);
}
finally
{
server.Stop();
Logger.Log("Server Stopped");
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
}