Click here to Skip to main content
15,889,462 members
Home / Discussions / C#
   

C#

 
GeneralRe: Internet Connection Pin
leppie8-Dec-02 3:34
leppie8-Dec-02 3:34 
Generalprocess and network path Pin
imran_rafique7-Dec-02 0:33
imran_rafique7-Dec-02 0:33 
GeneralRe: process and network path Pin
mikasa7-Dec-02 8:16
mikasa7-Dec-02 8:16 
GeneralRe: process and network path Pin
Burt Harris7-Dec-02 9:22
Burt Harris7-Dec-02 9:22 
GeneralDaylight Saving TIme Pin
Bugoy6-Dec-02 20:14
Bugoy6-Dec-02 20:14 
GeneralRe: Daylight Saving TIme Pin
Jon Sagara6-Dec-02 20:22
Jon Sagara6-Dec-02 20:22 
Generalmultithreading Pin
valisekkagari6-Dec-02 9:36
valisekkagari6-Dec-02 9:36 
GeneralRe: multithreading Pin
Burt Harris7-Dec-02 9:46
Burt Harris7-Dec-02 9:46 
So the hot way of doing this in C# is to use the asynchronous pattern. This uses multiple threads, but not so explicitly.

Think of it this way: You need one thread that uses Socket.Accept to listen for new connections on the port as you describe. However, when a connection comes in, you don't need to spawn another thread, let the runtime will manage threads for you.

You'll probably want to post a read in response to the connect. Do so with an async method, like NetworkStream.BeginWrite(). You pass a delegate to that method, and when the read completes, the runtime will call you back on what's called a "thread pool" thread. That callback will do some work, and post another asynchronous I/O operation instead of blocking. This way, the thread can return back to the thread pool and be used to service more than one client. This scales much better than a thread-per-client approach. Under the covers, the runtime uses a powerful (if obscure) feature Windows NT call "I/O completion ports", but as a C# programmer, it's fairly easy.

It does call for a design shift. You don't want to block thread-pool threads waiting for I/O, especially network I/O. Instead, you have to break your logic into a set of routines that gather the results from a previous I/O operation (like NetworkStream.EndRead()), do some work, and start another asynchronous operation, perhaps respond using NetworkStream.BeginWrite(). You can even do a BeginWrite follwed with a BeginRead and have both active at the same time, since the Begin methods don't wait for completion.

Warning: Having blocking (synchronous) network call nested deep in several subroutines breaks the model!

The ideal is that you have one thread-pool thread per CPU on your server. That way they can all be working, but you don't encounter excessive context (i.e. thread) switching overhead. In the real world, you typically need more than that because of unpredictable blocking I/O operations like page faults. The runtime & OS dynamically adjust the number of threads to match your program's behavior.

You don't even really need a thread to wait on the Socket.Accept function, as it has an async pair BeginAccept and EndAccept as well.

P.S. I/O Completion ports aren't available on Windows 9x & ME, but don't worry about that, the runtime simulates them if necessary.



Burt Harris
GeneralRe: multithreading Pin
valisekkagari12-Dec-02 7:44
valisekkagari12-Dec-02 7:44 
GeneralWindows Services Pin
Nic Oughton6-Dec-02 5:44
professionalNic Oughton6-Dec-02 5:44 
GeneralRe: Windows Services Pin
Burt Harris7-Dec-02 9:26
Burt Harris7-Dec-02 9:26 
GeneralRe: Windows Services Pin
Nic Oughton8-Dec-02 23:04
professionalNic Oughton8-Dec-02 23:04 
GeneralRe: Windows Services Pin
Ragavendran Vaidhyanadhan7-Dec-02 22:40
Ragavendran Vaidhyanadhan7-Dec-02 22:40 
GeneralRe: Windows Services Pin
Nic Oughton8-Dec-02 23:02
professionalNic Oughton8-Dec-02 23:02 
GeneralAdding Progress Bars to ListView Pin
athomas42195-Dec-02 21:04
athomas42195-Dec-02 21:04 
GeneralRe: Adding Progress Bars to ListView Pin
Richard Deeming5-Dec-02 23:47
mveRichard Deeming5-Dec-02 23:47 
GeneralWhy Are Mnemonic (Keys) Not Showing On Windows Forms Pin
Gaul5-Dec-02 17:33
Gaul5-Dec-02 17:33 
GeneralRe: Why Are Mnemonic (Keys) Not Showing On Windows Forms Pin
Richard Deeming5-Dec-02 23:49
mveRichard Deeming5-Dec-02 23:49 
GeneralRe: Why Are Mnemonic (Keys) Not Showing On Windows Forms Pin
Gaul6-Dec-02 16:00
Gaul6-Dec-02 16:00 
QuestionIs there a PushbackInputStream for C# or a work around? Pin
Rodney S. Foley5-Dec-02 13:53
Rodney S. Foley5-Dec-02 13:53 
GeneralLoading Windows Form dynamically into splitter Pin
femthink5-Dec-02 12:24
femthink5-Dec-02 12:24 
GeneralMail Merging with Word & C# Pin
MrEyes5-Dec-02 6:02
MrEyes5-Dec-02 6:02 
GeneralRe: Mail Merging with Word & C# Pin
Eric Gunnerson (msft)5-Dec-02 17:49
Eric Gunnerson (msft)5-Dec-02 17:49 
GeneralRe: Mail Merging with Word & C# Pin
MikeDoerfler6-Dec-02 6:19
MikeDoerfler6-Dec-02 6:19 
Generalvertically input text Pin
jirigala4-Dec-02 23:35
jirigala4-Dec-02 23:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.