Click here to Skip to main content
11,924,800 members (55,999 online)
Click here to Skip to main content
Add your own
alternative version


229 bookmarked

A Voice Chat Application in C#

, 5 Jul 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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

You may also be interested in...

Comments and Discussions

Questionhow can I fix the following errors during run the project display this errors so pls help me Pin
Member 1123983129-May-15 2:43
memberMember 1123983129-May-15 2:43 
QuestionCheck to Target platform Pin
Member 47040689-Mar-15 11:32
memberMember 47040689-Mar-15 11:32 
QuestionGet unhandled exception error when debugging Pin
Member 104483845-Dec-14 15:12
memberMember 104483845-Dec-14 15:12 
Questionconnection problem Pin
Member 1043437817-Feb-14 5:25
professionalMember 1043437817-Feb-14 5:25 
QuestionNoise Pin
Member 88923497-Jan-14 2:32
memberMember 88923497-Jan-14 2:32 
AnswerRe: Noise Pin
Zaryab Iftikhar27-May-14 6:55
memberZaryab Iftikhar27-May-14 6:55 
QuestionDirectX Problem Pin
Member 1006022322-May-13 8:37
memberMember 1006022322-May-13 8:37 
AnswerRe: DirectX Problem Pin
Aletheuein3-Jun-13 3:17
memberAletheuein3-Jun-13 3:17 
QuestionIs it possible to voice call to remote desktop using this code? Pin
rajesh@198910-Apr-13 4:17
memberrajesh@198910-Apr-13 4:17 
QuestionMessage Automatically Removed Pin
1-Sep-12 23:23
memberCrisLoveStefy1-Sep-12 23:23 
QuestionMessage Automatically Removed Pin
23-Aug-12 20:48
membersaravanan33323-Aug-12 20:48 
Questionfor all Pin
kumeneger11-May-12 4:06
memberkumeneger11-May-12 4:06 
AnswerRe: for all Pin
Helldrake1-Jul-13 22:57
memberHelldrake1-Jul-13 22:57 
BugProblem! Pin
JrPacheco17-Apr-12 4:59
memberJrPacheco17-Apr-12 4:59 
Questiononly one usage of each socket address (protocol/network address/port) is normally permitted Pin
Member 84226775-Apr-12 2:34
memberMember 84226775-Apr-12 2:34 
Questionis it possible to encrypt a voice in your application? Pin
Thendral.N27-Mar-12 2:02
memberThendral.N27-Mar-12 2:02 
QuestionVoice chat application doesnt connect with another machine Pin
Thendral.N24-Jan-12 2:00
memberThendral.N24-Jan-12 2:00 
Questiononly one usage of each socket address protocol network address port is normally permitted Pin
Thendral.N24-Jan-12 0:19
memberThendral.N24-Jan-12 0:19 
QuestionSend message issue Pin
danm3612-Jan-12 11:12
memberdanm3612-Jan-12 11:12 
Questionhi sir how can I add Microsoft.directx and Microsoft.directx .DirectSound and voicechat.vhost.exe.manifest refferance files tell me plz Pin
bahidab4-Jan-12 1:17
memberbahidab4-Jan-12 1:17 
QuestionQuerry.. Pin
Tarun Khatana14-Dec-11 9:45
memberTarun Khatana14-Dec-11 9:45 
Generalno sound while lost focus Pin
Akeeq14-Jun-11 2:39
memberAkeeq14-Jun-11 2:39 
GeneralRe: no sound while lost focus Pin
Akeeq16-Jun-11 21:40
memberAkeeq16-Jun-11 21:40 
GeneralRe: no sound while lost focus Pin
harshad3654-Dec-12 18:43
memberharshad3654-Dec-12 18:43 
GeneralRe: no sound while lost focus Pin
Akeeq11-Dec-12 1:52
memberAkeeq11-Dec-12 1:52 
GeneralSound Quality Pin
Oblivion1707-Jun-11 6:51
memberOblivion1707-Jun-11 6:51 
GeneralRe: Sound Quality Pin
bubifengyun7-Jan-13 17:36
memberbubifengyun7-Jan-13 17:36 
Questionhi Pin
maktumpashal22-May-11 3:15
membermaktumpashal22-May-11 3:15 
GeneralMDA LoaderLock Error; Error in Application Pin
Akeeq13-May-11 20:36
memberAkeeq13-May-11 20:36 
GeneralRe: MDA LoaderLock Error; Error in Application Pin
Akeeq16-Jun-11 21:44
memberAkeeq16-Jun-11 21:44 
GeneralMy vote of 5 Pin
Member 446500731-Mar-11 15:29
memberMember 446500731-Mar-11 15:29 
GeneralHi Pin
Kiya161-Mar-11 8:33
memberKiya161-Mar-11 8:33 
Generali want to make it over internet Pin
crysis8928-Feb-11 12:28
membercrysis8928-Feb-11 12:28 
Generalerror Pin
samathu22-Feb-11 21:11
membersamathu22-Feb-11 21:11 
Generalerror Pin
samathu21-Feb-11 5:19
membersamathu21-Feb-11 5:19 
GeneralSome question Pin
Rezabest19-Jan-11 3:27
memberRezabest19-Jan-11 3:27 
GeneralMy vote of 3 Pin
lxxlxx4-Jan-11 3:09
memberlxxlxx4-Jan-11 3:09 
GeneralMuLawEncoder Vs ALawEncoder What's the difference Pin
jibin.mn30-Oct-10 1:09
memberjibin.mn30-Oct-10 1:09 
Generalerrors regarding the project "A Voice Chat Application in C#". Pin
hemendrapsingh28-Feb-10 10:17
memberhemendrapsingh28-Feb-10 10:17 
GeneralMy vote of 2 Pin
somireddy25-Feb-10 23:04
membersomireddy25-Feb-10 23:04 
GeneralCompact Framework Pin
sonia108211-Jan-10 20:20
membersonia108211-Jan-10 20:20 
QuestionError while calling on second time [modified] Pin
hiran53612-Oct-09 22:08
memberhiran53612-Oct-09 22:08 
Generalnot working in studio 2008 Pin
surpatel785-Oct-09 3:21
membersurpatel785-Oct-09 3:21 
AnswerRe: not working in studio 2008 Pin
Smacky31125-May-10 4:44
memberSmacky31125-May-10 4:44 
GeneralNotify Pin
mayuwi1-Oct-09 16:12
membermayuwi1-Oct-09 16:12 
QuestionHow to select the device dynamically ? Pin
Jack98739-Sep-09 1:23
memberJack98739-Sep-09 1:23 
AnswerRe: How to select the device dynamically ? Pin
mayuwi1-Oct-09 14:52
membermayuwi1-Oct-09 14:52 
GeneralVoiveChat-InitializeCall() Only one usage of each socket address (protocol/ network address/ port) is nomally permitted Pin
aadiost24-Aug-09 3:49
memberaadiost24-Aug-09 3:49 
GeneralRe: VoiveChat-InitializeCall() Only one usage of each socket address (protocol/ network address/ port) is nomally permitted Pin
sloaken6-Sep-09 22:18
membersloaken6-Sep-09 22:18 
GeneralOnly one usage of each socket address(protocol/network address/port) is normally permitted Pin
goitom219-Aug-09 5:36
membergoitom219-Aug-09 5:36 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.3 | Last Updated 5 Jul 2007
Article Copyright 2007 by Hitesh Sharma
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid