472,986 Members | 2,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

Threads with Sockets in them? or Asynchronous Sockets?

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!

Feb 22 '06 #1
2 2117
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


"Ro*********@gmail.com" 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!

Feb 22 '06 #2
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]

<Ro*********@gmail.com> wrote in message
news:11********************@g44g2000cwa.googlegrou ps.com...
| 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!
|
Feb 22 '06 #3

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

Similar topics

6
by: Zunbeltz Izaola | last post by:
Hi, I have the following problem. I'm developing a GUI program (wxPython). This program has to comunicate (TCP) whit other program that controls a laboratory machine to do a measurement. I...
14
by: Michael C | last post by:
Hi all, I'm now trying to update a status indicator via a Timer control on a worker thread. The status indicator is just a label on the main form, but I'm having trouble figuring out exactly...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
6
by: roblugt | last post by:
I have what I imagine is a well-known .Net networking problem, but even though I've Googled for some time I've not yet come across a thread where this has been fully explained... There is a...
4
by: billie | last post by:
Hi all. I'm (re)writing an FTP server application by using asyncore/asynchat modules. FTP tipically got two different channels: command and data. I'm succesfully managing command channel through...
2
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
7
by: Brendon Costa | last post by:
Hi all, I have a small python project i am working on. Basically i always have two threads. A "Read" thread that sits in a loop reading a line at a time from some input (Usually stdin) and then...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.