473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scalability of asynchronous sockets?

I have a legacy application written in C that I'm trying to convert to
C#. It processes a very large amount of data from many clients
(actually, upstream servers - this is a mux) simultaneously.

I've read through what must be dozens of ways to do socket
communication in C#, and it seems they all devolve into three basic
options - Socket.Select, IOCP through a native interface, and
Asynchronous callbacks. I'm fine using Asynchronous callbacks, which
all the documentation and articles I've read seem to indicate is the
most scalable solution. My concern is over the ThreadPool involved.

I might have 40-60 connections talking simultaneously, and during peak
periods that can be 150+. Note that this ran fine without threads by
handling the data in round-robin fashion, so it can handle blocking if
that's what will happen when the thread pool gets starved. What it
can't tolerate is ADDITIONAL performance degradation. That is, if
hitting that magic 25-thread limit is going to make things not just N
ticks slower, but N^2 ticks slower as the system struggles to do some
thread magic I don't know about, then I'll have issues.

So, some questions:
What, exactly, will happen when I starve the ThreadPool?

Is there a list somewhere of other I/O (like file) activity I'll affect
by doing this?

If I have a dual-proc + HT box, 4 virtual processors, does this mean MY
application gets 100 theoretical asynch threads to use (25 per
processor)? Scalability via CPU is just fine with me.

Will I affect other applications running on the box (is the ThreadPool
shared across all of .Net, or just my application?)

Should I be trying to write a custom ThreadPool? If so, are there any
pointers on how to tell socket asynchronous functions to use the custom
pool? Or do I just make real threads and use blocking I/O?

Is there housekeeping overhead while the sockets are asleep? (If I have
3000 clients connected but very few talking, is that a problem?)

Do I really need a StateObject class? I have a wrapper class that does
all the dirty work described above. Any reason I can't just keep the
byte[] buffer and other elements in the wrapper and use "this" when I
call BeginReceive?

Any reason I can't just reference my instance variables without even
bothering with the StateObject indirection? I was going to instantiate
a new wrapper for every connection.

Thanks,
Chad

Jan 10 '06 #1
4 3800
> What, exactly, will happen when I starve the ThreadPool?
WorkItems that you've queued into it will not receive thread until thread
pool threads will become free.
Additionaly, how do you perform your network I/O?
ThreadPool has 2 types of threads : WorkerThreads and CompletionPort threads

AFAIK BeginSend/BeginReceive asycn socket methods use CompletionPort
threads, and their number
in Thread Pool is large.

