Menu

What I learned building a multi-client chat server in C# with TCP sockets
📰

What I learned building a multi-client chat server in C# with TCP sockets

DEV Community: csharp·Ahmed Gamal·7 days ago
#WU2kTieB
#devtocsharp#devto#dev#client#message
Long article (922 words) — no AI summary yet.
Reading 0:00
15s threshold

Most .NET developers reach for SignalR when they need a chat feature, and that is usually the right call. But I wanted to know what SignalR is doing for me, so I built a small chat system from scratch: one server, many clients, plain TCP sockets, C# and .NET 8, with WinForms on both sides.

The repository is here: https://github.com/AGA7474/Asynchronous-Socket-Chat

Starting the listener without freezing the UI

The server is a WinForms app, which means there is a UI thread I have to protect. The accept loop is the first place where that matters:

private async Task Start ( ) { IPAddress iPAddress = IPAddress . Parse ( "127.0.0.1" ) ; int port = 8080 ; TcpListener tcpListener = new TcpListener ( iPAddress , port ) ; tcpListener . Start ( ) ;

while ( true ) { TcpClient tcpClient = await tcpListener . AcceptTcpClientAsync ( ) ; Client client = new Client ( tcpClient ) ; clients . Add ( client ) ; client .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.