Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Chat Application Using .Net Sockets

0.00/5 (No votes)
22 Mar 2004 1  
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.

 

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