Click here to Skip to main content
15,069,297 members
Articles / Programming Languages / C#
Posted 5 Jul 2007


245 bookmarked

A Voice Chat Application in C#

Rate me:
Please Sign up or sign in to vote.
4.37/5 (60 votes)
5 Jul 2007CPOL2 min read
In this article I will discuss a simple voice chat application. We will grab the audio from the microphone using DirectSound and transmit it in UDP packets.

Screenshot - VoiceChat.jpg


In this article, I will discuss a simple voice chat application. We will grab the audio from the microphone using DirectSound and transmit it in UDP packets. To make things interesting, I used G711 vocoder to compress the data before transmission. I won't discuss here how to capture the audio from microphone and play it back using DirectSound or how G711 is implemented. For all these things you can look into the references given at the end of this article. Before we go further, let's discuss the architecture of the application. We will use the following call messages between the parties.

|             Invite                | 

| --------------------------------> |

|               OK                  |

| <-------------------------------- |

|                                   |

| --------------------------------> |

|            Audio flow             |

| <-------------------------------- |

|               Bye                 | 

| --------------------------------> |

A                                   B

Using the code

Here are the commands for interaction between the two parties:

enum Command
    Invite, //Make a call.
    Bye,    //End a call.
    Busy,   //User busy.
    OK,     //Response to an invite message. OK is sent to 
            //indicate that call is accepted.
    Null,   //No command.

When the user wants to make a call, we send an Invite message and wait for an OK response. When we receive an OK, we start receiving/sending audio captured from the microphone. If the remote party rejects the call then a Busy response is sent. To drop a call, we simply send a Bye message. The application will asynchronously receive/send call messages on port 1450 and synchronously receive/send audio data on port 1550. In other words, the application listens on two ports: one for call messages and the other for audio data. I will now walk you through some code. When the application starts, we start listening for call messages on port 1450:

//Using UDP sockets
clientSocket = new Socket(AddressFamily.InterNetwork, 
EndPoint ourEP = new IPEndPoint(IPAddress.Any, 1450);

//Listen asynchronously on port 1450 for 
//coming messages (Invite, Bye, etc).

//Receive data from any IP.
EndPoint remoteEP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0));
byteData = new byte[1024];

//Receive data asynchornously.
    0, byteData.Length,
    ref remoteEP,
    new AsyncCallback(OnReceive),

When we receive a message, we process it to see what type of message it is and act accordingly. Please see the OnReceive handler for it in the attached project files. To receive/send audio from a microphone, we start two threads so that the synchronous send/receive doesn't block our UI. This is done as follows, noting that the audio is received/sent on port 1550:

private void InitializeCall()
        //Start listening on port 1500.
        udpClient = new UdpClient(1550);
        Thread senderThread = new Thread(new ThreadStart(Send));
        Thread receiverThread = new Thread(new ThreadStart(Receive));
        bIsCallActive = true;

        //Start the receiver and sender thread.
        btnCall.Enabled = false;
        btnEndCall.Enabled = true;
    catch (Exception ex)
            "VoiceChat-InitializeCall ()", MessageBoxButtons.OK, 

The Send and Receive methods can be seen in the attached project; understanding them is easy.



  • 5 July, 2007 -- Original version posted


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hitesh Sharma
United States United States
No Biography provided

Comments and Discussions

QuestionWhy Winform WebBrowser doesn't load a page when starting .exe from Windows StartUp Pin
glbrt.gds23-Jul-18 4:00
Memberglbrt.gds23-Jul-18 4:00 
GeneralI can hear nothing but noise Pin
Member 1313306518-May-17 21:39
MemberMember 1313306518-May-17 21:39 
Questionno voice Pin
Member 118326193-May-17 7:15
MemberMember 118326193-May-17 7:15 
QuestionCode not working..There is an exception Pin
Member 122582446-Apr-17 18:58
MemberMember 122582446-Apr-17 18:58 
Questionmy voice call not fire Pin
La Nando5-Feb-17 2:46
MemberLa Nando5-Feb-17 2:46 
QuestionCross Thread Operation is not valid Pin
Member 117412208-Mar-16 8:05
MemberMember 117412208-Mar-16 8:05 
QuestionPlease help Pin
Member 16613571-Dec-15 5:43
professionalMember 16613571-Dec-15 5:43 
Questionhow can I fix the following errors during run the project display this errors so pls help me Pin
Member 1123983129-May-15 1:43
MemberMember 1123983129-May-15 1:43 
QuestionCheck to Target platform Pin
Member 47040689-Mar-15 10:32
MemberMember 47040689-Mar-15 10:32 
QuestionGet unhandled exception error when debugging Pin
Member 104483845-Dec-14 14:12
MemberMember 104483845-Dec-14 14:12 
Questionconnection problem Pin
Member 1043437817-Feb-14 4:25
professionalMember 1043437817-Feb-14 4:25 
AnswerRe: connection problem Pin
Member 1128299929-Nov-15 7:57
MemberMember 1128299929-Nov-15 7:57 
QuestionNoise Pin
Member 88923497-Jan-14 1:32
MemberMember 88923497-Jan-14 1:32 
AnswerRe: Noise Pin
Zaryab Iftikhar27-May-14 5:55
MemberZaryab Iftikhar27-May-14 5:55 
QuestionDirectX Problem Pin
Member 1006022322-May-13 7:37
MemberMember 1006022322-May-13 7:37 
AnswerRe: DirectX Problem Pin
Aletheuein3-Jun-13 2:17
MemberAletheuein3-Jun-13 2:17 
QuestionIs it possible to voice call to remote desktop using this code? Pin
rajesh@198910-Apr-13 3:17
Memberrajesh@198910-Apr-13 3:17 
Questionfor all Pin
kumeneger11-May-12 3:06
Memberkumeneger11-May-12 3:06 
AnswerRe: for all Pin
Helldrake1-Jul-13 21:57
MemberHelldrake1-Jul-13 21:57 
GeneralRe: for all Pin
kumeneger1-Oct-16 10:16
Memberkumeneger1-Oct-16 10:16 
BugProblem! Pin
Júnior Pacheco17-Apr-12 3:59
professionalJúnior Pacheco17-Apr-12 3:59 
Questiononly one usage of each socket address (protocol/network address/port) is normally permitted Pin
Member 84226775-Apr-12 1:34
MemberMember 84226775-Apr-12 1:34 
Questionis it possible to encrypt a voice in your application? Pin
Thendral.N27-Mar-12 1:02
MemberThendral.N27-Mar-12 1:02 
QuestionVoice chat application doesnt connect with another machine Pin
Thendral.N24-Jan-12 1:00
MemberThendral.N24-Jan-12 1:00 
Questiononly one usage of each socket address protocol network address port is normally permitted Pin
Thendral.N23-Jan-12 23:19
MemberThendral.N23-Jan-12 23:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.