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 .…