C# TcpListener listen forever
-
I am new to C# and .net core , I am building billing program to read data from PBX which send on defined TCP port. It does work , I receive the data but randomly tcp port stop listening and when I check window resource monitor I can see under Network --> Listening Ports --> it doesn't show . But window service is running, then after restart window service i can see port start listening again. There is no conflict of ports that I am sure of. My code logic is as below :
using CDR.Models;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;namespace CDR.Helpers
{
/// /// Active or Passive.
///
public class CDRServer
{
public static void Listen()
{
Logger.Log("Starting 3cX Listener");TcpListener server = null; try { // Set the TcpListener on port 5015. Int32 port = 5015; // IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); // if (!ipHostInfo.AddressList.Any(d => d.AddressFamily == AddressFamily.InterNetwork)) // return; //IPAddress ipAddress = ipHostInfo.AddressList.FirstOrDefault(d => d.AddressFamily == AddressFamily.InterNetwork);//IPAddress.Parse("127.0.0.1");// IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port); server = new TcpListener(ipAddress, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte\[\] bytes = new Byte\[1024\]; //this can be received. //INVESTIGATE //100KB String data = null; // Enter the listening loop. 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()}"); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); Logger.Log("Connected!");
-
I am new to C# and .net core , I am building billing program to read data from PBX which send on defined TCP port. It does work , I receive the data but randomly tcp port stop listening and when I check window resource monitor I can see under Network --> Listening Ports --> it doesn't show . But window service is running, then after restart window service i can see port start listening again. There is no conflict of ports that I am sure of. My code logic is as below :
using CDR.Models;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;namespace CDR.Helpers
{
/// /// Active or Passive.
///
public class CDRServer
{
public static void Listen()
{
Logger.Log("Starting 3cX Listener");TcpListener server = null; try { // Set the TcpListener on port 5015. Int32 port = 5015; // IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); // if (!ipHostInfo.AddressList.Any(d => d.AddressFamily == AddressFamily.InterNetwork)) // return; //IPAddress ipAddress = ipHostInfo.AddressList.FirstOrDefault(d => d.AddressFamily == AddressFamily.InterNetwork);//IPAddress.Parse("127.0.0.1");// IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port); server = new TcpListener(ipAddress, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte\[\] bytes = new Byte\[1024\]; //this can be received. //INVESTIGATE //100KB String data = null; // Enter the listening loop. 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()}"); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); Logger.Log("Connected!");
You may well have an unhandled exception killing your application. For instance, you use DateTime.Parse, but what happens if you don't receive a valid date? Use TryParse instead.