.NET Simple Simple Chat Example

I was looking for a simple step-by-step tutorial for .Net programmers. After some Google searches, I found a collection of "CSharp Communications" codes at net-informations.com. It looked pretty good until I got to the How to C # Chat Server example .

The authors propose a multi-threaded server with a HashTable container for storing all connections in the shared memory on the server side. According to the MSDN documentation TcpClient and NetworkStream, the classes used for broadcast messages are not thread safe, and the example uses them from several server threads.

My questions:

  • Could you confirm that the example is incorrect?
  • How to do this, is it enough to block the broadcast method (mark it as a critical section)?
  • Could you advise some tutorials for sockets (preferable?)?
+3
source share
2 answers

According to the MSDN documentation, the TcpClient and NetworkStream classes used for broadcast messages are not thread safe, and the example uses them from several server threads.

It is right; but it's about concurrent access. If each thread uses the instance in turn (for example, using locks to control access), different threads can be used.

In other words: an unsafe thread does not imply binding to a single thread.

+1
source

This is not ideal since I wrote this almost 7 years ago, but it covers and gives you a good idea of ​​the TCP message field:

Shared TCP / IP Client Server

+5
source

All Articles