472,351 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

Concurrent reader/writer threads on single socket

I have what I imagine is a well-known .Net networking problem, but even
though I've Googled for some time I've not yet come across a thread
where this has been fully explained...

There is a fairly common idiom (at least in the Java/UNIX/Win32 worlds)
where a socket connection is established and the program then utilises
two threads: one dedicated to reading from the socket and the other one
concurrently writing to the socket - both using blocking i/o. These
two streams of data are probably related, but the protocol is arranged
to allow pipelining of requests so that the full-duplex nature of the
socket can be fully utilised.

What I'd like to know most of all is whether this pattern is supported
in .Net. A secondary question is, if it's not supported, then how do
people cope with the additional complexity that the asynchronous
beginXXX/endXXX methods introduce.

To start with, I note from the Microsoft documentation [1] that the
Socket class is thread-safe, so it would be reasonable to assume that
what is valid for a Winsock socket is also valid for a .Net socket.
However, the same documentation states that if you need to access the
socket in a multi-threaded program you should use the asynchronous
methods. Even so, I think the document leaves some element of doubt as
to whether concurrent access by a separate reader and writer thread is
allowed (especially as it is such a common pattern in other
environments).

In order to follow this pattern, I would expect to see something like
the following (in C#)

NetworkStream ns = new NetworkStream(socket);
BinaryWriter writer = new BinaryWriter(ns);
BinaryReader reader = new BinaryReader(ns);
startReaderThread(reader);
startWriterThread(writer);

Now, a further inspecation of the Micorosft documentation reveals that
NetworkSocket [2] is not safe for concurrent access. I can understand
this as this class could potentially have some internal state (such as
a buffer) which would make multithreaded access unsafe. In that case,
perhaps we should modify the example to use two separate
NetworkStreams. e.g.:

BinaryWriter writer = new BinaryWriter(new NetworkStream(socket));
BinaryReader reader = new BinaryReader(new NetworkStream(socket));
...

This seems reasonable, the output stream would hold a separate buffer
to the input stream and everything is fine.

Certainly the above is possible, and in my test program it appears to
work (most of the time). But I believe it may not be correct because
my test program occasionally reads invalid input from it's reader - and
I can only imagine this is due to some data corruption at the stream
level.

So, on to the second part of my question: how does one go about
converting an application that's been designed to use blocking I/O and
layered streams to the asynchronous model? We have many message
parsing routines, all of which expect a stream as input and each
routine works on the expectation that it will read the data it needs
from the stream and then return. I'd love to know how this model could
be changed to use asynchrous i/o without greatly adding to the
complexity. I realise this may not be a simple question to answer
(hopefully someone will tell me that part (1) is okay so there's no
need).

Thanks in advance.
Rob Lugt

[1]
http://msdn2.microsoft.com/en-us/lib...ts.socket.aspx
[2]
http://msdn2.microsoft.com/en-us/lib...orkstream.aspx

Oct 20 '06 #1
6 6291
You don't have to use the asynchronous (beginXXX/endXXX ) calls to get the
effect of asynchronous IO. You can work directly with the Socket class
(implements the Berkely socket interface), fire up your own threads, and
make blocking calls on the socket. I haven't tried doing simultaneous reads
and writes, each on their own thread, but I think it ought to work - the
receive and send buffers ought to be logically separate. I don't believe the
Sockets class is inherently thread safe, but it should be ok so long as you
serialize your reads or writes to the socket on the same thread.

The sockets class is a thin wrapper over the Winsock implementation.

<ro*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>I have what I imagine is a well-known .Net networking problem, but even
though I've Googled for some time I've not yet come across a thread
where this has been fully explained...

There is a fairly common idiom (at least in the Java/UNIX/Win32 worlds)
where a socket connection is established and the program then utilises
two threads: one dedicated to reading from the socket and the other one
concurrently writing to the socket - both using blocking i/o. These
two streams of data are probably related, but the protocol is arranged
to allow pipelining of requests so that the full-duplex nature of the
socket can be fully utilised.

What I'd like to know most of all is whether this pattern is supported
in .Net. A secondary question is, if it's not supported, then how do
people cope with the additional complexity that the asynchronous
beginXXX/endXXX methods introduce.

To start with, I note from the Microsoft documentation [1] that the
Socket class is thread-safe, so it would be reasonable to assume that
what is valid for a Winsock socket is also valid for a .Net socket.
However, the same documentation states that if you need to access the
socket in a multi-threaded program you should use the asynchronous
methods. Even so, I think the document leaves some element of doubt as
to whether concurrent access by a separate reader and writer thread is
allowed (especially as it is such a common pattern in other
environments).

In order to follow this pattern, I would expect to see something like
the following (in C#)

NetworkStream ns = new NetworkStream(socket);
BinaryWriter writer = new BinaryWriter(ns);
BinaryReader reader = new BinaryReader(ns);
startReaderThread(reader);
startWriterThread(writer);

Now, a further inspecation of the Micorosft documentation reveals that
NetworkSocket [2] is not safe for concurrent access. I can understand
this as this class could potentially have some internal state (such as
a buffer) which would make multithreaded access unsafe. In that case,
perhaps we should modify the example to use two separate
NetworkStreams. e.g.:

BinaryWriter writer = new BinaryWriter(new NetworkStream(socket));
BinaryReader reader = new BinaryReader(new NetworkStream(socket));
...

This seems reasonable, the output stream would hold a separate buffer
to the input stream and everything is fine.

Certainly the above is possible, and in my test program it appears to
work (most of the time). But I believe it may not be correct because
my test program occasionally reads invalid input from it's reader - and
I can only imagine this is due to some data corruption at the stream
level.

So, on to the second part of my question: how does one go about
converting an application that's been designed to use blocking I/O and
layered streams to the asynchronous model? We have many message
parsing routines, all of which expect a stream as input and each
routine works on the expectation that it will read the data it needs
from the stream and then return. I'd love to know how this model could
be changed to use asynchrous i/o without greatly adding to the
complexity. I realise this may not be a simple question to answer
(hopefully someone will tell me that part (1) is okay so there's no
need).

Thanks in advance.
Rob Lugt

[1]
http://msdn2.microsoft.com/en-us/lib...ts.socket.aspx
[2]
http://msdn2.microsoft.com/en-us/lib...orkstream.aspx

Oct 21 '06 #2

David Levine wrote:
You don't have to use the asynchronous (beginXXX/endXXX ) calls to get the
effect of asynchronous IO. You can work directly with the Socket class
(implements the Berkely socket interface), fire up your own threads, and
make blocking calls on the socket. I haven't tried doing simultaneous reads
and writes, each on their own thread, but I think it ought to work - the
receive and send buffers ought to be logically separate.
Thanks David - this is what I expected. But my occasional program
crashes make me suspect that this might not be the case. I wonder
could anyone from the Microsoft .Net Networking team confirm this is
supported?

Oct 21 '06 #3
What kind of crashes?

"Rob Lugt" <ro*****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>
David Levine wrote:
>You don't have to use the asynchronous (beginXXX/endXXX ) calls to get
the
effect of asynchronous IO. You can work directly with the Socket class
(implements the Berkely socket interface), fire up your own threads, and
make blocking calls on the socket. I haven't tried doing simultaneous
reads
and writes, each on their own thread, but I think it ought to work - the
receive and send buffers ought to be logically separate.

Thanks David - this is what I expected. But my occasional program
crashes make me suspect that this might not be the case. I wonder
could anyone from the Microsoft .Net Networking team confirm this is
supported?

Oct 21 '06 #4

David Levine wrote:
What kind of crashes?
I'm pretty sure that occasionally the value read from the socket reader
is different from the value that was sent by the sender, which would
indicate some kind of buffer corruption is occurring. It only occurs
when the program is under heavy load (and never when running within the
debugger) - which is why I suspect the thrreading model.

Oct 21 '06 #5
I wrote:
David Levine wrote:
What kind of crashes?

I'm pretty sure that occasionally the value read from the socket reader
is different from the value that was sent by the sender, which would
indicate some kind of buffer corruption is occurring. It only occurs
when the program is under heavy load (and never when running within the
debugger) - which is why I suspect the thrreading model.
I'm delighted to say that I've found a bug in the test program which
was almost certainly causing these read errors - and it wasn't due to
multi-threading but was caused by the program running faster than the
socket could provide data. I'm sorry for misleading the group.

So I think I can take it that the dual thread concurrent reader/writer
model does indeed work in .Net. I'd still very much like to see
Micorosft document this as a valid case.

Best regards
~Rob

Oct 21 '06 #6
DC
I have used the concurrent blocking reader/writer model extensively and
confirm that it is fine. I presume you are not simply seeing the effect of
write blocks not matching read blocks? tcp (if thats what you are using)
makes no guarentee of message boudries.

"Rob Lugt" <ro*****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>
David Levine wrote:
>What kind of crashes?

I'm pretty sure that occasionally the value read from the socket reader
is different from the value that was sent by the sender, which would
indicate some kind of buffer corruption is occurring. It only occurs
when the program is under heavy load (and never when running within the
debugger) - which is why I suspect the thrreading model.

Oct 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gonçalo Rodrigues | last post by:
Hi, My setup is the following: I have socket s from which I want to read and write. So I made the following set up: There is a thread whose...
17
by: PyPK | last post by:
Hi I am looking for a simple tiff Image reader/writer in python.Can anyone point me to the right one.
5
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in...
2
by: Jonas Hei | last post by:
Is it safe to call socket.BeginSendTo and socket.BeginSendFrom on a single instance of Socket from two different threads running simultaneously? ...
10
by: Hunk | last post by:
Hi I would like some ideas on way to solve the concurrency issue. The problem is , I have an object say X which multiple readers need to access...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.