Click here to Skip to main content
Click here to Skip to main content

Simple Chat Application Using .Net Sockets

, 22 Mar 2004
Rate this:
Please Sign up or sign in to vote.
It is a simple chat application that helps two users to chat live. It makes simple use of .Net classes TcpClient and TcpListener.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS--><!-- Download Links --> <!-- Article image -->

Sample screenshot

<!-- Add the rest of your HTML here -->

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.

 

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.

 

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

Share

About the Author

Prasad N
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! PinmemberSerja Ru18-Oct-09 7:30 
General[Message Deleted] Pinmemberit.ragester2-Apr-09 21:58 
QuestionCan we use same for web application PinmemberNagraj Naik5-Jan-07 18:34 
AnswerRe: Can we use same for web application Pinmemberivasmens23-Apr-09 1:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.8.140827.1 | Last Updated 23 Mar 2004
Article Copyright 2004 by Prasad N
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid