473,508 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

block, end, async read/write cause delay?

Will TcpClient.GetStream().Read()/ReadByte() block until at least one byte
of data can be read?

In a Client/Server application, what does it mean at the end of stream/no
more data available? Client could send data once few seconds of minutes. Is
there an "end" at all?

In a C/S application, if server side call BeginginRead() again in EndRead()
to create a endless loop to get message from client, is this a better
approach than "one thread per client" approach?

I understand aync read/write will put job into query and later it uses
system thread pool to execute it. The question is when will it be executed?
Wait until hardware available or plus waiting thread available? If there are
too many clients, will this async approach cause too much delay and client
can not get expected response from server in timely manner? And this can
avoid by "one thread per client" mode?

Thanks!
Ryan
Jun 27 '08 #1
3 2673
On Mon, 19 May 2008 20:58:31 -0700, Ryan Liu <rl**@powercati.comwrote:
Will TcpClient.GetStream().Read()/ReadByte() block until at least one
byte
of data can be read?
Assuming the socket is in blocking mode, yes. Otherwise, you could get an
exception equivalent to the WSAEWOULDBLOCK error.
In a Client/Server application, what does it mean at the end of stream/no
more data available? Client could send data once few seconds of
minutes. Is
there an "end" at all?
The stream you get from a TCP socket will only reach the end when the
connection is closed.
In a C/S application, if server side call BeginginRead() again in
EndRead()
to create a endless loop to get message from client, is this a better
approach than "one thread per client" approach?
Yes. And this is better on the client side too. Using the async API on
the network i/o classes (well, for sure the Socket class...I'm pretty sure
TcpClient, NetworkStream, etc. all follow) will implicitly use IOCP when
available (which is on any NT-based version of Windows), and IOCP is the
most scalable API to use with network i/o.

I personally find the async API more convenient and easier to understand,
but then I'm odd that way. :) YMMV.
I understand aync read/write will put job into query and later it uses
system thread pool to execute it. The question is when will it be
executed?
Wait until hardware available or plus waiting thread available? If there
are
too many clients, will this async approach cause too much delay and
client
can not get expected response from server in timely manner? And this can
avoid by "one thread per client" mode?
As I wrote above, IOCP is the most scalable approach, and that's what the
async API uses. It works by having a pool of threads waiting on i/o
completions. One thread can handle i/o completions for any number of
sockets. This means that as i/o completions are queued by the OS, the
same thread can just keep pulling the completions off the queue and
processing them, avoiding a thread context switch just to process a
different socket.

As for when the thread will get to run, it will mostly run according to
the usual Windows scheduling mechanism. The IOCP threads all sit and wait
on a queue. As soon as something's in the queue, the threads become
runnable. They are scheduled according to the normal round-robin
rotation, so once the first runnable IOCP thread gets to run, it dequeues
the completion and processes it. If there are still i/o completions in
the queue, that one thread will keep dequeuing them. If another CPU core
becomes available and a second IOCP thread reaches the head of the line in
the round-robin scheme, then that second thread will dequeue an i/o
completion and process it.

Each IOCP thread, once it gets a chance to run, will not yield until
either the queue is empty or it's pre-empted. In this way, the threads
are assured of consuming their entire thread quantum as long as there's
actually work to be done.

Finally, Windows knows that IOCP is special and actually does manage
thread scheduling and some other things to help ensure that the CPUs are
used most efficiently with IOCP. For example, if I recall correctly the
Windows scheduler won't switch to another IOCP thread just because one
IOCP thread is done with its quantum. It's smart enough that, once an
IOCP thread gets to run, that thread will keep running through multiple
quantums as long as there aren't non-IOCP threads that are runnable.
Again, this minimizes context switching.

Pete
Jun 27 '08 #2
Thanks a lot, Peter.

Just one more question :

"Peter Duniho" <Np*********@nnowslpianmk.comдÈëÏûÏ¢ÐÂÎÅ:op****** *********@petes-computer.local...
On Mon, 19 May 2008 20:58:31 -0700, Ryan Liu <rl**@powercati.comwrote:
>Will TcpClient.GetStream().Read()/ReadByte() block until at least one
byte
of data can be read?

Assuming the socket is in blocking mode, yes. Otherwise, you could get an
exception equivalent to the WSAEWOULDBLOCK error.

What does Block meaning? Will it consume resouces like CPU? Will it occpuy a
thread? Is it just a hardware thing, e.g. wait for hardware to notify or
actually use thread poll status of stream?

>In a Client/Server application, what does it mean at the end of stream/no
more data available? Client could send data once few seconds of
minutes. Is
there an "end" at all?

The stream you get from a TCP socket will only reach the end when the
connection is closed.
>In a C/S application, if server side call BeginginRead() again in
EndRead()
to create a endless loop to get message from client, is this a better
approach than "one thread per client" approach?

