Asterisk manager Example: C Sharp

C# Example

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

namespace AsteriskPrototype
class AppConsole
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(""), 5038);

// 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;

byte[] buffer = new byte[1024];
bytesRead = clientSocket.Receive(buffer);

//Console.WriteLine(bytesRead + " bytes from asterisk server.");

string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);

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.");


Created by: flobi, Last modification: Mon 25 of Dec, 2006 (01:01 UTC) by ardaei
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+