Watch for word wrap
(
http://msdn.microsoft.com/library/de...ogthrepool.asp )
( http://msdn.microsoft.com/msdnmag/is...rmanceSockets/ )

Is there a list somewhere of other I/O (like file) activity I'll affect
by doing this?
If server most of the time will be completing your I/O ( processor and
memory usage will be high )
then it may affect other I/O types or simply downgrage whole server
performance.
Will I affect other applications running on the box (is the ThreadPool
shared across all of .Net, or just my application?)
ThreadPool is static that is it operates on the AppDomain. Every AppDomain
has it own ThreadPool
Should I be trying to write a custom ThreadPool? Only if current implementation doesn't suit your needs well.
If so, are there any pointers on how to tell socket asynchronous functions
to use the custom
pool? Or do I just make real threads and use blocking I/O?
AFAIK ThreadPool is used internally if you're using
Socket.BeginRec eive/BeginSend async methods
Is there housekeeping overhead while the sockets are asleep? (If I have
3000 clients connected but very few talking, is that a problem?) No it is not a problem, or at least it has not to be a problem. However, it
is more efficient to keep
the connection number not too large ( every connection allocates system
resources - memory for instance ).
Do I really need a StateObject class? I have a wrapper class that does
all the dirty work described above. Any reason I can't just keep the
byte[] buffer and other elements in the wrapper and use "this" when I
call BeginReceive?
Then you will have to provide one wrapper for each connection.

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

<ta******@gmail .com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .I have a legacy application written in C that I'm trying to convert to
C#. It processes a very large amount of data from many clients
(actually, upstream servers - this is a mux) simultaneously.

I've read through what must be dozens of ways to do socket
communication in C#, and it seems they all devolve into three basic
options - Socket.Select, IOCP through a native interface, and
Asynchronous callbacks. I'm fine using Asynchronous callbacks, which
all the documentation and articles I've read seem to indicate is the
most scalable solution. My concern is over the ThreadPool involved.

I might have 40-60 connections talking simultaneously, and during peak
periods that can be 150+. Note that this ran fine without threads by
handling the data in round-robin fashion, so it can handle blocking if
that's what will happen when the thread pool gets starved. What it
can't tolerate is ADDITIONAL performance degradation. That is, if
hitting that magic 25-thread limit is going to make things not just N
ticks slower, but N^2 ticks slower as the system struggles to do some
thread magic I don't know about, then I'll have issues.

So, some questions:
What, exactly, will happen when I starve the ThreadPool?

Is there a list somewhere of other I/O (like file) activity I'll affect
by doing this?

If I have a dual-proc + HT box, 4 virtual processors, does this mean MY
application gets 100 theoretical asynch threads to use (25 per
processor)? Scalability via CPU is just fine with me.

Will I affect other applications running on the box (is the ThreadPool
shared across all of .Net, or just my application?)

Should I be trying to write a custom ThreadPool? If so, are there any
pointers on how to tell socket asynchronous functions to use the custom
pool? Or do I just make real threads and use blocking I/O?

Is there housekeeping overhead while the sockets are asleep? (If I have
3000 clients connected but very few talking, is that a problem?)

Do I really need a StateObject class? I have a wrapper class that does
all the dirty work described above. Any reason I can't just keep the
byte[] buffer and other elements in the wrapper and use "this" when I
call BeginReceive?

Any reason I can't just reference my instance variables without even
bothering with the StateObject indirection? I was going to instantiate
a new wrapper for every connection.

Thanks,
Chad

Jan 10 '06 #2
The IOC threadpool is 1000 on v1.1 and v2.0.

Willy.

"Vadym Stetsyak" <va*****@ukr.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
|> What, exactly, will happen when I starve the ThreadPool?
| WorkItems that you've queued into it will not receive thread until thread
| pool threads will become free.
| Additionaly, how do you perform your network I/O?
| ThreadPool has 2 types of threads : WorkerThreads and CompletionPort
threads
|
| AFAIK BeginSend/BeginReceive asycn socket methods use CompletionPort
| threads, and their number
| in Thread Pool is large.
|
| Watch for word wrap
| (
|
http://msdn.microsoft.com/library/de...ogthrepool.asp )
| ( http://msdn.microsoft.com/msdnmag/is...rmanceSockets/ )
|
|
| > Is there a list somewhere of other I/O (like file) activity I'll affect
| > by doing this?
|
| If server most of the time will be completing your I/O ( processor and
| memory usage will be high )
| then it may affect other I/O types or simply downgrage whole server
| performance.
|
| > Will I affect other applications running on the box (is the ThreadPool
| > shared across all of .Net, or just my application?)
|
| ThreadPool is static that is it operates on the AppDomain. Every AppDomain
| has it own ThreadPool
|
| > Should I be trying to write a custom ThreadPool?
| Only if current implementation doesn't suit your needs well.
|
| > If so, are there any pointers on how to tell socket asynchronous
functions
| > to use the custom
| > pool? Or do I just make real threads and use blocking I/O?
|
| AFAIK ThreadPool is used internally if you're using
| Socket.BeginRec eive/BeginSend async methods
|
| > Is there housekeeping overhead while the sockets are asleep? (If I have
| > 3000 clients connected but very few talking, is that a problem?)
| No it is not a problem, or at least it has not to be a problem. However,
it
| is more efficient to keep
| the connection number not too large ( every connection allocates system
| resources - memory for instance ).
|
| > Do I really need a StateObject class? I have a wrapper class that does
| > all the dirty work described above. Any reason I can't just keep the
| > byte[] buffer and other elements in the wrapper and use "this" when I
| > call BeginReceive?
|
| Then you will have to provide one wrapper for each connection.
|
| --
| Vadym Stetsyak aka Vadmyst
| http://vadmyst.blogspot.com
|
| <ta******@gmail .com> wrote in message
| news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
| >I have a legacy application written in C that I'm trying to convert to
| > C#. It processes a very large amount of data from many clients
| > (actually, upstream servers - this is a mux) simultaneously.
| >
| > I've read through what must be dozens of ways to do socket
| > communication in C#, and it seems they all devolve into three basic
| > options - Socket.Select, IOCP through a native interface, and
| > Asynchronous callbacks. I'm fine using Asynchronous callbacks, which
| > all the documentation and articles I've read seem to indicate is the
| > most scalable solution. My concern is over the ThreadPool involved.
| >
| > I might have 40-60 connections talking simultaneously, and during peak
| > periods that can be 150+. Note that this ran fine without threads by
| > handling the data in round-robin fashion, so it can handle blocking if
| > that's what will happen when the thread pool gets starved. What it
| > can't tolerate is ADDITIONAL performance degradation. That is, if
| > hitting that magic 25-thread limit is going to make things not just N
| > ticks slower, but N^2 ticks slower as the system struggles to do some
| > thread magic I don't know about, then I'll have issues.
| >
| > So, some questions:
| > What, exactly, will happen when I starve the ThreadPool?
| >
| > Is there a list somewhere of other I/O (like file) activity I'll affect
| > by doing this?
| >
| > If I have a dual-proc + HT box, 4 virtual processors, does this mean MY
| > application gets 100 theoretical asynch threads to use (25 per
| > processor)? Scalability via CPU is just fine with me.
| >
| > Will I affect other applications running on the box (is the ThreadPool
| > shared across all of .Net, or just my application?)
| >
| > Should I be trying to write a custom ThreadPool? If so, are there any
| > pointers on how to tell socket asynchronous functions to use the custom
| > pool? Or do I just make real threads and use blocking I/O?
| >
| > Is there housekeeping overhead while the sockets are asleep? (If I have
| > 3000 clients connected but very few talking, is that a problem?)
| >
| > Do I really need a StateObject class? I have a wrapper class that does
| > all the dirty work described above. Any reason I can't just keep the
| > byte[] buffer and other elements in the wrapper and use "this" when I
| > call BeginReceive?
| >
| > Any reason I can't just reference my instance variables without even
| > bothering with the StateObject indirection? I was going to instantiate
| > a new wrapper for every connection.
| >
| > Thanks,
| > Chad
| >
|
|
Jan 10 '06 #3
OK, thanks for the tips. One more question. What is the behavior of
BeginReceive/EndReceive if I close the socket? Are they guaranteed not
to be called? The documentation makes it appear as though I can't
cancel the request in any way.

This is mainly a concern for transmissions. I want to make a queue and
do asynchronous sends. If I close the connection, though, I want to
wipe out the queue. The trouble is, I may (will) have already called
BeginSend and given the address of one of these buffers. If I then
release the buffer, I'm concerned that something will crash somewhere.

Also, I was hoping to preallocate an "empty" list of pending queue
nodes to increase performance. If necessary I can retain the object I
passed to EndReceive somewhere but that gets tricky.

So, what will happen to pending BeginReceive/EndReceive events if the
socket is disconnected, either by the remote, or by me calling
Shutdown() and/or Close()?

Thanks,
Chad

Jan 10 '06 #4
> So, what will happen to pending BeginReceive/EndReceive events if the
socket is disconnected, either by the remote, or by me calling
Shutdown() and/or Close()?
EndReceive/EndSend will return either exception or SocketError ( .NET 2.0 )
The error or exception will have errornumber. if connected was closed
gracefully
EndReceive will return 0 without exception or error.

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

<ta******@gmail .com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. . OK, thanks for the tips. One more question. What is the behavior of
BeginReceive/EndReceive if I close the socket? Are they guaranteed not
to be called? The documentation makes it appear as though I can't
cancel the request in any way.

This is mainly a concern for transmissions. I want to make a queue and
do asynchronous sends. If I close the connection, though, I want to
wipe out the queue. The trouble is, I may (will) have already called
BeginSend and given the address of one of these buffers. If I then
release the buffer, I'm concerned that something will crash somewhere.

Also, I was hoping to preallocate an "empty" list of pending queue
nodes to increase performance. If necessary I can retain the object I
passed to EndReceive somewhere but that gets tricky.

So, what will happen to pending BeginReceive/EndReceive events if the
socket is disconnected, either by the remote, or by me calling
Shutdown() and/or Close()?

Thanks,
Chad

Jan 10 '06 #5

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

Similar topics

3
2804
by: Corne Oosthuizen | last post by:
I'm writing a Telnet Server application using Asynchronous sockets. I spawn a listener thread to handel incomming connections and create a separate client socket for each new connection. I then set the new client socket to BeginReceive(). My problem: When two client socket connections send data
3
2471
by: Matthew King | last post by:
Hi all I've written a asynchronous socket client class, but i've found that in order to consume it I have to use events, and cannot simply for example SocketClient client = new SocketClient(110, "some.server.com") client.Connect() client.SendData("Hello World") Instead I have to wait for the async method to raise a Connected event, and...
9
8658
by: Michael Lindsey | last post by:
I need to write a server app to send images to client GUIs that are outside of the server's domain. The client will have the file system path to the image but can not access the file system. I am trying to decide if I should use remoting vs. writing a server that uses networkstreams. I have read that networkstreams\tcp programming should...
1
3358
by: Assaf Shemesh | last post by:
Hi, I'm having a problem with asynchronous HttpWebRequest. It's a simple http client-server. For most of my users it works fine. However, some of them get the exception: System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: The attempted operation is not supported for the type of object...
4
2753
by: Macca | last post by:
I am writing an application that uses asynchronous sockets to get data over ethernet from embedded devices, up to 30 concurrent devices.(These devices are written in C). My application implements an asychronous socket server while the embedded devices are the clients When the data comes in over the socket it is eventually passed into a...
2
2173
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send it back to the SAME socket - the data isnt a large value (measured in bytes rather than MB or GB) i TRIED thinking of this in the Asynchronous...
4
3592
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket client and asynchronous socket server example code provided in the .NET framework developers guide is a great start but I have not dealt with...
2
3413
by: Nicolas Le Gland | last post by:
Hello everyone here. This is my first post in this newsgroup, I hope I won't be to much off-topic. Feel free to redirect me to any better group. I am getting strange timing issues when failing to asynchronously connect sockets on closed or filtered ports, but I'm quite unsure if this is a PHP issue or my misunderstanding, as it seems...
0
1261
by: alan | last post by:
Hello all, I'd like to ask recommendations about a "good" generic asynchronous I/O library for C++. By generic I mean, something I can use even on files, stdin/stdout, and sockets. I've seen boost::asio, and also ioxxx, but most of the examples I've seen with them are about sockets. Can they be used for general i/o (files, terminal)? Can...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5984
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4960
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.