Tutorials‎ > ‎

[C# Async TCP] Simple Chat Application

posted Jan 3, 2013, 1:33 AM by Dandi Aulia   [ updated Aug 15, 2016, 11:22 PM by Surya Wang ]

 Table of Contents:

-          Asynchronous Programming introduction

-          Socket Programming Introduction

-          Asynchronous Socket Introduction

Asynchronous Programming Introduction


In programming, asynchronous events are those occurring independently of the main program flow. Asynchronous actions are actions executed in a non-blocking scheme, allowing the main program flow to continue processing.

Example of blocking scheme:

In a piece of code above, you see that application will wait until File Stream finish to write a file, it’s bad for your user if he wants to write a bulk of file/documents because application such as windows form will freezing until operation finished.

This problem could be solved easily using multithread / asynchronous programming model, with this model we can separate process in one application.

Example of non-blocking scheme with thread:

In example above, application flow will skip/jump over to the next line which code will not prevent main programming flow to be executed.

Example of non-blocking scheme with Asynchronous model:


In example above, application will writing to the stream. After write is finished, a callback method should be called with IAsyncResult interface parameters. It is very useful because we’re noticed when it will be finished, and we can do some work after writing is finished like showing success to write notification.

Socket Programming Introduction


A socket is one of the most fundamental technologies of computer networking. Sockets allow applications to communicate using standard mechanisms built into network hardware and operating systems. Although network software may seem to be a relatively new "Web" phenomenon, socket technology actually has been employed for roughly two decades.

Sockets are bidirectional, meaning that either side of the connection is capable of both sending and receiving data. Sometimes the one application that initiates communication is termed the client and the other application the server, but this terminology leads to confusion in non-client/server systems and should generally be avoided.

Socket programming is a  is very attractive programming field, because we can do many things with multiple machine such as parallel programming, chat application, file sharing etc.

                In a piece of code below demonstrate how to build simple socket programming, as you can see WriteMessage method act as client that sending message over IP(Internet Protocol) loopback and specified port to ReadMessage method as server.

Asynchronous Socket Introduction


Generally, sockets are identical with I/O operations, because we sent data from one source to another. Thus, we can use identically asynchronous operation with sockets.

In a piece of code below, we define a socket that will listen to incoming connection.

in call of BeginAccept, program will not block the application flow, so next line of code will be run immediately after BeginAccept is called. So application will not stack at this point.

In code above, program will accept a connection, adding client connection to a connection pool from IAsyncResult then begin receiving data from it and start new acceptance for another client.

For further example of real application on TCP Async sockets, please download attached files.

Dandi Aulia,
Jan 3, 2013, 1:33 AM