info@voip-sip-sdk.com Tel: 00 36 52 532 731

C# SIP registration example

Download: SIP_Register.zip

This example demonstrates how to implement the sip register method in c#. The SIP REGISTER method is the first method you will call, when you create a voip softphone. To use this example, you need to have Ozeki VoIP SIP SDK installed, and a reference to ozeki voipsipsdk.dll should be added to your visual studio project.


Figure 1 - SIP registration dialog

What is a SIP REGISTER method? When is it needed?

When you build a C# sip client - for example a C# softphone -, the first task you will perform will be connecting it to a VoIP PBX system. This task is called registration. In other words you register the SIP client to the VoIP PBX. The purpose of the SIP registration is to authenticate the SIP client and to tell the VoIP PBX the IP address of the SIP client.

When you register the softphone to the voip PBX, the softphone (sip client) will send a SIP REGISTER PDU over the network in an UDP packet to the PBX. The PBX will respond to this request with a response UDP packet. In this response UDP packet, authentication data is included. The next step is performed by the softphone to send the SIP REGISTER PDU again, but this time it will include the username and password manipulated by the authentication data received from the PBX. If the authentication procedure is successful, the PBX will reply with an OK (Figure 1).

How to register a SIP account using C#?

To perform the SIP registration task in C# using the Ozeki VoIP SIP SDK, you need to use the ISoftphone and IPhoneLine interfaces. First, you need to create a softphone, and a phone line, then you need to specify the SIP account to be used for the phone line. You can learn how to create an account and what configurations are needed to connect to a PBX, at the Supported VoIP PBXs chapter, and you can also learn how to connect to VoIP service providers. The final step is to call the RegisterPhoneLine method (see example). The regsitration procedure will happen asynchronously in the background, and your program will be notified about the success or failure via the mySoftphone_PhoneLineStateChanged callback method.

SIP register example in C#

using System;
using Ozeki.VoIP;
using Ozeki.VoIP.SDK;

namespace SIP_Register
{
    class Program
    {
        private static ISoftPhone softphone;   // softphone object
        private static IPhoneLine phoneLine;   // phoneline object

        private static void Main(string[] args)
        {
            // Create a softphone object with RTP port range 5000-10000
            softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);

            // SIP account registration data, (supplied by your VoIP service provider)
            var registrationRequired = true;
            var userName = "sipusername";
            var displayName = "sipdisplayname";
            var authenticationId = "authenticationid";
            var registerPassword = "Password";
            var domainHost = "pbxip.voipprovider.com";
            var domainPort = 5060;

            var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);

            // Send SIP regitration request
            RegisterAccount(account);

            // Prevents the termination of the application
            Console.ReadLine();
        }

        static void RegisterAccount(SIPAccount account)
        {
            try
            {
                phoneLine = softphone.CreatePhoneLine(account);
                phoneLine.RegistrationStateChanged += sipAccount_RegStateChanged;
                softphone.RegisterPhoneLine(phoneLine);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error during SIP registration: " + ex);
            }
        }

        static void sipAccount_RegStateChanged(object sender, RegistrationStateChangedArgs e)
        {
            if (e.State == RegState.Error || e.State == RegState.NotRegistered)
                Console.WriteLine("Registration failed!");

            if (e.State == RegState.RegistrationSucceeded)
                Console.WriteLine("Registration succeeded - Online!");
        }
    }
}

Training guides, simple examples

If you would like to visit the detailed developer documentations, you can learn much more from the Ozeki VoIP SIP SDK Training chapter.

SIP REGISTER PDU sent over the network

The SIP registration starts by sending a registration request to the PBX from. Then the PBX will ask for the authentication information. The softphone will provide its authentication information e.g.: authenticationid, sipusername, sipdisplayname and Password. Then it will receive a confirmation of the successfull registration from the PBX.

You can see the PDUs responsible for these below:

Step 1: Registration request (UDP message, Softphone -> PBX)

