troubleshooting Question

C# Loop getting slow

Avatar of VDulip
VDulip asked on
.NET ProgrammingC#
5 Comments1 Solution728 ViewsLast Modified:
I found one asterisk C# code. It is connecting server successfully and it giving responses. but until this loop ends it is becoming slow and stucking the program. so is there very good system to do this
Console.WriteLine("Quick and Dirty Asterisk Manager Daemon Test:\n"); 
 
  // Connect to the asterisk server. 
  Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
  IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.20"), 5038); 
  clientSocket.Connect(serverEndPoint);	
 
  // Login to the server; manager.conf needs to be setup with matching credentials. 
  clientSocket.Send(Encoding.ASCII.GetBytes("Action: Login\r\nUsername: mark\r\nSecret: mysecret\r\nActionID: 1\r\n\r\n")); 
 
  int bytesRead = 0; 
 
 
  do 
  { 
   byte[] buffer = new byte[1024]; 
   bytesRead = clientSocket.Receive(buffer); 
 
   //Console.WriteLine(bytesRead + " bytes from asterisk server."); 
 
   string response = Encoding.ASCII.GetString(buffer, 0, bytesRead); 
   Console.WriteLine(response); 
 
   if(Regex.Match(response, "Message: Authentication accepted", RegexOptions.IgnoreCase).Success) 
   { 
    // Send a ping request the asterisk server will send back a pong response. 
    clientSocket.Send(Encoding.ASCII.GetBytes("Action: Ping\r\nActionID: 2\r\n\r\n")); 
   } 
  }while(bytesRead != 0); 
 
  Console.WriteLine("Connection to server lost."); 
  Console.ReadLine();
ASKER CERTIFIED SOLUTION
Avodah
Serves the Most High - Not Religion

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros