Click here to Skip to main content
15,891,409 members
Articles / Programming Languages / C#
Article

Simple Chat Application Using .Net Sockets

Rate me:
Please Sign up or sign in to vote.
1.23/5 (19 votes)
22 Mar 20043 min read 116.8K   7.1K   32   4
It is a simple chat application that helps two users to chat live. It makes simple use of .Net classes TcpClient and TcpListener.

Sample screenshot

Introduction

Working with .Net Sockets is fun. The three main classes used are TcpClient, TcpListener and NetworkStream. In the chat application, there is a listener application that is always started first. It starts the listener using TcpListener.Start(). A TcpClient object is obtained for the listener using TcpListener.AcceptTcpClient(). This is underlying TcpClient object for the listener. A NetworkStream object is obtained for the listener that can be used for sending and receiving byte data over the TCP/IP. This is done using TcpClient.GetStream(). It also starts a thread, which polls for an incoming message using NetworkStream.Read() . This incoming byte data is converted into a string and displayed in the incoming message text box that is a read-only text box, obviously. There is a “Send” button associated with the text box for entering the message to send. When this button is clicked the contents of the text box are converted into an array of bytes and sent to the other party that is the client. This activity is done on clicking the send button only and runs in the main thread and so is independent of the polling thread.<o:p>

 <o:p>

On the client side, a thread is started, which polls for an incoming message from the listener. The client application uses only TcpClient. A NetworkStream object is obtained for the listener that can be used for sending and receiving byte data over the TCP/IP. This is done using TcpClient.GetStream().  It also starts a thread, which polls for an incoming message using NetworkStream.Read(). This incoming byte data is converted into a string and displayed in the incoming message text box that is a read-only text box, obviously. There is a “Send” button associated with the text box for entering the message to send. When this button is clicked the contents of the text box are converted into an array of bytes and sent to the other party that is the listener. This activity is done on clicking the send button only and runs in the main thread and so is independent of the polling thread.<o:p>

 <o:p>

Three is a special message that can be sent to the other party to close the conversation. This message has string “shutdown”. If this message is written into the text box for sending message, the other party, on receiving the message will stop the polling thread and show a user warning that they cannot send any message anymore. The user of either application can then close the window. Closing the window on either side can also close the conversation.

Download the source code zip and extract zip file contents. The TcpDeviceSimulatoryListener project should be built and the executable is to be run. Click on the button "Start Listener" to start the listener. The TcpClient project should be copied to machine other than the machine where listener is running. The TcpClient project should be built and the executable is to be run. Enter IP Address of the machine on which listener is running, and click on the "Connect" button. At the listener's machine, a window is opened that provides text box to write the message to send to the client. On the client's machine, again a text box is provided to enter message to send to listener. The use of "Send" buttons on both the client and listener windows is required to send message to other party. At any point of time, windows could be closed to end the conversation. This application becomes very useful when only two users want to communicate to each other. Appropriate user warning messages are shown when required.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
I have been in the field of object-oriented programming for six years now. My areas of interest are discrete mathematics, programming in C++,C#, .Net framework.

Comments and Discussions

 
GeneralDont Work! Pin
BSH-Systems18-Oct-09 7:30
professionalBSH-Systems18-Oct-09 7:30 
General[Message Deleted] Pin
it.ragester2-Apr-09 21:58
it.ragester2-Apr-09 21:58 
QuestionCan we use same for web application Pin
Nagraj Naik5-Jan-07 18:34
Nagraj Naik5-Jan-07 18:34 
AnswerRe: Can we use same for web application Pin
ivasmens23-Apr-09 1:12
ivasmens23-Apr-09 1:12 

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.