.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to ring a C# SIP softphone

Posted By:Evan Miller       Posted Date: July 07, 2011    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

This article is about the simple act of ringing a SIP softphone.

The  Voice over Internet Protocol (VoIP) (telephoning on the internet) is a world-wide telecommunication technology in the corporated telecommunication systems and lately most internet suppliers offer VoIP communication solutions to their customers. Its spread can be attributable to the favorable configurating and maintaining costs. To realize VoIP communication many protocols were born, but the two most successful were the SIP (Session Initiation Protocol) and the H.323 standard made by the ITU-T (International Telecommunication Union). Because SIP offers a newer and simplier realization than H.323 the VoIP world is tending to the SIP VoIP solutions. SIP is a text based internet communication protocol. Its object is to build up the communication connections between the participants. Doing this by SIP messages in request-response form, with HTML-like header-fields.

When I started to deal with this subject, I aimed my first milestone which was ringing a SIP phone. In this article I try to present my results.


First, let's see how to initialize a SIP call. SIP standard makes direct communication possible for SIP devices without adding any SIP server and with the help of certain conditions (e.g. the two devices need to be in one network domain). That is why I have developed a program for ringing a SIP phone that sends an INVITE request to a SIP telephone which is in the same network domain. According to the SIP standard the INVITE request is for initializing a call. It signs that a call has been initialized toward its direction. If this device has received my message and it could process the message, as well, it rings and displays the phone number from which the call was received.

1.    string myUserName = "oz871";  

2.                string myLocalAddress = "";  


4.                string calleeUserName = "oz873";  

5.                string calleeIPAddress = "";  


7.                string message =  

8.                    "INVITE sip:" + calleeUserName + "@" + calleeIPAddress + " SIP/2.0\r\n" +  

9.                    "Via: SIP/2.0/UDP " + myLocalAddress + ";branch=z9hG4bKnashds8\r\n" +  

10.                                       "Max-Forwards: 70\r\n" +  

11.                                       "To: " + calleeUserName + " \r\n" +  

12.                                       "From: " + myUserName + " ;tag=1928301774\r\n" +  

13.                                       "Call-ID: a84b4c76e66710\r\n" +  

14.                                       "CSeq: 314159 INVITE\r\n" +  

15.                                       "Contact: \r\n" +  

16.                                       "Content-Length: 0\r\n" +  

17.                                       "\r\n";  

The requirements relating to the form of SIP messages can also be discovered here:

1.    "INVITE sip:" + calleeUserName + "@" + calleeIPAddress + " SIP/2.0\r\n" +  

This line builds up the request-line of the message that makes it clear that it is a SIP request. The following line includes the IP address (SIP URI) of the device that has been rang:

1.    calleeUserName + "@" + calleeIPAddress   

It is followed by the version number of the used SIP that is closed by the Carriage Return characters that also close the start-line of the SIP message.

1.    "SIP/2.0\r\n"    

Via header

1.    "Via: SIP/2.0/UDP " + myLocalAddress + ";branch=z9hG4bKnashds8\r\n"   

This line is already a header field that is mandatory to create a request. It marks the used SIP version of which value needs to be constant SIP/2.0 according to the current SIP standard. It also marks the data transfer protocol and the address where the addressee device needs to send its response to.

MAX forwards header

1.    "Max-Forwards: 70\r\n"  

All request messages includes this header. It defines with an integer number that the given message via how many "hops" reaches its destination. The standard suggests 70 values for determining this quantity.

To header

1.    "To: " + calleeUserName + " \r\n"  


It includes the recipient of the request message and its first optional parameter is the display name of the caller. It is the calleeUserName in this example. This is followed by the SIP URI of the recipient that can be followed by the TAG parameter. TAG is another definition to identify dialogues; it is a string that is created randomly.


The connection evolved between two or more SIP devices during the call is called dialogue. Usually dialogues are created with an INVITE message and destroyed with the BYE message. The call-id is the TAG parameter of "to" header and "from" header and it identifies a dialogue.

From header

1.    "From: " + myUserName + " ;tag=1928301774\r\n"  


This message header includes the optional display name that creates a request as well as its SIP uri. Also it contains the randomly generated string TAG which identifies the current dialogue.

Call-ID header

1.    "Call-ID: a84b4c76e66710\r\n"   

It is a generated unique identifier and its value is the same within a dialogue in every request and response message. It is meant to identify the dialogues that are separated from each other.

CSeq header

1.    "CSeq: 314159 INVITE\r\n"   

This is the header that identifies the transactions between SIP devices through an unsigned 32 bit integer number. This number is ascending by +1 in the consecutive transactions. Also the header includes the method that creates the transaction in this case it is the INVITE.

Contact header

1.    "Contact: \r\n"  


The contact header includes the UA SIP address that creates the request. Via this header the UAs participating in the communication can contact with each other.


1.    "Content-Length: 0\r\n"  

The content-length header includes the length of the SIP message body and it is in bytes. In this sample code this length is 0 because the aim of the sample is only to ring a telephone.


Now the previously created call initializing SIP message only needs to be sent to the wished telephone. This is done with the help of UDP (User Datagramm Protocol). It can be done easily through the UdpClient device offered by Microsoft .Net. Here is the code to do it:

1.    UdpClient udpClient = new UdpClient();  

2.                    Console.Write("Sending INVITE message ... ");  

3.                    udpClient.Connect(calleeIPAddress, 5060);  

4.                    Byte[] sendBytes = Encoding.ASCII.GetBytes(message);  

5.                    udpClient.Send(sendBytes, sendBytes.Length);  

6.                    Console.WriteLine("DONE!");  

Where I make an instance of the .NET device then I set the address of the recipient. I code the SIP message into a byte block then I send it with Send method which is related to UdpClient object.


This was only a brief presentation of the coding if you want to ring a SIP phone. These are the foundations you can build your application on. But you will need many more working hours in order to implement this coding into an existing VoIP SIP standard. If you want to ease the whole process you can choose to use a VoIP SIP SDK. My favourite is made by a company named Ozeki. Here you can find information about their software, if you're interested: http://www.voip-sip-sdk.com/p_190-how-to-ring-a-c-sip-softphone-using-the-sip-stack-without-using-an-ip-pbx-voip.html

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend