4

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)?
kuszi
  • 2,069
  • 29
  • 36

2 Answers2

6

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

G.Y
  • 6,042
  • 2
  • 37
  • 54
  • -1 - [Stack Overflow is not Your Free Promotional Site™](http://meta.stackexchange.com/a/128560/140505). – Oded May 14 '12 at 09:48
  • 3
    I replied to his question: "Could you recommend some socket communication tutorials (.Net preferred)?" (Code-Project is not my site! very insulting that you would judge my motives like that.) – G.Y May 14 '12 at 09:57
  • Well, that part of the question is considered off-topic on Stack Overflow. See [Stack Overflow is not a Link Farm or Search Engine](http://meta.stackexchange.com/a/128549/140505). You have also disregarded the other parts of the question that are on topic. – Oded May 14 '12 at 10:00
  • 2
    I think that his question is in the spirit of the site and my answer is as well - but you have a point as well and I can see why you would question my motives - so, i'll let the community decide. If even 1 more person would think that I am out of line and demote my answer - so be it - I will be happy to remove it. – G.Y May 14 '12 at 10:16
  • I would vote up, if you would do the courtesy of adding a bit of a summary. Other than that it's exactly what the op is asking for. – Michael Brown May 14 '12 at 22:43
  • Thanks Mike, do you mean adding summary of what is in the tutorial? I'm not sure I understand what you meant. – G.Y May 15 '12 at 08:11
1

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.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • There are many threads at the server side broadcasting messages to many clients connected to the server. – kuszi May 15 '12 at 11:43
  • @kuszi See my first paragraph: avoid two threads both accessing the same socket at exactly the same time via locking. – Richard May 15 '12 at 13:15