Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. C# TcpListener listen forever

C# TcpListener listen forever

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpsysadminasp-netdotnetjson
2 Posts 2 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Goga Work
    wrote on last edited by
    #1

    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!");
    
    P 1 Reply Last reply
    0
    • G Goga Work

      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!");
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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.

      Advanced TypeScript Programming Projects

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups