Upgrade 3CX to v18 and get it hosted free!

Asterisk manager Example: C Sharp

Author image

C# Example

using System;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Text;

namespace AsteriskPrototype
{
class AppConsole
{
[STAThread]
static void Main(string[] args)
{
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();
}
}
}

Back

Related Posts:

Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.