473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 6427
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 only job is to read. Any data read (from recv call)...
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 which it is called? I've been testing this and...
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? This is required because I need to listen on a...
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 and also to update. They do so by say two...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.