kuszi
kuszi

Reputation: 2079

.NET Simple chat server example

I was looking for the simple step by step communication tutorial for .Net programmers. After some Google queries, I have found the "CSharp Communications" code collections at net-informations.com. It looked quite good as long as I reached the "How to C# Chat Server" example.

Authors propose multithreaded server with the HashTable container to keep all connections in the shared memory at the server side. According to MSDN documentation TcpClient and NetworkStream classes used for broadcast messages are not thread safe, while the example uses them from multiple server threads.

My questions are:

  1. Could you confirm that the example is wrong?
  2. How should it be done, is it enough to lock the broadcast method (mark it as a critical section)?
  3. Could you recommend some socket communication tutorials (.Net preferred)?

Upvotes: 4

Views: 6600

Answers (2)

Richard
Richard

Reputation: 109140

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

This is correct; but it is about concurrent access. If each thread uses the instance in turn (eg. using locks to control access) then different threads can be used.

In other words: not thread safe does not imply tied to a single thread.

Upvotes: 1

G.Y
G.Y

Reputation: 6159

It is not perfect as I wrote it almost 7 years ago, but it is covering and will give you good understanding regarding the field of TCP communications:

Generic TCP/IP Client Server

Upvotes: 6

Related Questions