Click here to Skip to main content
13,250,453 members (58,571 online)
Click here to Skip to main content
Add your own
alternative version


238 bookmarked
Posted 5 Jul 2007

A Voice Chat Application in C#

, 5 Jul 2007
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

GeneralChat Conference Pin
tempsh21-Aug-07 11:56
membertempsh21-Aug-07 11:56 
GeneralRe: Chat Conference Pin
hitesh_sharma21-Aug-07 12:25
memberhitesh_sharma21-Aug-07 12:25 
GeneralRe: Chat Conference Pin
tempsh21-Aug-07 20:32
membertempsh21-Aug-07 20:32 
GeneralArticle title misleading Pin
lallous17-Jul-07 6:58
memberlallous17-Jul-07 6:58 

First, let me thank you for your efforts and work.

When I saw the article, I thought you're going to talk about how to capture audio, send audio, and discuss some networking issues.

I was disappointed to learn that you only covered the networking part, which is obvious than the audio part.

You could have written the article in a better way.

GeneralRe: Article title misleading Pin
hitesh_sharma17-Jul-07 8:45
memberhitesh_sharma17-Jul-07 8:45 
GeneralRe: Article title misleading Pin
Jason Barrera25-Jul-07 19:57
memberJason Barrera25-Jul-07 19:57 
GeneralRe: Article title misleading Pin
hitesh_sharma21-Aug-07 12:18
memberhitesh_sharma21-Aug-07 12:18 
QuestionJust a little tick in audio? Pin
jerommeke8312-Jul-07 4:24
memberjerommeke8312-Jul-07 4:24 
AnswerRe: Just a little tick in audio? [modified] Pin
hitesh_sharma13-Jul-07 6:11
memberhitesh_sharma13-Jul-07 6:11 
GeneralUDP unreliable Pin
mav.northwind5-Jul-07 10:49
membermav.northwind5-Jul-07 10:49 

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.

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