Chat Program using Asynchronous Socket | CodeGuru

Chat Program using Asynchronous Socket

. Environment: C# This is a simple chat program using asynchronous socket. This program is act like a client or a server. It can connect to another other PC (installed with the same program) or it can wait for another PC to connect to it. You can test the program on the same PC by […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 1, 2002
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

.

Environment: C#

This is a simple chat program using asynchronous socket. This program is act like a client or a server. It can connect to another other PC (installed with the same program) or it can wait for another PC to connect to it.

You can test the program on the same PC by running two instances of the
program. Each one listens to a port (e.g. first is listen to 998 and the second to 999). Click the listen button of both programs, key in the hostname/Host IP Address as “local” or “127.0.0.1”, fill in the port number as 999 on the first program (because you want to connect to the second program which listens to port 999),and finally click connect. The IP address will display in the list of the second instance of the program (if successfully connected). At this point, you can start sending messages.

In this simple program, I also demonstrated how the event is raised during
connection, disconnection, sending, and receiving (like the OnConnect, OnSend… in MSVC++ MFC’s socket class).

Update on 17 Apr 2001


This is an update to the
previous code post on 03/22/2002. In the update, I
include the sending and receiving function. The sending and receiving file
is based on my own command that can modify according to u requirement. The
concept is descript as below:


 




























Sending


Client


Server


Receiving


 


Send Filename and size to
`


Accepted the file
detail


 


 


 


_ Send signal back, tell client
to start sending


 


 


Once signal is received, start
sending file data. (Block by block until end of file)`


Store to
file.


 


 


Done


_ Successfully received
all


 


       


Actually, you can base on this kind of command to do a lot of stuff like searching file remotely, check remote PC information and other you can think of. Have fun.

Downloads


Download demo project – 13 Kb

Download source – 10 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.