Request-Line: REGISTER sip:192.168.113.175 SIP/2.0 
Via: SIP/2.0/UDP 192.168.115.181:20041;branch=z9hG4bK1f46f504-022a-4b80-89fa-
e743407f5926;rport 
To: "300"sip:300@192.168.113.175 
From: "300"<sip:300@192.168.113.175>tag=hkxlofod 
CSeq: 7 REGISTER 
Call-ID: vwenplkrqdxvaekqbbwjgqxrgenxdlntenvdxgluhmuflcqaet 
Max-Forwards: 70 
Contact: sip:300@192.168.115.181:20041;rinstance=11372d3a07ebbc47 
User-Agent: Ozeki VoIP SIP SDK v10.1.7 
Content-Length: 0 
Expires: 3600 
Supported: 100rel 
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, SUBSCRIBE, NOTIFY, REFER, INFO, MESSAGE

Step 2: Reply from PBX with request for authentication (UDP message, PBX -> softphone)

Status-Line: SIP/2.0 407 Proxy Authentication Required 
Via: SIP/2.0/UDP 192.168.115.181:20041;branch=z9hG4bK1f46f504-022a-4b80-89fa-
e743407f5926;rport=20041;
received=192.168.115.181 
From: "300"<sip:300@192.168.113.175>tag=hkxlofod 
Call-ID: vwenplkrqdxvaekqbbwjgqxrgenxdlntenvdxgluhmuflcqaet 
CSeq: 7 REGISTER 
To: "300"<sip:300@192.168.113.175>tag=belglckn
User-Agent: Ozeki Phone System XE v5.2.1
Content-Length: 0
Proxy-Authenticate:Digest nonce="1f9c065c0bd24a4992f3dcca91de99e8", realm="OzekiPBX", 
algorithm=MD5

Step 3: Sending the authentication information for registration (UDP message, Softphone -> PBX)

Request-Line: REGISTER sip:192.168.113.175 SIP/2.0 
Via: SIP/2.0/UDP 192.168.115.181:20041;branch=z9hG4bK68c7248e-bc7d-4d7f-afd2-
9c7f055b569c;rport 
To: "300"sip:300@192.168.113.175 
From: "300"<sip:300@192.168.113.175>tag=hkxlofod 
CSeq: 8 REGISTER 
Call-ID: vwenplkrqdxvaekqbbwjgqxrgenxdlntenvdxgluhmuflcqaet 
Max-Forwards: 70 
Contact: sip:300@192.168.115.181:20041;rinstance=11372d3a07ebbc47 
User-Agent: Ozeki VoIP SIP SDK v10.1.7
Content-Length: 0 
Expires: 3600 
Supported: 100rel 
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, SUBSCRIBE, NOTIFY, REFER, INFO, MESSAGE 
Proxy-Authorization:Digest username="300",realm="OzekiPBX",
nonce="1f9c065c0bd24a4992f3dcca91de99e8",
response="3d893c1f2ad94e306c3eebd15f7a7a2c",uri="sip:192.168.113.175",
algorithm=MD5

Step 4: Acknowledge of registration (UDP message, PBX -> Softphone)

Status-Line: SIP/2.0 200 OK 
Via: SIP/2.0/UDP 192.168.115.181:20041;branch=z9hG4bK68c7248e-bc7d-4d7f-afd2-
9c7f055b569c;rport=20041;
received=192.168.115.181 
From: "300"<sip:300@192.168.113.175>tag=hkxlofod 
Call-ID: vwenplkrqdxvaekqbbwjgqxrgenxdlntenvdxgluhmuflcqaet 
CSeq: 8 REGISTER 
To: "300"<sip:300@192.168.113.175>tag=vlkgrwte 
User-Agent: Ozeki Phone System XE v5.2.1 
Content-Length: 0 
Contact: <sip:300@192.168.115.181:20041;rinstance=11372d3a07ebbc47>expires=3600

Related Pages

Ozeki Cookie Policy
Ozeki Informatics Ltd uses cookies to provide you the best experience on this website. The further use of the website will be considered as an agreement to the use of cookies. For more information read this website.

Cookies are enabled You are browsing the optimized version of this website. For more information read this website.