HighTechTalks DotNet Forums  

Async TCPClients

Dotnet Distributed Applications microsoft.public.dotnet.distributed_apps


Discuss Async TCPClients in the Dotnet Distributed Applications forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Shak
 
Posts: n/a

Default Async TCPClients - 06-19-2006 , 06:40 AM






Hi all,

Three questions really:

1) The async call to the networkstream's endread() (or even endxxx() in
general) blocks. Async calls are made on the threadpool - aren't we advised
not to cause these to block?

2) You can connect together a binaryreader to a networkstream:

BinaryReader reader = new BinaryReader(stream);

And then read an int from it:

int i = reader.ReadInt32();

And vice versa.

Since there are no calls to beginread/endread, I presume this isn't a
synchronous call. Is there any way to make it async?

3) I'm trying to implement the message mechanism described here:

http://msdn.microsoft.com/library/de...rp09182003.asp

In order to answer question 2), I'm going to read the the messagetype
asynchronously, but the rest of the message synchrounously in the callback
(by tying together the binaryreader and networkstream). Is it a good idea to
do mix the two like this? What happens if the network lags, or bytes go
missing?

Thanks!

Shak







Reply With Quote
  #2  
Old   
Peter Ritchie
 
Posts: n/a

Default RE: Async TCPClients - 06-21-2006 , 11:29 AM






EndRead isn't an asynchronous call... The BeginRead is the asynchronous call.

BinaryReader is synchronous, so you'd have to wait until you got all the
data you expected from your network stream before using BinaryReader.

The docs for NetworkStream.BeginRead don't really show using asynchronous to
it's full potential. (i.e. calling BeginRead then calling WaitOne kind of
defeats the purpose...).
See the docs for NetworkStream.EndRead to see the other half the
asynchronous part (the callback). The basic pattern is BeginRead, do your
processing in your async callback calling EndRead and another BeginRead if
there is more data. When there is no more data you can process the bytes
that where received. It's here where you can use BinaryReader. If you've
got a byte array, you can simply pass a MemoryStream object to the
BinaryReader constructor and starting using the BinaryReader object. For
example, if the bytes you've received from the NetworkStrream are stored in
an object defined as "byte[] bytes;" then you can create BinaryReader object
to read those bytes as follows: "BinaryReader br = new BinaryReader(new
MemoryStream(bytes));" If you need to seek into the stream, you'll probably
can to store a reference to the MemoryStream object...



--
http://www.peterRitchie.com/


"Shak" wrote:

Quote:
Hi all,

Three questions really:

1) The async call to the networkstream's endread() (or even endxxx() in
general) blocks. Async calls are made on the threadpool - aren't we advised
not to cause these to block?

2) You can connect together a binaryreader to a networkstream:

BinaryReader reader = new BinaryReader(stream);

And then read an int from it:

int i = reader.ReadInt32();

And vice versa.

Since there are no calls to beginread/endread, I presume this isn't a
synchronous call. Is there any way to make it async?

3) I'm trying to implement the message mechanism described here:

http://msdn.microsoft.com/library/de...rp09182003.asp

In order to answer question 2), I'm going to read the the messagetype
asynchronously, but the rest of the message synchrounously in the callback
(by tying together the binaryreader and networkstream). Is it a good idea to
do mix the two like this? What happens if the network lags, or bytes go
missing?

Thanks!

Shak








Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.