Click here to Skip to main content
15,867,308 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: Can not create object of DirectorySearcher class with Visual C++ with project reference of System.DirectoryServices.dll Pin
Alex Banar1-Apr-22 4:42
Alex Banar1-Apr-22 4:42 
GeneralRe: Can not create object of DirectorySearcher class with Visual C++ with project reference of System.DirectoryServices.dll Pin
Richard MacCutchan1-Apr-22 5:44
mveRichard MacCutchan1-Apr-22 5:44 
GeneralRe: Can not create object of DirectorySearcher class with Visual C++ with project reference of System.DirectoryServices.dll Pin
Alex Banar1-Apr-22 6:22
Alex Banar1-Apr-22 6:22 
GeneralRe: Can not create object of DirectorySearcher class with Visual C++ with project reference of System.DirectoryServices.dll Pin
Richard MacCutchan1-Apr-22 6:28
mveRichard MacCutchan1-Apr-22 6:28 
GeneralRe: Can not create object of DirectorySearcher class with Visual C++ with project reference of System.DirectoryServices.dll Pin
Alex Banar1-Apr-22 6:49
Alex Banar1-Apr-22 6:49 
Questionrunning Powershell from ASP.NET Core 6 Pin
Johannes B. Latzel18-Mar-22 2:20
Johannes B. Latzel18-Mar-22 2:20 
AnswerRe: running Powershell from ASP.NET Core 6 Pin
Dave Kreskowiak27-Mar-22 18:17
mveDave Kreskowiak27-Mar-22 18:17 
QuestionC# TcpListener listen forever Pin
Goga Work11-Mar-22 18:42
Goga Work11-Mar-22 18:42 
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 :

C#
using CDR.Models;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace CDR.Helpers
{
    /// <summary>
    /// Active or Passive.
    /// </summary>
    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!");

                    data = null;

                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();

                    int i;

                    // Loop to receive all the data sent by the client.
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        // Translate data bytes to a ASCII string.
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

                        Console.WriteLine("Received: {0}", data);

                        //process the received call data and then save it to the database. 
                        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]);//.Zone().AddHours(-3);
                            Logger.Log($"After Date: {cdr.timestart.ToString()}");

                            if (!string.IsNullOrEmpty(datasplit[4]))
                            {
                                cdr.timeanswered = DateTime.Parse(datasplit[4]);//.Zone().AddHours(-3);
                            }
                            cdr.timeend = DateTime.Parse(datasplit[5]);//.Zone().AddHours(-3);

                            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];
                            //cDR.Chain = dataSplit[19];

                            // Process the data sent by the client add to the database.
                            //using (var context = new MySqlContext())
                            //{
                            //    context.CDRs.Add(cDR);
                            //    context.SaveChanges();
                            //}

                            //Post object to external server. 
                            Logger.Log(JsonConvert.SerializeObject(cdr));

                            //var url = Environment.GetEnvironmentVariable("cdrlink");
                            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);
                            // 192.168.1.234/postdata3cx/data3cx.php
                            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);
                        }


                        //byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                        // Send back a response.
                        //stream.Write(msg, 0, msg.Length);
                        //Console.WriteLine("Sent: {0}", data);
                    }

                    // Shutdown and end connection
                    client.Close();
                    Logger.Log("Client Closed");

                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
                Logger.Log(e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
                Logger.Log("Server Stopped");
            }


            Console.WriteLine("\nHit enter to continue...");
            Console.Read();

        }

    }

}

AnswerRe: C# TcpListener listen forever Pin
Pete O'Hanlon11-Mar-22 20:57
subeditorPete O'Hanlon11-Mar-22 20:57 
QuestionPower BI Embedded : Cant doCross Filtering selecting multiple rows on a table visual pressing CTRL Pin
Member 155547063-Mar-22 17:19
Member 155547063-Mar-22 17:19 
Questionhow it works Pin
Calin Negru17-Feb-22 8:48
Calin Negru17-Feb-22 8:48 
QuestionRe: how it works Pin
Calin Negru17-Feb-22 10:41
Calin Negru17-Feb-22 10:41 
AnswerRe: how it works Pin
jschell17-Feb-22 10:44
jschell17-Feb-22 10:44 
AnswerRe: how it works Pin
Richard MacCutchan17-Feb-22 21:35
mveRichard MacCutchan17-Feb-22 21:35 
GeneralRe: how it works Pin
Calin Negru18-Feb-22 2:17
Calin Negru18-Feb-22 2:17 
QuestionPlease help with dynamic objects modeling Blazor/MVC Pin
Guillermo Perez28-Jan-22 13:53
Guillermo Perez28-Jan-22 13:53 
AnswerRe: Please help with dynamic objects modeling Blazor/MVC Pin
Richard MacCutchan30-Jan-22 6:17
mveRichard MacCutchan30-Jan-22 6:17 
AnswerRe: Please help with dynamic objects modeling Blazor/MVC Pin
Richard Deeming30-Jan-22 21:48
mveRichard Deeming30-Jan-22 21:48 
GeneralRe: Please help with dynamic objects modeling Blazor/MVC Pin
Guillermo Perez31-Jan-22 11:01
Guillermo Perez31-Jan-22 11:01 
Question<pre>I Have an application that shows output of cpu utilization its memory used and current date and time in console application output now i want to store this 3 things in my database how can i store it?</pre> Pin
Member 154919019-Jan-22 22:06
Member 154919019-Jan-22 22:06 
AnswerRe: <pre>I Have an application that shows output of cpu utilization its memory used and current date and time in console application output now i want to store this 3 things in my database how can i store it?</pre> Pin
dan!sh 9-Jan-22 22:42
professional dan!sh 9-Jan-22 22:42 
AnswerRe: <pre>I Have an application that shows output of cpu utilization its memory used and current date and time in console application output now i want to store this 3 things in my database how can i store it?</pre> Pin
jschell17-Feb-22 10:46
jschell17-Feb-22 10:46 
QuestionIncorporating values directly (as is/bytewise) into a string Pin
primem0ver28-Dec-21 10:19
primem0ver28-Dec-21 10:19 
AnswerRe: Incorporating values directly (as is/bytewise) into a string Pin
primem0ver28-Dec-21 20:07
primem0ver28-Dec-21 20:07 
AnswerRe: Incorporating values directly (as is/bytewise) into a string Pin
trønderen29-Dec-21 1:21
trønderen29-Dec-21 1:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.