Re: Objects Persist Across Threads - Please Help.
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Sat, 26 Jan 2008 19:47:44 -0800
On Sat, 26 Jan 2008 16:43:59 -0800, pbd22 <dushkin@xxxxxxxxx> wrote:
Hi.
I have a telnet application (which many of you have already helped me
with, thank you).
I am now running into a bit of a serious design problem.
It seems that each new thread (telnet connection) is "connected" to
the pre-existing thread.
[...]
Can this be addressed conceptually or should I dump my code here (I
wasn't really sure what to post code-wise).
I don't think there's any point posting code. If you do, you would not post all of it. You would reduce the code to the bare minimum required to reproduce the problem and then post that reduced version.
That said, it sounds clear that you are using a single object instance to handle all of the data for all of the users. Obviously, this is wrong.
You should create a data structure that will contain whatever data is unique for each user. Then for each user connected, you'll create an instance of that data structure and reference it whenever you are processing a particular user.
For what it's worth, you should not be creating a new thread for each connected user. This will work okay for relatively small numbers of users, but it won't scale at all as the number of connections increases, and the code will work better with a different mechanism even for smaller users.
I prefer the asynchronous APIs, involving the methods that start with "Begin" and "End". With these methods, you pass an object reference as a "state" value to the "Begin" method. This value is then put into the IAsyncResult instance for future reference, as the IAsyncResult.AsyncState property. So, for example, if you use the callback technique (IMHO the way that is usually best), you can retrieve your object instance from the IAsyncResult passed to your callback method. Just cast it back to your own class, and you've got your per-user data for the operation (likely including the Socket reference as part of this data structure, which you'd need in the callback in order to call the "End" method, and a buffer reference, so that you can retrieve received data once an operation has completed, to name a couple of things that commonly go into a data structure like this).
Pete
.
- Follow-Ups:
- References:
- Objects Persist Across Threads - Please Help.
- From: pbd22
- Objects Persist Across Threads - Please Help.
- Prev by Date: Re: directory recursive c# example stops before completing scan
- Next by Date: Re: Passing Parameters to an Exe
- Previous by thread: Objects Persist Across Threads - Please Help.
- Next by thread: Re: Objects Persist Across Threads - Please Help.
- Index(es):
Relevant Pages
|