PC Review


Reply
Thread Tools Rate Thread

Threads with Sockets in them? or Asynchronous Sockets?

 
 
Ronodev.Sen@gmail.com
Guest
Posts: n/a
 
      22nd Feb 2006
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 way - BeginReceive - then
pass to the OnClient Connected handler, which calls a Wait For Data
function.

the way i see it , there's no way for me to tell WHICH socket is being
processed - which poses a problem since i also need to LOG any
successes / failures to TEXT FILE (not EventLog , sadly)
i'd have to write a whole NEW set of handlers to send the data BACK to
the socket.
if i call these functions from within the "WaitForData" function - i'd
PROBABLY be nixing the benefits of ASYNCHRONICITY... wouldn't i ?

on a cursory glance, would this appear to be a good candidate where i
process my own threads (keeping them in a synchronised arraylist) for
each socket , instead of taking the advantages of (what i understand
in my LIMITED knowledge so far) anonymous threads connecting to
anonymous sockets ???

thanks in advance!

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      22nd Feb 2006
Ronodev,
This kind of business scenario is a good candidate for Asynchronous socket
operations. You have a State object that can contain an instance of the
socket itself (or a custom stateobject class of your own design) which is
passed as a parameter.

You can retrieve this object in the callback (endrecieve) and therefore you
can gain access to the socket that pertains to the particular work item.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"(E-Mail Removed)" wrote:

> 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 way - BeginReceive - then
> pass to the OnClient Connected handler, which calls a Wait For Data
> function.
>
> the way i see it , there's no way for me to tell WHICH socket is being
> processed - which poses a problem since i also need to LOG any
> successes / failures to TEXT FILE (not EventLog , sadly)
> i'd have to write a whole NEW set of handlers to send the data BACK to
> the socket.
> if i call these functions from within the "WaitForData" function - i'd
> PROBABLY be nixing the benefits of ASYNCHRONICITY... wouldn't i ?
>
> on a cursory glance, would this appear to be a good candidate where i
> process my own threads (keeping them in a synchronised arraylist) for
> each socket , instead of taking the advantages of (what i understand
> in my LIMITED knowledge so far) anonymous threads connecting to
> anonymous sockets ???
>
> thanks in advance!
>
>

 
Reply With Quote
 
 
 
 
William Stacey [MVP]
Guest
Posts: n/a
 
      22nd Feb 2006
Check this out:
http://www.eecs.harvard.edu/~mdw/papers/seda-sosp01.pdf

I am using this pattern now for something and it seems to work very well.
After reading this, your design may be something like:
1) ReadStage - sync or async socket read pattern within the stage. Multiple
threads if blocking reads.
2) WriteStage - probably blocking writes ok here. Multiple threads.
Naturally, could use async socket writes as well.
3) LogStage - Do any logs here - single thread probably ok.
4) StateObject - includes TcpClient and any other shared state for client.
This passes between stages.
5) ServerStage - Your main server logic. Number of threads depends. One may
due. Probably no more then 1 + #CPUs.

It is a different design then what one would normally use with threaded or
async servers, but basically your just doing the async yourself, but all
your app logic feels like sync - so it is easy to reason about and you don't
have callbacks all over the place. Is also loosely coupled, which has other
advantages for your app over its life.

--
William Stacey [MVP]

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| 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 way - BeginReceive - then
| pass to the OnClient Connected handler, which calls a Wait For Data
| function.
|
| the way i see it , there's no way for me to tell WHICH socket is being
| processed - which poses a problem since i also need to LOG any
| successes / failures to TEXT FILE (not EventLog , sadly)
| i'd have to write a whole NEW set of handlers to send the data BACK to
| the socket.
| if i call these functions from within the "WaitForData" function - i'd
| PROBABLY be nixing the benefits of ASYNCHRONICITY... wouldn't i ?
|
| on a cursory glance, would this appear to be a good candidate where i
| process my own threads (keeping them in a synchronised arraylist) for
| each socket , instead of taking the advantages of (what i understand
| in my LIMITED knowledge so far) anonymous threads connecting to
| anonymous sockets ???
|
| thanks in advance!
|


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Async sockets vs synch sockets and threads nyhetsgrupper@gmail.com Microsoft C# .NET 4 20th Sep 2006 04:06 AM
Thread safety of asynchronous sockets, also - documentation vs reality chris gouldie via .NET 247 Microsoft Dot NET Framework 8 12th Jun 2005 12:49 AM
Asynchronous operations with System.Net.Sockets =?Utf-8?B?TmF0ZSBB?= Microsoft Dot NET Framework 1 30th Apr 2005 12:02 AM
Asynchronous Sockets and Threading Darren Thomas Microsoft Dot NET 0 11th Nov 2003 08:45 PM
Asynchronous Sockets in .NET Corne Oosthuizen Microsoft Dot NET 3 5th Nov 2003 01:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:33 AM.