469,286 Members | 2,409 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

Thread safe socket code

Hi,

Im currently using the following code for reading/writing to a network
socket.

private StreamReader clientStreamReader;
private StreamWriter clientStreamWriter;
....
TcpClient tcpClient = new
TcpClient(server_host_name, server_port);
NetworkStream clientSockStream =
tcpClient.GetStream();
clientStreamReader = new
StreamReader(clientSockStream);
clientStreamWriter = new
StreamWriter(clientSockStream);

I have been told using the StreamReader & StreamWriter classes is not
thread safe, and as i am about to implement threading i need to get
this fixed.

Is there a simple solution to make reading and writing to the stream
thread safe?

Thanks
Jack

Apr 7 '07 #1
1 4238
On Sat, 07 Apr 2007 12:44:00 -0700, <je******@gmail.comwrote:
[...]
I have been told using the StreamReader & StreamWriter classes is not
thread safe, and as i am about to implement threading i need to get
this fixed.

Is there a simple solution to make reading and writing to the stream
thread safe?
Maybe you can be more clear about what "thread safeness" it is you're
looking for.

"Thread safe" means that something can be used simultaneously from
multiple threads without a problem. However, using StreamRead and
StreamWriter with the TcpClient, even if you synchronized access to the
reader and writer across multiple threads, that's not usually what one
would want to do. Making the use of the objects "thread safe" is simple
enough, but if you have more than one thread reading from the same stream
or more than one thread writing to the same stream, you may run into all
sorts of other problems (mostly to do with coordinating what each thread
reads or writes).

If you are adding new threads to your design, but those new threads are
not actually going to access the stream and/or the reader and writer, then
they are already "thread safe" enough. That is, they aren't really thread
safe but you also aren't doing anything that would cause any problems in
that respect.

(Note: using the lower-level API, Winsock, it is fine to read in one
thread and write in another thread using the same socket. I don't know
whether the NetworkStream class inherits this behavior or not; it's
possible that the documentation for the class says one way or the other,
but I haven't had a chance to review it to see. It's probably safest to
assume that you can't use the NetworkStream from more than one thread at a
time, even if it's a matter of reading in one thread and writing in
another. But it's possible that requirement doesn't actually exist and
that all you really need to worry about is not reading in more than one
thread at a time and not writing in more than one thread at a time).

The fact is that most of the .NET classes aren't thread safe. If a .NET
class (or some subset of the class) is thread safe, usually it's just
because the work that the class does is inherently thread safe. And yet,
multi-threaded .NET programs can often be written without any explicit
synchronization happening, simply because the threads are actually doing
different things and not trying to access the same data.

So maybe if you can elaborate on how it is you are using the streams and
whether they are actually going to be used from different threads
simultaneously, that would help in providing you the best answer.

Pete
Apr 7 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by phil | last post: by
3 posts views Thread by Jos | last post: by
4 posts views Thread by Matthew Groch | last post: by
7 posts views Thread by e2wugui | last post: by
6 posts views Thread by fniles | last post: by
5 posts views Thread by Gordon Messmer | last post: by
18 posts views Thread by =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post: by
8 posts views Thread by Brad Walton | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.