Yes. And this is better on the client side too. Using the async API on
the network i/o classes (well, for sure the Socket class...I'm pretty sure
TcpClient, NetworkStream, etc. all follow) will implicitly use IOCP when
available (which is on any NT-based version of Windows), and IOCP is the
most scalable API to use with network i/o.

I personally find the async API more convenient and easier to understand,
but then I'm odd that way. :) YMMV.
>I understand aync read/write will put job into query and later it uses
system thread pool to execute it. The question is when will it be
executed?
Wait until hardware available or plus waiting thread available? If there
are
too many clients, will this async approach cause too much delay and
client
can not get expected response from server in timely manner? And this can
avoid by "one thread per client" mode?

As I wrote above, IOCP is the most scalable approach, and that's what the
async API uses. It works by having a pool of threads waiting on i/o
completions. One thread can handle i/o completions for any number of
sockets. This means that as i/o completions are queued by the OS, the
same thread can just keep pulling the completions off the queue and
processing them, avoiding a thread context switch just to process a
different socket.

As for when the thread will get to run, it will mostly run according to
the usual Windows scheduling mechanism. The IOCP threads all sit and wait
on a queue. As soon as something's in the queue, the threads become
runnable. They are scheduled according to the normal round-robin
rotation, so once the first runnable IOCP thread gets to run, it dequeues
the completion and processes it. If there are still i/o completions in
the queue, that one thread will keep dequeuing them. If another CPU core
becomes available and a second IOCP thread reaches the head of the line in
the round-robin scheme, then that second thread will dequeue an i/o
completion and process it.

Each IOCP thread, once it gets a chance to run, will not yield until
either the queue is empty or it's pre-empted. In this way, the threads
are assured of consuming their entire thread quantum as long as there's
actually work to be done.

Finally, Windows knows that IOCP is special and actually does manage
thread scheduling and some other things to help ensure that the CPUs are
used most efficiently with IOCP. For example, if I recall correctly the
Windows scheduler won't switch to another IOCP thread just because one
IOCP thread is done with its quantum. It's smart enough that, once an
IOCP thread gets to run, that thread will keep running through multiple
quantums as long as there aren't non-IOCP threads that are runnable.
Again, this minimizes context switching.

Pete

Jun 27 '08 #3
On Tue, 20 May 2008 04:22:49 -0700, Ryan Liu <rl**@powercati.comwrote:
>Assuming the socket is in blocking mode, yes. Otherwise, you could get
an
exception equivalent to the WSAEWOULDBLOCK error.

What does Block meaning? Will it consume resouces like CPU? Will it
occpuy a
thread? Is it just a hardware thing, e.g. wait for hardware to notify or
actually use thread poll status of stream?
To "block" means for the thread to become unrunnable, and thus stop
executing, and wait for something to happen.

A thread that is unrunnable doesn't consume CPU time. To "occupy a
thread" is ambiguous, but if you mean that the thread is stuck and can't
do anything else, then yes...the thread is occupied. But it's not
runnable, so the only resources that are consumed are the memory-based
resources allocated to that thread (and if a thread remains unrunnable for
long enough, those resources will get moved from physical memory to the
swap file, removing contention for that last high-value resource).

It's not possible to answer the question about interrupt vs polling in a
general sort of way. However, you can depend on the OS using
interrupt-driven management as much as possible, and where polling exists
it will be at a very low level in the OS, almost never in a way that would
affect your own code. When your own code blocks on i/o, you can rest
assured that you're essentially consuming no CPU resources at all.

Pete
Jun 27 '08 #4

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

Similar topics

0
1740
by: Paul Clinch | last post by:
Has anyone tried the echod-async.py example in m2crypto-0.12/demo/ssl? I am only connecting one client, the echo.py example in the same directory. Although the synchronous and threading examples...
6
10441
by: Vanessa | last post by:
I have a question regarding async mode for calling Microsoft.XMLHTTP object. Microsoft.XMLHTTP hangs the IE once in a while suddenly, but it will work again after half an hour or so without doing...
2
2615
by: Tom Vandeplas | last post by:
Hi all, When using an asynchronous API call (in my case WriteFileEx) in a thread it seems to become blocking instead of non-blocking (see details below). Can somebody explain me what is...
5
4026
by: Homa | last post by:
Hi all, Can anyone give me some links about how to do an async web service call from aspx and display a temperary page before the web service returns? Thanks, Homa Wong
2
1527
by: Leneise44 | last post by:
Does the new async features within asp.net 2.0 and ado.net 2.0 render the async application block (1.1) extinct? What would be the advantages of using the async application block with 2.0? Seems...
4
3549
by: Greg Young | last post by:
Ok so I think everyone can agree that creating buffers on the fly in an async socket server is bad ... there is alot of literature available on the problems this will cause with the heap. I am...
11
8580
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
3
3563
by: Ryan Liu | last post by:
Hi, Is Async I/O (e.g. NetworkStream.Begin/End Read/Write) always better than synchronous I/O? At least as good? When I don't concern about easy or difficult to write code, should I always...
1
20548
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
0
7135
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...
1
7067
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
7505
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...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.