Multithreading Question

Hi,

I am writing a multithreaded server application.

I have written a class to handle thread(CThread , say) activities like 
creating, suspending, resuming threads etc. I have derived a class from 
CThread (CMainThread, say) and a class (CClientThread, say) to handle 
client connection which is also derived from CThread. In CMainThread 
class i am creating a thread and waiting for incomming connections. As 
soon as a connection came i am creating an object for CClientThread and 
call the function to create a thread and so on.

code flow:

1. Start the main listening thread.
2. wait for a connection.
3. connection received. start a client thread.
	like this -> clientObj = new CClientThread;
		     clientObj->StartClientThread(pArg);
4. Go to step 2.

Problem is that, how i can delete the "clientObj" object to aoid memory 
leaks? where this should happen? or any other preferred approach is 
there to do this task correctly. I do not want to write the 
mainlistening thread and client thread code in the same class. I want 
them in separate classes.

Anybody can give me some idea how to do this correctly.

Thanks
Sreeram

0
8/8/2003 4:28:04 AM
vc.mfc 33608 articles. 0 followers. Follow

16 Replies
1010 Views

Similar Articles

[PageSpeed] 38

  Sreeram,

  My approach would be to have the CClientThread class manage
instances of itself.  This would allow CMainThread access to
the data as well as allowing instances of CClientThread to 
know about each other, should that become necessary.  I've 
done this many times before.

  CClientThread would need a small group of management methods
and data members.  Basically, you want some form of set (array,
linked list, STL array, etc) of pointers to each instance
of CClientThread.  You would probably want to keep this information
protected and have accessors.

  I generally create a static method to kill all instances.  It knows
how to find all threads and ask them to die (or kill them outright).

  The ctor and dtor of CClientThread manage the set of instance pointers.
Thus when an instance destroys itself everything gets managed on its
own.  You could provide callbacks or virtual overrides to allow the
parent controlling object (main or CMainThread) to be informed when
important events occur.

  I tend to approach the design a bit differently.  Many years ago
I created ISocket, IClientSocket, IServerSocket, 
IMultiprotocolServerSocket, and so on as a basis for my communications.
There are also Serial and IPC variants.  From these I derive classes
that embody the conversations.  Take email as an example.  I created
ISMTPClient<> and ISMTPServer<> template classes.  Writing just the
client/server sides is an easy task that can be seperated from the
delivery tasks.  It also allowed several rather novice programmers
to reuse my code to handle conversations in their programs.  We
were doing cross platform communications.  I had used the code under
DOS, Unix, and OS/2.  These people were from the AIX world and didn't
seem to handle protocols too well (just a bit green).  I handed them
the Unix variant of my ISocket class and all the client/server classes
they needed.  Since I was also in charge of the protocol (remote FAXing)
it was a simple matter to write both sides of the conversation at
once -- the IxxxServer<> and IxxxClient<> portions.  They didn't have
to do anything but compile the code.  Saved weeks trying to teach them 
TCP/IP, Network Byte Ordering, and all the other things they needed to
know to develop a fully debugged and tested application.  I'll back off
and let you think about how you might do it next time.  All this stuff
didn't come into being overnight.  Porting from the Unix world to Windows
was a bit tough but the classes and templates held together -- only one
class and one template needed changing.

  G'night,

  David

On Fri, 8 Aug 2003 04:28:04 UTC, Sreeram <sreeram0425@netscape.net> wrote:

> Hi,
> 
> I am writing a multithreaded server application.
> 
> I have written a class to handle thread(CThread , say) activities like 
> creating, suspending, resuming threads etc. I have derived a class from 
> CThread (CMainThread, say) and a class (CClientThread, say) to handle 
> client connection which is also derived from CThread. In CMainThread 
> class i am creating a thread and waiting for incomming connections. As 
> soon as a connection came i am creating an object for CClientThread and 
> call the function to create a thread and so on.
> 
> code flow:
> 
> 1. Start the main listening thread.
> 2. wait for a connection.
> 3. connection received. start a client thread.
> 	like this -> clientObj = new CClientThread;
> 		     clientObj->StartClientThread(pArg);
> 4. Go to step 2.
> 
> Problem is that, how i can delete the "clientObj" object to aoid memory 
> leaks? where this should happen? or any other preferred approach is 
> there to do this task correctly. I do not want to write the 
> mainlistening thread and client thread code in the same class. I want 
> them in separate classes.
> 
> Anybody can give me some idea how to do this correctly.
> 
> Thanks
> Sreeram
> 


0
8/8/2003 5:23:10 AM
How about a smart pointer approch !

Sreeram wrote:
> Hi,
> 
> I am writing a multithreaded server application.
> 
> I have written a class to handle thread(CThread , say) activities like 
> creating, suspending, resuming threads etc. I have derived a class from 
> CThread (CMainThread, say) and a class (CClientThread, say) to handle 
> client connection which is also derived from CThread. In CMainThread 
> class i am creating a thread and waiting for incomming connections. As 
> soon as a connection came i am creating an object for CClientThread and 
> call the function to create a thread and so on.
> 
> code flow:
> 
> 1. Start the main listening thread.
> 2. wait for a connection.
> 3. connection received. start a client thread.
>     like this -> clientObj = new CClientThread;
>              clientObj->StartClientThread(pArg);
> 4. Go to step 2.
> 
> Problem is that, how i can delete the "clientObj" object to aoid memory 
> leaks? where this should happen? or any other preferred approach is 
> there to do this task correctly. I do not want to write the 
> mainlistening thread and client thread code in the same class. I want 
> them in separate classes.
> 
> Anybody can give me some idea how to do this correctly.
> 
> Thanks
> Sreeram
> 

0
8/8/2003 8:47:07 AM
David,

Thank you for your reply.

    >   I generally create a static method to kill all instances.  It knows
    > how to find all threads and ask them to die (or kill them outright).

    You mean, to create a static function in the CClientThread class right?
and use that in the following way.

    CClientThread::StaticFn(pArg);   // call from the Main Listening Thread.

    In this static function,  void CClientThread::StaticFn(pArg) {
                                                // create a new
CClientThread object by passing pArg as argument.
                                        }

    The constructor(one-argument) of the CClientThread must be private.
    in the destructor delete the CClientThread object. right?

    Please correct me if i am going wrong in this.

Thanks
Sreeram


"David" <FlyLikeAnEagle@United.Com> wrote in message
news:rOdGr40LMPU3-pn2-kBxBcsDJ7HBw@localhost...
>   Sreeram,
>
>   My approach would be to have the CClientThread class manage
> instances of itself.  This would allow CMainThread access to
> the data as well as allowing instances of CClientThread to
> know about each other, should that become necessary.  I've
> done this many times before.
>
>   CClientThread would need a small group of management methods
> and data members.  Basically, you want some form of set (array,
> linked list, STL array, etc) of pointers to each instance
> of CClientThread.  You would probably want to keep this information
> protected and have accessors.
>
>   I generally create a static method to kill all instances.  It knows
> how to find all threads and ask them to die (or kill them outright).
>
>   The ctor and dtor of CClientThread manage the set of instance pointers.
> Thus when an instance destroys itself everything gets managed on its
> own.  You could provide callbacks or virtual overrides to allow the
> parent controlling object (main or CMainThread) to be informed when
> important events occur.
>
>   I tend to approach the design a bit differently.  Many years ago
> I created ISocket, IClientSocket, IServerSocket,
> IMultiprotocolServerSocket, and so on as a basis for my communications.
> There are also Serial and IPC variants.  From these I derive classes
> that embody the conversations.  Take email as an example.  I created
> ISMTPClient<> and ISMTPServer<> template classes.  Writing just the
> client/server sides is an easy task that can be seperated from the
> delivery tasks.  It also allowed several rather novice programmers
> to reuse my code to handle conversations in their programs.  We
> were doing cross platform communications.  I had used the code under
> DOS, Unix, and OS/2.  These people were from the AIX world and didn't
> seem to handle protocols too well (just a bit green).  I handed them
> the Unix variant of my ISocket class and all the client/server classes
> they needed.  Since I was also in charge of the protocol (remote FAXing)
> it was a simple matter to write both sides of the conversation at
> once -- the IxxxServer<> and IxxxClient<> portions.  They didn't have
> to do anything but compile the code.  Saved weeks trying to teach them
> TCP/IP, Network Byte Ordering, and all the other things they needed to
> know to develop a fully debugged and tested application.  I'll back off
> and let you think about how you might do it next time.  All this stuff
> didn't come into being overnight.  Porting from the Unix world to Windows
> was a bit tough but the classes and templates held together -- only one
> class and one template needed changing.
>
>   G'night,
>
>   David
>
> On Fri, 8 Aug 2003 04:28:04 UTC, Sreeram <sreeram0425@netscape.net> wrote:
>
> > Hi,
> >
> > I am writing a multithreaded server application.
> >
> > I have written a class to handle thread(CThread , say) activities like
> > creating, suspending, resuming threads etc. I have derived a class from
> > CThread (CMainThread, say) and a class (CClientThread, say) to handle
> > client connection which is also derived from CThread. In CMainThread
> > class i am creating a thread and waiting for incomming connections. As
> > soon as a connection came i am creating an object for CClientThread and
> > call the function to create a thread and so on.
> >
> > code flow:
> >
> > 1. Start the main listening thread.
> > 2. wait for a connection.
> > 3. connection received. start a client thread.
> > like this -> clientObj = new CClientThread;
> >      clientObj->StartClientThread(pArg);
> > 4. Go to step 2.
> >
> > Problem is that, how i can delete the "clientObj" object to aoid memory
> > leaks? where this should happen? or any other preferred approach is
> > there to do this task correctly. I do not want to write the
> > mainlistening thread and client thread code in the same class. I want
> > them in separate classes.
> >
> > Anybody can give me some idea how to do this correctly.
> >
> > Thanks
> > Sreeram
> >
>
>


0
8/8/2003 9:54:31 AM
David,

Sorry the last post i sent from my colleagues machine. So it went in his
name... :-(

Sreeram

"David" <FlyLikeAnEagle@United.Com> wrote in message
news:rOdGr40LMPU3-pn2-kBxBcsDJ7HBw@localhost...
>   Sreeram,
>
>   My approach would be to have the CClientThread class manage
> instances of itself.  This would allow CMainThread access to
> the data as well as allowing instances of CClientThread to
> know about each other, should that become necessary.  I've
> done this many times before.
>
>   CClientThread would need a small group of management methods
> and data members.  Basically, you want some form of set (array,
> linked list, STL array, etc) of pointers to each instance
> of CClientThread.  You would probably want to keep this information
> protected and have accessors.
>
>   I generally create a static method to kill all instances.  It knows
> how to find all threads and ask them to die (or kill them outright).
>
>   The ctor and dtor of CClientThread manage the set of instance pointers.
> Thus when an instance destroys itself everything gets managed on its
> own.  You could provide callbacks or virtual overrides to allow the
> parent controlling object (main or CMainThread) to be informed when
> important events occur.
>
>   I tend to approach the design a bit differently.  Many years ago
> I created ISocket, IClientSocket, IServerSocket,
> IMultiprotocolServerSocket, and so on as a basis for my communications.
> There are also Serial and IPC variants.  From these I derive classes
> that embody the conversations.  Take email as an example.  I created
> ISMTPClient<> and ISMTPServer<> template classes.  Writing just the
> client/server sides is an easy task that can be seperated from the
> delivery tasks.  It also allowed several rather novice programmers
> to reuse my code to handle conversations in their programs.  We
> were doing cross platform communications.  I had used the code under
> DOS, Unix, and OS/2.  These people were from the AIX world and didn't
> seem to handle protocols too well (just a bit green).  I handed them
> the Unix variant of my ISocket class and all the client/server classes
> they needed.  Since I was also in charge of the protocol (remote FAXing)
> it was a simple matter to write both sides of the conversation at
> once -- the IxxxServer<> and IxxxClient<> portions.  They didn't have
> to do anything but compile the code.  Saved weeks trying to teach them
> TCP/IP, Network Byte Ordering, and all the other things they needed to
> know to develop a fully debugged and tested application.  I'll back off
> and let you think about how you might do it next time.  All this stuff
> didn't come into being overnight.  Porting from the Unix world to Windows
> was a bit tough but the classes and templates held together -- only one
> class and one template needed changing.
>
>   G'night,
>
>   David
>
> On Fri, 8 Aug 2003 04:28:04 UTC, Sreeram <sreeram0425@netscape.net> wrote:
>
> > Hi,
> >
> > I am writing a multithreaded server application.
> >
> > I have written a class to handle thread(CThread , say) activities like
> > creating, suspending, resuming threads etc. I have derived a class from
> > CThread (CMainThread, say) and a class (CClientThread, say) to handle
> > client connection which is also derived from CThread. In CMainThread
> > class i am creating a thread and waiting for incomming connections. As
> > soon as a connection came i am creating an object for CClientThread and
> > call the function to create a thread and so on.
> >
> > code flow:
> >
> > 1. Start the main listening thread.
> > 2. wait for a connection.
> > 3. connection received. start a client thread.
> > like this -> clientObj = new CClientThread;
> >      clientObj->StartClientThread(pArg);
> > 4. Go to step 2.
> >
> > Problem is that, how i can delete the "clientObj" object to aoid memory
> > leaks? where this should happen? or any other preferred approach is
> > there to do this task correctly. I do not want to write the
> > mainlistening thread and client thread code in the same class. I want
> > them in separate classes.
> >
> > Anybody can give me some idea how to do this correctly.
> >
> > Thanks
> > Sreeram
> >
>
>


0
8/8/2003 10:05:38 AM
  No problem.  I'll answer your question there.

  David

On Fri, 8 Aug 2003 10:05:38 UTC, "Sreeram" <sreeram0425@netscape.net> wrote:

> David,
> 
> Sorry the last post i sent from my colleagues machine. So it went in his
> name... :-(
> 
> Sreeram

0
8/9/2003 12:29:25 AM
  Sreeram,

  That's not quite what I mean.  I'll try to give a more complete example.
Note, the response about using smart pointers could be useful too.

  Okay, most people get the concept of a class being able to define and
manage objects.  Sometimes they overlook the fact that classes themselves
can have data and methods -- without actually having any objects.  I find
this useful in making objects that may be considered part of a set, or
have to rely on some inter-object management, to become self aware.

  In your orignal post you asked about where each CClientObject should
be deleted.  You had a pointer, clientObj, that you used to create an
object and then call its start method.  Your code fragment didn't tell
us if you reused clientObj for the next accept, or perhaps intended 
that to be stored in some collection for later use.  You realized that
at some point the CClientObject should be deleted to free up its
resources.  That could be done by your main program when it decides to
end.  It is also entirely possible to have an object delete itself.
Threads will clean up after themselves when they exit.
CClientObject::~CClientObject could simply clean up itself and end the
threads it owns.  Thus there is nothing you need to do in your main 
program.

  Server applications usually don't like to "just dissapear" on their
clients.  It might be nice, for instance, for your main program to decide
to exit and as part of that process, ask each CClientObject to clean
up its conversation.  That could be as simple as closing the socket.
Usually it means exchanging messages with the client and cleaning up
in an orderly fashion.

  Okay, now back to my discussion of last night.

  I am presuming that CClientObject will operate on its own once it is
started.  The ctor itself could do this.  You chose to have a 
StartClientThread method.  I will presume that you also have something
akin to a message loop (Windows) or main processing thread (Unix).
This thread processes messages from the socket and responds to them.
On the client end this is generally an easy task.  A server thread may
be much more complicated in what it does.  In either case, it is a
good idea to have a method or datum that you can call/set to indicate
to the thread that it should clean up and destroy itself.

  Given this, and supposing that you saved the clientObj pointers of
the objects you created, you could then have your main application
call the terminate, dtor, or other methods of your objects.

  My approach last night, was to have the objects manage themselves.
That is, all you really do most of the time is create objects.  i.e.

{
  // Accept code
  ...
  CClientObject *p = new CClientObject (...);

                 p = NULL;  // I don't care about them anyway
}


  Okay, you may still want to look at these objects again.  Why
did we not save the pointer to it?  My response is that these
objects will destroy themselves.  How do you know that the pointer
to them is still valid?  Also, I wanted a way to manage the whole
group of server conversations.  After they are started the only
thing I wanted was to tell them to die.  I suppose we could also
look for status and other things.  I'll explain how I'd get all
the objects to kill themselves.

  Like you, I'd have CClientThread handle a new conversation
from an accept.  I'd probably just let the ctor start the processing
loop but that is my preference.  The dtor would clean itself up
just like and other good object would.

  Classes can have static methods and data.  These are independant
of the objects a class can create.  I propose that (my) CClientThread
have the following:

- a set of pointers to all objects created with this class
  I'll call this "IObjectSet" later and it will be a set of
  pointers to objects of this class.

- a method that returns the number of objects that exist

- a method that asks all the objects to terminate (this could also
  just call the dtor for all objects if that will do)

  The ctor would be modified to add its pointer (this) to the
IObjectSet.  The dtor would be modified to remove its pointer (this)
from the IObjectSet.

  We might have a CClientThread::Count() method (static) that returns
the number of items in IObjectSet.

  We might have a CClientThread::KillAll() method (static) that gets
rid of all objects of this class.  KillAll just scans through all the
items in IObjectSet and send them the Terminate or dtor command.

  Your code then just has to accept new conversations and create
CClientThread objects.  When you want to end the program, call
CClientThread::KillAll() and that is it.

  You do want to remember to call KillAll before your program ends.
Its like a memory/resource leak if you don't.

  Class Static data and mehods have lots of uses.  Consider a linked
list or balance binary tree.  Each node would be an object and perhaps
the structure of the objects would be its organization.  So, we could
ask the nodes to delete themselves and the organization to sort/copy/
list/whatever itself.  I've seen some quite nice array and set 
management tools made this way.

  I hope this gives you some ideas.  Have fun!

  David

On Fri, 8 Aug 2003 09:54:31 UTC, "Murthy" <deepakbsmurthy@rediff.com> wrote:

> David,
> 
> Thank you for your reply.
> 
>     >   I generally create a static method to kill all instances.  It knows
>     > how to find all threads and ask them to die (or kill them outright).
> 
>     You mean, to create a static function in the CClientThread class right?
> and use that in the following way.
> 
>     CClientThread::StaticFn(pArg);   // call from the Main Listening Thread.
> 
>     In this static function,  void CClientThread::StaticFn(pArg) {
>                                                 // create a new
> CClientThread object by passing pArg as argument.
>                                         }
> 
>     The constructor(one-argument) of the CClientThread must be private.
>     in the destructor delete the CClientThread object. right?
> 
>     Please correct me if i am going wrong in this.
> 
> Thanks
> Sreeram
> 
> 
> "David" <FlyLikeAnEagle@United.Com> wrote in message
> news:rOdGr40LMPU3-pn2-kBxBcsDJ7HBw@localhost...
> >   Sreeram,
> >
> >   My approach would be to have the CClientThread class manage
> > instances of itself.  This would allow CMainThread access to
> > the data as well as allowing instances of CClientThread to
> > know about each other, should that become necessary.  I've
> > done this many times before.
> >
> >   CClientThread would need a small group of management methods
> > and data members.  Basically, you want some form of set (array,
> > linked list, STL array, etc) of pointers to each instance
> > of CClientThread.  You would probably want to keep this information
> > protected and have accessors.
> >
> >   I generally create a static method to kill all instances.  It knows
> > how to find all threads and ask them to die (or kill them outright).
> >
> >   The ctor and dtor of CClientThread manage the set of instance pointers.
> > Thus when an instance destroys itself everything gets managed on its
> > own.  You could provide callbacks or virtual overrides to allow the
> > parent controlling object (main or CMainThread) to be informed when
> > important events occur.
> >
> >   I tend to approach the design a bit differently.  Many years ago
> > I created ISocket, IClientSocket, IServerSocket,
> > IMultiprotocolServerSocket, and so on as a basis for my communications.
> > There are also Serial and IPC variants.  From these I derive classes
> > that embody the conversations.  Take email as an example.  I created
> > ISMTPClient<> and ISMTPServer<> template classes.  Writing just the
> > client/server sides is an easy task that can be seperated from the
> > delivery tasks.  It also allowed several rather novice programmers
> > to reuse my code to handle conversations in their programs.  We
> > were doing cross platform communications.  I had used the code under
> > DOS, Unix, and OS/2.  These people were from the AIX world and didn't
> > seem to handle protocols too well (just a bit green).  I handed them
> > the Unix variant of my ISocket class and all the client/server classes
> > they needed.  Since I was also in charge of the protocol (remote FAXing)
> > it was a simple matter to write both sides of the conversation at
> > once -- the IxxxServer<> and IxxxClient<> portions.  They didn't have
> > to do anything but compile the code.  Saved weeks trying to teach them
> > TCP/IP, Network Byte Ordering, and all the other things they needed to
> > know to develop a fully debugged and tested application.  I'll back off
> > and let you think about how you might do it next time.  All this stuff
> > didn't come into being overnight.  Porting from the Unix world to Windows
> > was a bit tough but the classes and templates held together -- only one
> > class and one template needed changing.
> >
> >   G'night,
> >
> >   David
> >
> > On Fri, 8 Aug 2003 04:28:04 UTC, Sreeram <sreeram0425@netscape.net> wrote:
> >
> > > Hi,
> > >
> > > I am writing a multithreaded server application.
> > >
> > > I have written a class to handle thread(CThread , say) activities like
> > > creating, suspending, resuming threads etc. I have derived a class from
> > > CThread (CMainThread, say) and a class (CClientThread, say) to handle
> > > client connection which is also derived from CThread. In CMainThread
> > > class i am creating a thread and waiting for incomming connections. As
> > > soon as a connection came i am creating an object for CClientThread and
> > > call the function to create a thread and so on.
> > >
> > > code flow:
> > >
> > > 1. Start the main listening thread.
> > > 2. wait for a connection.
> > > 3. connection received. start a client thread.
> > > like this -> clientObj = new CClientThread;
> > >      clientObj->StartClientThread(pArg);
> > > 4. Go to step 2.
> > >
> > > Problem is that, how i can delete the "clientObj" object to aoid memory
> > > leaks? where this should happen? or any other preferred approach is
> > > there to do this task correctly. I do not want to write the
> > > mainlistening thread and client thread code in the same class. I want
> > > them in separate classes.
> > >
> > > Anybody can give me some idea how to do this correctly.
> > >
> > > Thanks
> > > Sreeram
> > >
> >
> >
> 
> 


0
8/9/2003 3:38:07 AM
Hi David,

Thanks for the detailed explanation. I understand now. :-)

As i am doing this in Windows OS can i use CArray template to store the 
objects?
static CArray<CTCPClients *, CTCPClients *> objArray;

But i am facing a problem if i declare this like this. It is a basic 
thing. But i am not sure how to do that. :-( How i can initialize a 
static "template" class variable?

Thanks
Sreeram

David wrote:

>   Sreeram,
> 
>   That's not quite what I mean.  I'll try to give a more complete example.
> Note, the response about using smart pointers could be useful too.
> 
>   Okay, most people get the concept of a class being able to define and
> manage objects.  Sometimes they overlook the fact that classes themselves
> can have data and methods -- without actually having any objects.  I find
> this useful in making objects that may be considered part of a set, or
> have to rely on some inter-object management, to become self aware.
> 
>   In your orignal post you asked about where each CClientObject should
> be deleted.  You had a pointer, clientObj, that you used to create an
> object and then call its start method.  Your code fragment didn't tell
> us if you reused clientObj for the next accept, or perhaps intended 
> that to be stored in some collection for later use.  You realized that
> at some point the CClientObject should be deleted to free up its
> resources.  That could be done by your main program when it decides to
> end.  It is also entirely possible to have an object delete itself.
> Threads will clean up after themselves when they exit.
> CClientObject::~CClientObject could simply clean up itself and end the
> threads it owns.  Thus there is nothing you need to do in your main 
> program.
> 
>   Server applications usually don't like to "just dissapear" on their
> clients.  It might be nice, for instance, for your main program to decide
> to exit and as part of that process, ask each CClientObject to clean
> up its conversation.  That could be as simple as closing the socket.
> Usually it means exchanging messages with the client and cleaning up
> in an orderly fashion.
> 
>   Okay, now back to my discussion of last night.
> 
>   I am presuming that CClientObject will operate on its own once it is
> started.  The ctor itself could do this.  You chose to have a 
> StartClientThread method.  I will presume that you also have something
> akin to a message loop (Windows) or main processing thread (Unix).
> This thread processes messages from the socket and responds to them.
> On the client end this is generally an easy task.  A server thread may
> be much more complicated in what it does.  In either case, it is a
> good idea to have a method or datum that you can call/set to indicate
> to the thread that it should clean up and destroy itself.
> 
>   Given this, and supposing that you saved the clientObj pointers of
> the objects you created, you could then have your main application
> call the terminate, dtor, or other methods of your objects.
> 
>   My approach last night, was to have the objects manage themselves.
> That is, all you really do most of the time is create objects.  i.e.
> 
> {
>   // Accept code
>   ...
>   CClientObject *p = new CClientObject (...);
> 
>                  p = NULL;  // I don't care about them anyway
> }
> 
> 
>   Okay, you may still want to look at these objects again.  Why
> did we not save the pointer to it?  My response is that these
> objects will destroy themselves.  How do you know that the pointer
> to them is still valid?  Also, I wanted a way to manage the whole
> group of server conversations.  After they are started the only
> thing I wanted was to tell them to die.  I suppose we could also
> look for status and other things.  I'll explain how I'd get all
> the objects to kill themselves.
> 
>   Like you, I'd have CClientThread handle a new conversation
> from an accept.  I'd probably just let the ctor start the processing
> loop but that is my preference.  The dtor would clean itself up
> just like and other good object would.
> 
>   Classes can have static methods and data.  These are independant
> of the objects a class can create.  I propose that (my) CClientThread
> have the following:
> 
> - a set of pointers to all objects created with this class
>   I'll call this "IObjectSet" later and it will be a set of
>   pointers to objects of this class.
> 
> - a method that returns the number of objects that exist
> 
> - a method that asks all the objects to terminate (this could also
>   just call the dtor for all objects if that will do)
> 
>   The ctor would be modified to add its pointer (this) to the
> IObjectSet.  The dtor would be modified to remove its pointer (this)
> from the IObjectSet.
> 
>   We might have a CClientThread::Count() method (static) that returns
> the number of items in IObjectSet.
> 
>   We might have a CClientThread::KillAll() method (static) that gets
> rid of all objects of this class.  KillAll just scans through all the
> items in IObjectSet and send them the Terminate or dtor command.
> 
>   Your code then just has to accept new conversations and create
> CClientThread objects.  When you want to end the program, call
> CClientThread::KillAll() and that is it.
> 
>   You do want to remember to call KillAll before your program ends.
> Its like a memory/resource leak if you don't.
> 
>   Class Static data and mehods have lots of uses.  Consider a linked
> list or balance binary tree.  Each node would be an object and perhaps
> the structure of the objects would be its organization.  So, we could
> ask the nodes to delete themselves and the organization to sort/copy/
> list/whatever itself.  I've seen some quite nice array and set 
> management tools made this way.
> 
>   I hope this gives you some ideas.  Have fun!
> 
>   David
> 
> On Fri, 8 Aug 2003 09:54:31 UTC, "Murthy" <deepakbsmurthy@rediff.com> wrote:
> 
> 
>>David,
>>
>>Thank you for your reply.
>>
>>    >   I generally create a static method to kill all instances.  It knows
>>    > how to find all threads and ask them to die (or kill them outright).
>>
>>    You mean, to create a static function in the CClientThread class right?
>>and use that in the following way.
>>
>>    CClientThread::StaticFn(pArg);   // call from the Main Listening Thread.
>>
>>    In this static function,  void CClientThread::StaticFn(pArg) {
>>                                                // create a new
>>CClientThread object by passing pArg as argument.
>>                                        }
>>
>>    The constructor(one-argument) of the CClientThread must be private.
>>    in the destructor delete the CClientThread object. right?
>>
>>    Please correct me if i am going wrong in this.
>>
>>Thanks
>>Sreeram
>>
>>
>>"David" <FlyLikeAnEagle@United.Com> wrote in message
>>news:rOdGr40LMPU3-pn2-kBxBcsDJ7HBw@localhost...
>>
>>>  Sreeram,
>>>
>>>  My approach would be to have the CClientThread class manage
>>>instances of itself.  This would allow CMainThread access to
>>>the data as well as allowing instances of CClientThread to
>>>know about each other, should that become necessary.  I've
>>>done this many times before.
>>>
>>>  CClientThread would need a small group of management methods
>>>and data members.  Basically, you want some form of set (array,
>>>linked list, STL array, etc) of pointers to each instance
>>>of CClientThread.  You would probably want to keep this information
>>>protected and have accessors.
>>>
>>>  I generally create a static method to kill all instances.  It knows
>>>how to find all threads and ask them to die (or kill them outright).
>>>
>>>  The ctor and dtor of CClientThread manage the set of instance pointers.
>>>Thus when an instance destroys itself everything gets managed on its
>>>own.  You could provide callbacks or virtual overrides to allow the
>>>parent controlling object (main or CMainThread) to be informed when
>>>important events occur.
>>>
>>>  I tend to approach the design a bit differently.  Many years ago
>>>I created ISocket, IClientSocket, IServerSocket,
>>>IMultiprotocolServerSocket, and so on as a basis for my communications.
>>>There are also Serial and IPC variants.  From these I derive classes
>>>that embody the conversations.  Take email as an example.  I created
>>>ISMTPClient<> and ISMTPServer<> template classes.  Writing just the
>>>client/server sides is an easy task that can be seperated from the
>>>delivery tasks.  It also allowed several rather novice programmers
>>>to reuse my code to handle conversations in their programs.  We
>>>were doing cross platform communications.  I had used the code under
>>>DOS, Unix, and OS/2.  These people were from the AIX world and didn't
>>>seem to handle protocols too well (just a bit green).  I handed them
>>>the Unix variant of my ISocket class and all the client/server classes
>>>they needed.  Since I was also in charge of the protocol (remote FAXing)
>>>it was a simple matter to write both sides of the conversation at
>>>once -- the IxxxServer<> and IxxxClient<> portions.  They didn't have
>>>to do anything but compile the code.  Saved weeks trying to teach them
>>>TCP/IP, Network Byte Ordering, and all the other things they needed to
>>>know to develop a fully debugged and tested application.  I'll back off
>>>and let you think about how you might do it next time.  All this stuff
>>>didn't come into being overnight.  Porting from the Unix world to Windows
>>>was a bit tough but the classes and templates held together -- only one
>>>class and one template needed changing.
>>>
>>>  G'night,
>>>
>>>  David
>>>
>>>On Fri, 8 Aug 2003 04:28:04 UTC, Sreeram <sreeram0425@netscape.net> wrote:
>>>
>>>
>>>>Hi,
>>>>
>>>>I am writing a multithreaded server application.
>>>>
>>>>I have written a class to handle thread(CThread , say) activities like
>>>>creating, suspending, resuming threads etc. I have derived a class from
>>>>CThread (CMainThread, say) and a class (CClientThread, say) to handle
>>>>client connection which is also derived from CThread. In CMainThread
>>>>class i am creating a thread and waiting for incomming connections. As
>>>>soon as a connection came i am creating an object for CClientThread and
>>>>call the function to create a thread and so on.
>>>>
>>>>code flow:
>>>>
>>>>1. Start the main listening thread.
>>>>2. wait for a connection.
>>>>3. connection received. start a client thread.
>>>>like this -> clientObj = new CClientThread;
>>>>     clientObj->StartClientThread(pArg);
>>>>4. Go to step 2.
>>>>
>>>>Problem is that, how i can delete the "clientObj" object to aoid memory
>>>>leaks? where this should happen? or any other preferred approach is
>>>>there to do this task correctly. I do not want to write the
>>>>mainlistening thread and client thread code in the same class. I want
>>>>them in separate classes.
>>>>
>>>>Anybody can give me some idea how to do this correctly.
>>>>
>>>>Thanks
>>>>Sreeram
>>>>
>>>
>>>
>>
> 
> 

0
8/12/2003 9:09:21 AM
  Hi Sreeram,

On Tue, 12 Aug 2003 09:09:21 UTC, Sreeram <sreeram0425@netscape.net> wrote:

> Hi David,
> 
> Thanks for the detailed explanation. I understand now. :-)
> 
> As i am doing this in Windows OS can i use CArray template to store the 
> objects?
> static CArray<CTCPClients *, CTCPClients *> objArray;

  That should be fine.

> But i am facing a problem if i declare this like this. It is a basic 
> thing. But i am not sure how to do that. :-( How i can initialize a 
> static "template" class variable?

  objArray will be initialized where you declare it.  If I recall
correctly, this will be a static part of CClientObject.  So, the
*.h file will have the definition

static CArray<...> objArray;

  In the *.cpp file, you will declare it as

CArray<...> CClientObject::objArray;

  Hope this helps,

  David

> Thanks
> Sreeram
> 
> David wrote:
> 
> >   Sreeram,
> > 
> >   That's not quite what I mean.  I'll try to give a more complete example.
> > Note, the response about using smart pointers could be useful too.
> > 
> >   Okay, most people get the concept of a class being able to define and
> > manage objects.  Sometimes they overlook the fact that classes themselves
> > can have data and methods -- without actually having any objects.  I find
> > this useful in making objects that may be considered part of a set, or
> > have to rely on some inter-object management, to become self aware.
> > 
> >   In your orignal post you asked about where each CClientObject should
> > be deleted.  You had a pointer, clientObj, that you used to create an
> > object and then call its start method.  Your code fragment didn't tell
> > us if you reused clientObj for the next accept, or perhaps intended 
> > that to be stored in some collection for later use.  You realized that
> > at some point the CClientObject should be deleted to free up its
> > resources.  That could be done by your main program when it decides to
> > end.  It is also entirely possible to have an object delete itself.
> > Threads will clean up after themselves when they exit.
> > CClientObject::~CClientObject could simply clean up itself and end the
> > threads it owns.  Thus there is nothing you need to do in your main 
> > program.
> > 
> >   Server applications usually don't like to "just dissapear" on their
> > clients.  It might be nice, for instance, for your main program to decide
> > to exit and as part of that process, ask each CClientObject to clean
> > up its conversation.  That could be as simple as closing the socket.
> > Usually it means exchanging messages with the client and cleaning up
> > in an orderly fashion.
> > 
> >   Okay, now back to my discussion of last night.
> > 
> >   I am presuming that CClientObject will operate on its own once it is
> > started.  The ctor itself could do this.  You chose to have a 
> > StartClientThread method.  I will presume that you also have something
> > akin to a message loop (Windows) or main processing thread (Unix).
> > This thread processes messages from the socket and responds to them.
> > On the client end this is generally an easy task.  A server thread may
> > be much more complicated in what it does.  In either case, it is a
> > good idea to have a method or datum that you can call/set to indicate
> > to the thread that it should clean up and destroy itself.
> > 
> >   Given this, and supposing that you saved the clientObj pointers of
> > the objects you created, you could then have your main application
> > call the terminate, dtor, or other methods of your objects.
> > 
> >   My approach last night, was to have the objects manage themselves.
> > That is, all you really do most of the time is create objects.  i.e.
> > 
> > {
> >   // Accept code
> >   ...
> >   CClientObject *p = new CClientObject (...);
> > 
> >                  p = NULL;  // I don't care about them anyway
> > }
> > 
> > 
> >   Okay, you may still want to look at these objects again.  Why
> > did we not save the pointer to it?  My response is that these
> > objects will destroy themselves.  How do you know that the pointer
> > to them is still valid?  Also, I wanted a way to manage the whole
> > group of server conversations.  After they are started the only
> > thing I wanted was to tell them to die.  I suppose we could also
> > look for status and other things.  I'll explain how I'd get all
> > the objects to kill themselves.
> > 
> >   Like you, I'd have CClientThread handle a new conversation
> > from an accept.  I'd probably just let the ctor start the processing
> > loop but that is my preference.  The dtor would clean itself up
> > just like and other good object would.
> > 
> >   Classes can have static methods and data.  These are independant
> > of the objects a class can create.  I propose that (my) CClientThread
> > have the following:
> > 
> > - a set of pointers to all objects created with this class
> >   I'll call this "IObjectSet" later and it will be a set of
> >   pointers to objects of this class.
> > 
> > - a method that returns the number of objects that exist
> > 
> > - a method that asks all the objects to terminate (this could also
> >   just call the dtor for all objects if that will do)
> > 
> >   The ctor would be modified to add its pointer (this) to the
> > IObjectSet.  The dtor would be modified to remove its pointer (this)
> > from the IObjectSet.
> > 
> >   We might have a CClientThread::Count() method (static) that returns
> > the number of items in IObjectSet.
> > 
> >   We might have a CClientThread::KillAll() method (static) that gets
> > rid of all objects of this class.  KillAll just scans through all the
> > items in IObjectSet and send them the Terminate or dtor command.
> > 
> >   Your code then just has to accept new conversations and create
> > CClientThread objects.  When you want to end the program, call
> > CClientThread::KillAll() and that is it.
> > 
> >   You do want to remember to call KillAll before your program ends.
> > Its like a memory/resource leak if you don't.
> > 
> >   Class Static data and mehods have lots of uses.  Consider a linked
> > list or balance binary tree.  Each node would be an object and perhaps
> > the structure of the objects would be its organization.  So, we could
> > ask the nodes to delete themselves and the organization to sort/copy/
> > list/whatever itself.  I've seen some quite nice array and set 
> > management tools made this way.
> > 
> >   I hope this gives you some ideas.  Have fun!
> > 
> >   David
> > 
> > On Fri, 8 Aug 2003 09:54:31 UTC, "Murthy" <deepakbsmurthy@rediff.com> wrote:
> > 
> > 
> >>David,
> >>
> >>Thank you for your reply.
> >>
> >>    >   I generally create a static method to kill all instances.  It knows
> >>    > how to find all threads and ask them to die (or kill them outright).
> >>
> >>    You mean, to create a static function in the CClientThread class right?
> >>and use that in the following way.
> >>
> >>    CClientThread::StaticFn(pArg);   // call from the Main Listening Thread.
> >>
> >>    In this static function,  void CClientThread::StaticFn(pArg) {
> >>                                                // create a new
> >>CClientThread object by passing pArg as argument.
> >>                                        }
> >>
> >>    The constructor(one-argument) of the CClientThread must be private.
> >>    in the destructor delete the CClientThread object. right?
> >>
> >>    Please correct me if i am going wrong in this.
> >>
> >>Thanks
> >>Sreeram
> >>
> >>
> >>"David" <FlyLikeAnEagle@United.Com> wrote in message
> >>news:rOdGr40LMPU3-pn2-kBxBcsDJ7HBw@localhost...
> >>
> >>>  Sreeram,
> >>>
> >>>  My approach would be to have the CClientThread class manage
> >>>instances of itself.  This would allow CMainThread access to
> >>>the data as well as allowing instances of CClientThread to
> >>>know about each other, should that become necessary.  I've
> >>>done this many times before.
> >>>
> >>>  CClientThread would need a small group of management methods
> >>>and data members.  Basically, you want some form of set (array,
> >>>linked list, STL array, etc) of pointers to each instance
> >>>of CClientThread.  You would probably want to keep this information
> >>>protected and have accessors.
> >>>
> >>>  I generally create a static method to kill all instances.  It knows
> >>>how to find all threads and ask them to die (or kill them outright).
> >>>
> >>>  The ctor and dtor of CClientThread manage the set of instance pointers.
> >>>Thus when an instance destroys itself everything gets managed on its
> >>>own.  You could provide callbacks or virtual overrides to allow the
> >>>parent controlling object (main or CMainThread) to be informed when
> >>>important events occur.
> >>>
> >>>  I tend to approach the design a bit differently.  Many years ago
> >>>I created ISocket, IClientSocket, IServerSocket,
> >>>IMultiprotocolServerSocket, and so on as a basis for my communications.
> >>>There are also Serial and IPC variants.  From these I derive classes
> >>>that embody the conversations.  Take email as an example.  I created
> >>>ISMTPClient<> and ISMTPServer<> template classes.  Writing just the
> >>>client/server sides is an easy task that can be seperated from the
> >>>delivery tasks.  It also allowed several rather novice programmers
> >>>to reuse my code to handle conversations in their programs.  We
> >>>were doing cross platform communications.  I had used the code under
> >>>DOS, Unix, and OS/2.  These people were from the AIX world and didn't
> >>>seem to handle protocols too well (just a bit green).  I handed them
> >>>the Unix variant of my ISocket class and all the client/server classes
> >>>they needed.  Since I was also in charge of the protocol (remote FAXing)
> >>>it was a simple matter to write both sides of the conversation at
> >>>once -- the IxxxServer<> and IxxxClient<> portions.  They didn't have
> >>>to do anything but compile the code.  Saved weeks trying to teach them
> >>>TCP/IP, Network Byte Ordering, and all the other things they needed to
> >>>know to develop a fully debugged and tested application.  I'll back off
> >>>and let you think about how you might do it next time.  All this stuff
> >>>didn't come into being overnight.  Porting from the Unix world to Windows
> >>>was a bit tough but the classes and templates held together -- only one
> >>>class and one template needed changing.
> >>>
> >>>  G'night,
> >>>
> >>>  David
> >>>
> >>>On Fri, 8 Aug 2003 04:28:04 UTC, Sreeram <sreeram0425@netscape.net> wrote:
> >>>
> >>>
> >>>>Hi,
> >>>>
> >>>>I am writing a multithreaded server application.
> >>>>
> >>>>I have written a class to handle thread(CThread , say) activities like
> >>>>creating, suspending, resuming threads etc. I have derived a class from
> >>>>CThread (CMainThread, say) and a class (CClientThread, say) to handle
> >>>>client connection which is also derived from CThread. In CMainThread
> >>>>class i am creating a thread and waiting for incomming connections. As
> >>>>soon as a connection came i am creating an object for CClientThread and
> >>>>call the function to create a thread and so on.
> >>>>
> >>>>code flow:
> >>>>
> >>>>1. Start the main listening thread.
> >>>>2. wait for a connection.
> >>>>3. connection received. start a client thread.
> >>>>like this -> clientObj = new CClientThread;
> >>>>     clientObj->StartClientThread(pArg);
> >>>>4. Go to step 2.
> >>>>
> >>>>Problem is that, how i can delete the "clientObj" object to aoid memory
> >>>>leaks? where this should happen? or any other preferred approach is
> >>>>there to do this task correctly. I do not want to write the
> >>>>mainlistening thread and client thread code in the same class. I want
> >>>>them in separate classes.
> >>>>
> >>>>Anybody can give me some idea how to do this correctly.
> >>>>
> >>>>Thanks
> >>>>Sreeram
> >>>>
> >>>
> >>>
> >>
> > 
> > 
> 


0
8/12/2003 11:22:02 PM
Hi David,

Thanks...

I do have few more doubts:

1. I am deleting the object in the destructor.
Destructor code:
// Begin
lpReq->clientSocket->CloseSocket();
delete clientSocket;
delete lpReq;

for (int i = 0; i < objArray.GetSize(); ++i)
{
	CTCPClients *pObj = objArray.GetAt(i);
	if (pObj == this)
	{
		objArray.RemoveAt(i);
		delete pObj;
		break;
	}
}
// End...

I am just thinking whether i do have to tighten my code little more by 
implementing CRTICAL_SECTIONS whereever i am acessing the objArray. 
objArray is a static variable inside the class. I am adding "this" 
pointer in the constructor and removing it from the destructor. or Any 
other prefered/better approach than this?

2. I do have a SOCKET class where i am doing all socket related 
functions. In my program i do have messaging between the server and 
client. I am defining my own format for server and client conversation. 
All of my messages starts from a message header(giving my application 
name) and ends with 0XFFFFFF. Also each word is separated by a LF character.
Format: MSGHEADER LF Field 1 LF Field 2 LF etc... MSGENDCHAR.

I am receiving data from the socket character by character and checking 
for the "MSGENDCHAR". If i found a "MSGENDCHAR" i am assuming message 
has been finished and doing further checking / validity of the message 
and doing neccassry operations.
// Begin
int CMySocket::Receive(char *cBuff, int iBufLen, const char chMsgEnd)
{
	BOOL bDone = TRUE;
	char ch;
	int nBytes;
	const int nBYTESTOREAD = 1;
	int nBytesRead = 0;
	do
	{
		//Read data
		nBytes = recv(sd, &ch, nBYTESTOREAD, 0);
		if (nBytes > 0)
		{
			//Read till 'MSGENDCHAR'
			if (ch == chMsgEnd)	
				bDone = FALSE;

			cBuff[nBytesRead++] = ch;
			if (nBytesRead >= iBufLen)
			{
				nBytesRead = 0; // some error
				bDone = FALSE;
			}
		}
		else if (nBytes == SOCKET_ERROR)
		{
			//socket error
			nBytesRead = 0;
			bDone = FALSE;
		}
		else if (nBytes == 0)
		{
			//connection closed
			nBytesRead = 0;
			bDone = FALSE;	
		}
		
	} while(bDone);
// little more is there i am omitting that...
}
// End

Can you please advice me how this can be done in a better way?  Is there 
any method available to see how many bytes are available in socket and 
just go and read that much data from there.

Please advice me.

Thanks
Sreeram


David wrote:
>   Hi Sreeram,
> 
> On Tue, 12 Aug 2003 09:09:21 UTC, Sreeram <sreeram0425@netscape.net> wrote:
> 
> 
>>Hi David,
>>
>>Thanks for the detailed explanation. I understand now. :-)
>>
>>As i am doing this in Windows OS can i use CArray template to store the 
>>objects?
>>static CArray<CTCPClients *, CTCPClients *> objArray;
> 
> 
>   That should be fine.
> 
> 
>>But i am facing a problem if i declare this like this. It is a basic 
>>thing. But i am not sure how to do that. :-( How i can initialize a 
>>static "template" class variable?
> 
> 
>   objArray will be initialized where you declare it.  If I recall
> correctly, this will be a static part of CClientObject.  So, the
> *.h file will have the definition
> 
> static CArray<...> objArray;
> 
>   In the *.cpp file, you will declare it as
> 
> CArray<...> CClientObject::objArray;
> 
>   Hope this helps,
> 
>   David
> 
> 
>>Thanks
>>Sreeram
>>
>>David wrote:
>>
>>
>>>  Sreeram,
>>>
>>>  That's not quite what I mean.  I'll try to give a more complete example.
>>>Note, the response about using smart pointers could be useful too.
>>>
>>>  Okay, most people get the concept of a class being able to define and
>>>manage objects.  Sometimes they overlook the fact that classes themselves
>>>can have data and methods -- without actually having any objects.  I find
>>>this useful in making objects that may be considered part of a set, or
>>>have to rely on some inter-object management, to become self aware.
>>>
>>>  In your orignal post you asked about where each CClientObject should
>>>be deleted.  You had a pointer, clientObj, that you used to create an
>>>object and then call its start method.  Your code fragment didn't tell
>>>us if you reused clientObj for the next accept, or perhaps intended 
>>>that to be stored in some collection for later use.  You realized that
>>>at some point the CClientObject should be deleted to free up its
>>>resources.  That could be done by your main program when it decides to
>>>end.  It is also entirely possible to have an object delete itself.
>>>Threads will clean up after themselves when they exit.
>>>CClientObject::~CClientObject could simply clean up itself and end the
>>>threads it owns.  Thus there is nothing you need to do in your main 
>>>program.
>>>
>>>  Server applications usually don't like to "just dissapear" on their
>>>clients.  It might be nice, for instance, for your main program to decide
>>>to exit and as part of that process, ask each CClientObject to clean
>>>up its conversation.  That could be as simple as closing the socket.
>>>Usually it means exchanging messages with the client and cleaning up
>>>in an orderly fashion.
>>>
>>>  Okay, now back to my discussion of last night.
>>>
>>>  I am presuming that CClientObject will operate on its own once it is
>>>started.  The ctor itself could do this.  You chose to have a 
>>>StartClientThread method.  I will presume that you also have something
>>>akin to a message loop (Windows) or main processing thread (Unix).
>>>This thread processes messages from the socket and responds to them.
>>>On the client end this is generally an easy task.  A server thread may
>>>be much more complicated in what it does.  In either case, it is a
>>>good idea to have a method or datum that you can call/set to indicate
>>>to the thread that it should clean up and destroy itself.
>>>
>>>  Given this, and supposing that you saved the clientObj pointers of
>>>the objects you created, you could then have your main application
>>>call the terminate, dtor, or other methods of your objects.
>>>
>>>  My approach last night, was to have the objects manage themselves.
>>>That is, all you really do most of the time is create objects.  i.e.
>>>
>>>{
>>>  // Accept code
>>>  ...
>>>  CClientObject *p = new CClientObject (...);
>>>
>>>                 p = NULL;  // I don't care about them anyway
>>>}
>>>
>>>
>>>  Okay, you may still want to look at these objects again.  Why
>>>did we not save the pointer to it?  My response is that these
>>>objects will destroy themselves.  How do you know that the pointer
>>>to them is still valid?  Also, I wanted a way to manage the whole
>>>group of server conversations.  After they are started the only
>>>thing I wanted was to tell them to die.  I suppose we could also
>>>look for status and other things.  I'll explain how I'd get all
>>>the objects to kill themselves.
>>>
>>>  Like you, I'd have CClientThread handle a new conversation
>>>from an accept.  I'd probably just let the ctor start the processing
>>>loop but that is my preference.  The dtor would clean itself up
>>>just like and other good object would.
>>>
>>>  Classes can have static methods and data.  These are independant
>>>of the objects a class can create.  I propose that (my) CClientThread
>>>have the following:
>>>
>>>- a set of pointers to all objects created with this class
>>>  I'll call this "IObjectSet" later and it will be a set of
>>>  pointers to objects of this class.
>>>
>>>- a method that returns the number of objects that exist
>>>
>>>- a method that asks all the objects to terminate (this could also
>>>  just call the dtor for all objects if that will do)
>>>
>>>  The ctor would be modified to add its pointer (this) to the
>>>IObjectSet.  The dtor would be modified to remove its pointer (this)
>>>from the IObjectSet.
>>>
>>>  We might have a CClientThread::Count() method (static) that returns
>>>the number of items in IObjectSet.
>>>
>>>  We might have a CClientThread::KillAll() method (static) that gets
>>>rid of all objects of this class.  KillAll just scans through all the
>>>items in IObjectSet and send them the Terminate or dtor command.
>>>
>>>  Your code then just has to accept new conversations and create
>>>CClientThread objects.  When you want to end the program, call
>>>CClientThread::KillAll() and that is it.
>>>
>>>  You do want to remember to call KillAll before your program ends.
>>>Its like a memory/resource leak if you don't.
>>>
>>>  Class Static data and mehods have lots of uses.  Consider a linked
>>>list or balance binary tree.  Each node would be an object and perhaps
>>>the structure of the objects would be its organization.  So, we could
>>>ask the nodes to delete themselves and the organization to sort/copy/
>>>list/whatever itself.  I've seen some quite nice array and set 
>>>management tools made this way.
>>>
>>>  I hope this gives you some ideas.  Have fun!
>>>
>>>  David
>>>
>>>On Fri, 8 Aug 2003 09:54:31 UTC, "Murthy" <deepakbsmurthy@rediff.com> wrote:
>>>
>>>
>>>
>>>>David,
>>>>
>>>>Thank you for your reply.
>>>>
>>>>   >   I generally create a static method to kill all instances.  It knows
>>>>   > how to find all threads and ask them to die (or kill them outright).
>>>>
>>>>   You mean, to create a static function in the CClientThread class right?
>>>>and use that in the following way.
>>>>
>>>>   CClientThread::StaticFn(pArg);   // call from the Main Listening Thread.
>>>>
>>>>   In this static function,  void CClientThread::StaticFn(pArg) {
>>>>                                               // create a new
>>>>CClientThread object by passing pArg as argument.
>>>>                                       }
>>>>
>>>>   The constructor(one-argument) of the CClientThread must be private.
>>>>   in the destructor delete the CClientThread object. right?
>>>>
>>>>   Please correct me if i am going wrong in this.
>>>>
>>>>Thanks
>>>>Sreeram
>>>>
>>>>
>>>>"David" <FlyLikeAnEagle@United.Com> wrote in message
>>>>news:rOdGr40LMPU3-pn2-kBxBcsDJ7HBw@localhost...
>>>>
>>>>
>>>>> Sreeram,
>>>>>
>>>>> My approach would be to have the CClientThread class manage
>>>>>instances of itself.  This would allow CMainThread access to
>>>>>the data as well as allowing instances of CClientThread to
>>>>>know about each other, should that become necessary.  I've
>>>>>done this many times before.
>>>>>
>>>>> CClientThread would need a small group of management methods
>>>>>and data members.  Basically, you want some form of set (array,
>>>>>linked list, STL array, etc) of pointers to each instance
>>>>>of CClientThread.  You would probably want to keep this information
>>>>>protected and have accessors.
>>>>>
>>>>> I generally create a static method to kill all instances.  It knows
>>>>>how to find all threads and ask them to die (or kill them outright).
>>>>>
>>>>> The ctor and dtor of CClientThread manage the set of instance pointers.
>>>>>Thus when an instance destroys itself everything gets managed on its
>>>>>own.  You could provide callbacks or virtual overrides to allow the
>>>>>parent controlling object (main or CMainThread) to be informed when
>>>>>important events occur.
>>>>>
>>>>> I tend to approach the design a bit differently.  Many years ago
>>>>>I created ISocket, IClientSocket, IServerSocket,
>>>>>IMultiprotocolServerSocket, and so on as a basis for my communications.
>>>>>There are also Serial and IPC variants.  From these I derive classes
>>>>>that embody the conversations.  Take email as an example.  I created
>>>>>ISMTPClient<> and ISMTPServer<> template classes.  Writing just the
>>>>>client/server sides is an easy task that can be seperated from the
>>>>>delivery tasks.  It also allowed several rather novice programmers
>>>>>to reuse my code to handle conversations in their programs.  We
>>>>>were doing cross platform communications.  I had used the code under
>>>>>DOS, Unix, and OS/2.  These people were from the AIX world and didn't
>>>>>seem to handle protocols too well (just a bit green).  I handed them
>>>>>the Unix variant of my ISocket class and all the client/server classes
>>>>>they needed.  Since I was also in charge of the protocol (remote FAXing)
>>>>>it was a simple matter to write both sides of the conversation at
>>>>>once -- the IxxxServer<> and IxxxClient<> portions.  They didn't have
>>>>>to do anything but compile the code.  Saved weeks trying to teach them
>>>>>TCP/IP, Network Byte Ordering, and all the other things they needed to
>>>>>know to develop a fully debugged and tested application.  I'll back off
>>>>>and let you think about how you might do it next time.  All this stuff
>>>>>didn't come into being overnight.  Porting from the Unix world to Windows
>>>>>was a bit tough but the classes and templates held together -- only one
>>>>>class and one template needed changing.
>>>>>
>>>>> G'night,
>>>>>
>>>>> David
>>>>>
>>>>>On Fri, 8 Aug 2003 04:28:04 UTC, Sreeram <sreeram0425@netscape.net> wrote:
>>>>>
>>>>>
>>>>>
>>>>>>Hi,
>>>>>>
>>>>>>I am writing a multithreaded server application.
>>>>>>
>>>>>>I have written a class to handle thread(CThread , say) activities like
>>>>>>creating, suspending, resuming threads etc. I have derived a class from
>>>>>>CThread (CMainThread, say) and a class (CClientThread, say) to handle
>>>>>>client connection which is also derived from CThread. In CMainThread
>>>>>>class i am creating a thread and waiting for incomming connections. As
>>>>>>soon as a connection came i am creating an object for CClientThread and
>>>>>>call the function to create a thread and so on.
>>>>>>
>>>>>>code flow:
>>>>>>
>>>>>>1. Start the main listening thread.
>>>>>>2. wait for a connection.
>>>>>>3. connection received. start a client thread.
>>>>>>like this -> clientObj = new CClientThread;
>>>>>>    clientObj->StartClientThread(pArg);
>>>>>>4. Go to step 2.
>>>>>>
>>>>>>Problem is that, how i can delete the "clientObj" object to aoid memory
>>>>>>leaks? where this should happen? or any other preferred approach is
>>>>>>there to do this task correctly. I do not want to write the
>>>>>>mainlistening thread and client thread code in the same class. I want
>>>>>>them in separate classes.
>>>>>>
>>>>>>Anybody can give me some idea how to do this correctly.
>>>>>>
>>>>>>Thanks
>>>>>>Sreeram
>>>>>>
>>>>>
>>>>>
>>>
> 
> 

0
8/13/2003 11:49:00 AM
  Good morning Sreeram,

  I'll make this quick as I'm off to work.

  Use the dtor to undo what you did in the ctor.  If you
created the CSocket, close and delete it.

  The dtor loop for finding and removing pObj may be overkill.
Specifically, isn't the value of pObj 'this'.  Thus if you
delete pObj you may be calling the memory destructor twice,
thus leaving you open to referencing part of the current object
when you've actually already deleted it.  Just let the class
dtor free up the memory for 'this'.

  Yes, it would be best if objArray was protected by a semaphore
or other locking mechanism so that it is thread safe.


On Wed, 13 Aug 2003 11:49:00 UTC, Sreeram <sreeram0425@netscape.net> wrote:

> Hi David,
> 
> Thanks...
> 
> I do have few more doubts:
> 
> 1. I am deleting the object in the destructor.
> Destructor code:
> // Begin
> lpReq->clientSocket->CloseSocket();
> delete clientSocket;
> delete lpReq;
> 
> for (int i = 0; i < objArray.GetSize(); ++i)
> {
> 	CTCPClients *pObj = objArray.GetAt(i);
> 	if (pObj == this)
> 	{
> 		objArray.RemoveAt(i);
> 		delete pObj;

  You probably don't need to delete pObj.  'this' was
created for you by the ctor (before your ctor was called)
and the dtor will free that memory after your dtor is complete.

> 		break;
> 	}
> }
> // End...
> 
> I am just thinking whether i do have to tighten my code little more by 
> implementing CRTICAL_SECTIONS whereever i am acessing the objArray. 
> objArray is a static variable inside the class. I am adding "this" 
> pointer in the constructor and removing it from the destructor. or Any 
> other prefered/better approach than this?

  Good idea.

> 2. I do have a SOCKET class where i am doing all socket related 
> functions. In my program i do have messaging between the server and 
> client. I am defining my own format for server and client conversation. 
> All of my messages starts from a message header(giving my application 
> name) and ends with 0XFFFFFF. Also each word is separated by a LF character.
> Format: MSGHEADER LF Field 1 LF Field 2 LF etc... MSGENDCHAR.

  Okay.  I presume that the fields cannot contain the MSGHEADER/MSGENDCHAR,
or LF.
 
> I am receiving data from the socket character by character and checking 
> for the "MSGENDCHAR". If i found a "MSGENDCHAR" i am assuming message 
> has been finished and doing further checking / validity of the message 
> and doing neccassry operations.
> // Begin
> int CMySocket::Receive(char *cBuff, int iBufLen, const char chMsgEnd)
> {
> 	BOOL bDone = TRUE;
> 	char ch;
> 	int nBytes;
> 	const int nBYTESTOREAD = 1;
> 	int nBytesRead = 0;
> 	do
> 	{
> 		//Read data
> 		nBytes = recv(sd, &ch, nBYTESTOREAD, 0);
> 		if (nBytes > 0)
> 		{
> 			//Read till 'MSGENDCHAR'
> 			if (ch == chMsgEnd)	
> 				bDone = FALSE;
> 
> 			cBuff[nBytesRead++] = ch;
> 			if (nBytesRead >= iBufLen)
> 			{
> 				nBytesRead = 0; // some error
> 				bDone = FALSE;
> 			}
> 		}
> 		else if (nBytes == SOCKET_ERROR)
> 		{
> 			//socket error
> 			nBytesRead = 0;
> 			bDone = FALSE;
> 		}
> 		else if (nBytes == 0)
> 		{
> 			//connection closed
> 			nBytesRead = 0;
> 			bDone = FALSE;	
> 		}
> 		
> 	} while(bDone);
> // little more is there i am omitting that...
> }
> // End
> 
> Can you please advice me how this can be done in a better way?  Is there 
> any method available to see how many bytes are available in socket and 
> just go and read that much data from there.

  Reading one byte at a time may be slower than reading all incoming bytes
and then processing them one at a time within your method.

  The method you are looking for is CAsyncSocket::IOCtl ().  Use the READ
(not its full name) parameter and it returns the number of bytes in the
socket's incoming buffer.  Remember to check the return code for errors
too.

  I didn't check all the above code but it seems reasonable with a quick
review.

  Have a great day,

  David

> Please advice me.
> 
> Thanks
> Sreeram
> 
> 
> David wrote:
> >   Hi Sreeram,
> > 
> > On Tue, 12 Aug 2003 09:09:21 UTC, Sreeram <sreeram0425@netscape.net> wrote:
> > 
> > 
> >>Hi David,
> >>
> >>Thanks for the detailed explanation. I understand now. :-)
> >>
> >>As i am doing this in Windows OS can i use CArray template to store the 
> >>objects?
> >>static CArray<CTCPClients *, CTCPClients *> objArray;
> > 
> > 
> >   That should be fine.
> > 
> > 
> >>But i am facing a problem if i declare this like this. It is a basic 
> >>thing. But i am not sure how to do that. :-( How i can initialize a 
> >>static "template" class variable?
> > 
> > 
> >   objArray will be initialized where you declare it.  If I recall
> > correctly, this will be a static part of CClientObject.  So, the
> > *.h file will have the definition
> > 
> > static CArray<...> objArray;
> > 
> >   In the *.cpp file, you will declare it as
> > 
> > CArray<...> CClientObject::objArray;
> > 
> >   Hope this helps,
> > 
> >   David
> > 
> > 
> >>Thanks
> >>Sreeram
> >>
> >>David wrote:
> >>
> >>
> >>>  Sreeram,
> >>>
> >>>  That's not quite what I mean.  I'll try to give a more complete example.
> >>>Note, the response about using smart pointers could be useful too.
> >>>
> >>>  Okay, most people get the concept of a class being able to define and
> >>>manage objects.  Sometimes they overlook the fact that classes themselves
> >>>can have data and methods -- without actually having any objects.  I find
> >>>this useful in making objects that may be considered part of a set, or
> >>>have to rely on some inter-object management, to become self aware.
> >>>
> >>>  In your orignal post you asked about where each CClientObject should
> >>>be deleted.  You had a pointer, clientObj, that you used to create an
> >>>object and then call its start method.  Your code fragment didn't tell
> >>>us if you reused clientObj for the next accept, or perhaps intended 
> >>>that to be stored in some collection for later use.  You realized that
> >>>at some point the CClientObject should be deleted to free up its
> >>>resources.  That could be done by your main program when it decides to
> >>>end.  It is also entirely possible to have an object delete itself.
> >>>Threads will clean up after themselves when they exit.
> >>>CClientObject::~CClientObject could simply clean up itself and end the
> >>>threads it owns.  Thus there is nothing you need to do in your main 
> >>>program.
> >>>
> >>>  Server applications usually don't like to "just dissapear" on their
> >>>clients.  It might be nice, for instance, for your main program to decide
> >>>to exit and as part of that process, ask each CClientObject to clean
> >>>up its conversation.  That could be as simple as closing the socket.
> >>>Usually it means exchanging messages with the client and cleaning up
> >>>in an orderly fashion.
> >>>
> >>>  Okay, now back to my discussion of last night.
> >>>
> >>>  I am presuming that CClientObject will operate on its own once it is
> >>>started.  The ctor itself could do this.  You chose to have a 
> >>>StartClientThread method.  I will presume that you also have something
> >>>akin to a message loop (Windows) or main processing thread (Unix).
> >>>This thread processes messages from the socket and responds to them.
> >>>On the client end this is generally an easy task.  A server thread may
> >>>be much more complicated in what it does.  In either case, it is a
> >>>good idea to have a method or datum that you can call/set to indicate
> >>>to the thread that it should clean up and destroy itself.
> >>>
> >>>  Given this, and supposing that you saved the clientObj pointers of
> >>>the objects you created, you could then have your main application
> >>>call the terminate, dtor, or other methods of your objects.
> >>>
> >>>  My approach last night, was to have the objects manage themselves.
> >>>That is, all you really do most of the time is create objects.  i.e.
> >>>
> >>>{
> >>>  // Accept code
> >>>  ...
> >>>  CClientObject *p = new CClientObject (...);
> >>>
> >>>                 p = NULL;  // I don't care about them anyway
> >>>}
> >>>
> >>>
> >>>  Okay, you may still want to look at these objects again.  Why
> >>>did we not save the pointer to it?  My response is that these
> >>>objects will destroy themselves.  How do you know that the pointer
> >>>to them is still valid?  Also, I wanted a way to manage the whole
> >>>group of server conversations.  After they are started the only
> >>>thing I wanted was to tell them to die.  I suppose we could also
> >>>look for status and other things.  I'll explain how I'd get all
> >>>the objects to kill themselves.
> >>>
> >>>  Like you, I'd have CClientThread handle a new conversation
> >>>from an accept.  I'd probably just let the ctor start the processing
> >>>loop but that is my preference.  The dtor would clean itself up
> >>>just like and other good object would.
> >>>
> >>>  Classes can have static methods and data.  These are independant
> >>>of the objects a class can create.  I propose that (my) CClientThread
> >>>have the following:
> >>>
> >>>- a set of pointers to all objects created with this class
> >>>  I'll call this "IObjectSet" later and it will be a set of
> >>>  pointers to objects of this class.
> >>>
> >>>- a method that returns the number of objects that exist
> >>>
> >>>- a method that asks all the objects to terminate (this could also
> >>>  just call the dtor for all objects if that will do)
> >>>
> >>>  The ctor would be modified to add its pointer (this) to the
> >>>IObjectSet.  The dtor would be modified to remove its pointer (this)
> >>>from the IObjectSet.
> >>>
> >>>  We might have a CClientThread::Count() method (static) that returns
> >>>the number of items in IObjectSet.
> >>>
> >>>  We might have a CClientThread::KillAll() method (static) that gets
> >>>rid of all objects of this class.  KillAll just scans through all the
> >>>items in IObjectSet and send them the Terminate or dtor command.
> >>>
> >>>  Your code then just has to accept new conversations and create
> >>>CClientThread objects.  When you want to end the program, call
> >>>CClientThread::KillAll() and that is it.
> >>>
> >>>  You do want to remember to call KillAll before your program ends.
> >>>Its like a memory/resource leak if you don't.
> >>>
> >>>  Class Static data and mehods have lots of uses.  Consider a linked
> >>>list or balance binary tree.  Each node would be an object and perhaps
> >>>the structure of the objects would be its organization.  So, we could
> >>>ask the nodes to delete themselves and the organization to sort/copy/
> >>>list/whatever itself.  I've seen some quite nice array and set 
> >>>management tools made this way.
> >>>
> >>>  I hope this gives you some ideas.  Have fun!
> >>>
> >>>  David
> >>>
> >>>On Fri, 8 Aug 2003 09:54:31 UTC, "Murthy" <deepakbsmurthy@rediff.com> wrote:
> >>>
> >>>
> >>>
> >>>>David,
> >>>>
> >>>>Thank you for your reply.
> >>>>
> >>>>   >   I generally create a static method to kill all instances.  It knows
> >>>>   > how to find all threads and ask them to die (or kill them outright).
> >>>>
> >>>>   You mean, to create a static function in the CClientThread class right?
> >>>>and use that in the following way.
> >>>>
> >>>>   CClientThread::StaticFn(pArg);   // call from the Main Listening Thread.
> >>>>
> >>>>   In this static function,  void CClientThread::StaticFn(pArg) {
> >>>>                                               // create a new
> >>>>CClientThread object by passing pArg as argument.
> >>>>                                       }
> >>>>
> >>>>   The constructor(one-argument) of the CClientThread must be private.
> >>>>   in the destructor delete the CClientThread object. right?
> >>>>
> >>>>   Please correct me if i am going wrong in this.
> >>>>
> >>>>Thanks
> >>>>Sreeram
> >>>>
> >>>>
> >>>>"David" <FlyLikeAnEagle@United.Com> wrote in message
> >>>>news:rOdGr40LMPU3-pn2-kBxBcsDJ7HBw@localhost...
> >>>>
> >>>>
> >>>>> Sreeram,
> >>>>>
> >>>>> My approach would be to have the CClientThread class manage
> >>>>>instances of itself.  This would allow CMainThread access to
> >>>>>the data as well as allowing instances of CClientThread to
> >>>>>know about each other, should that become necessary.  I've
> >>>>>done this many times before.
> >>>>>
> >>>>> CClientThread would need a small group of management methods
> >>>>>and data members.  Basically, you want some form of set (array,
> >>>>>linked list, STL array, etc) of pointers to each instance
> >>>>>of CClientThread.  You would probably want to keep this information
> >>>>>protected and have accessors.
> >>>>>
> >>>>> I generally create a static method to kill all instances.  It knows
> >>>>>how to find all threads and ask them to die (or kill them outright).
> >>>>>
> >>>>> The ctor and dtor of CClientThread manage the set of instance pointers.
> >>>>>Thus when an instance destroys itself everything gets managed on its
> >>>>>own.  You could provide callbacks or virtual overrides to allow the
> >>>>>parent controlling object (main or CMainThread) to be informed when
> >>>>>important events occur.
> >>>>>
> >>>>> I tend to approach the design a bit differently.  Many years ago
> >>>>>I created ISocket, IClientSocket, IServerSocket,
> >>>>>IMultiprotocolServerSocket, and so on as a basis for my communications.
> >>>>>There are also Serial and IPC variants.  From these I derive classes
> >>>>>that embody the conversations.  Take email as an example.  I created
> >>>>>ISMTPClient<> and ISMTPServer<> template classes.  Writing just the
> >>>>>client/server sides is an easy task that can be seperated from the
> >>>>>delivery tasks.  It also allowed several rather novice programmers
> >>>>>to reuse my code to handle conversations in their programs.  We
> >>>>>were doing cross platform communications.  I had used the code under
> >>>>>DOS, Unix, and OS/2.  These people were from the AIX world and didn't
> >>>>>seem to handle protocols too well (just a bit green).  I handed them
> >>>>>the Unix variant of my ISocket class and all the client/server classes
> >>>>>they needed.  Since I was also in charge of the protocol (remote FAXing)
> >>>>>it was a simple matter to write both sides of the conversation at
> >>>>>once -- the IxxxServer<> and IxxxClient<> portions.  They didn't have
> >>>>>to do anything but compile the code.  Saved weeks trying to teach them
> >>>>>TCP/IP, Network Byte Ordering, and all the other things they needed to
> >>>>>know to develop a fully debugged and tested application.  I'll back off
> >>>>>and let you think about how you might do it next time.  All this stuff
> >>>>>didn't come into being overnight.  Porting from the Unix world to Windows
> >>>>>was a bit tough but the classes and templates held together -- only one
> >>>>>class and one template needed changing.
> >>>>>
> >>>>> G'night,
> >>>>>
> >>>>> David
> >>>>>
> >>>>>On Fri, 8 Aug 2003 04:28:04 UTC, Sreeram <sreeram0425@netscape.net> wrote:
> >>>>>
> >>>>>
> >>>>>
> >>>>>>Hi,
> >>>>>>
> >>>>>>I am writing a multithreaded server application.
> >>>>>>
> >>>>>>I have written a class to handle thread(CThread , say) activities like
> >>>>>>creating, suspending, resuming threads etc. I have derived a class from
> >>>>>>CThread (CMainThread, say) and a class (CClientThread, say) to handle
> >>>>>>client connection which is also derived from CThread. In CMainThread
> >>>>>>class i am creating a thread and waiting for incomming connections. As
> >>>>>>soon as a connection came i am creating an object for CClientThread and
> >>>>>>call the function to create a thread and so on.
> >>>>>>
> >>>>>>code flow:
> >>>>>>
> >>>>>>1. Start the main listening thread.
> >>>>>>2. wait for a connection.
> >>>>>>3. connection received. start a client thread.
> >>>>>>like this -> clientObj = new CClientThread;
> >>>>>>    clientObj->StartClientThread(pArg);
> >>>>>>4. Go to step 2.
> >>>>>>
> >>>>>>Problem is that, how i can delete the "clientObj" object to aoid memory
> >>>>>>leaks? where this should happen? or any other preferred approach is
> >>>>>>there to do this task correctly. I do not want to write the
> >>>>>>mainlistening thread and client thread code in the same class. I want
> >>>>>>them in separate classes.
> >>>>>>
> >>>>>>Anybody can give me some idea how to do this correctly.
> >>>>>>
> >>>>>>Thanks
> >>>>>>Sreeram
> >>>>>>
> >>>>>
> >>>>>
> >>>
> > 
> > 
> 


0
8/13/2003 12:44:39 PM
Good Morning David,

I understand now... I was looking for a way like this. Thanks I have 
done it. I added a function GetBytesAvailable() in the socket class 
which will return -1 in case of SOCKET_ERROR else returned the no. of 
bytes read.
int CMySocket::GetBytesAvailable()
{
	int dwRet = 0;
	DWORD dwBytes = 0;
	if (ioctlsocket(sd, FIONREAD, &dwBytes) == SOCKET_ERROR)
		dwRet = -1;
	else
		dwRet = (int)dwBytes;
	
return(dwRet);
}

And i changed the Receive function
int CMySocket::Receive(char *cBuff, int iBufLen)
{	
	int nBytesRead = 0;
	//Read data
	nBytesRead = recv(sd, cBuff, iBufLen, 0);	
	if (nBytesRead == SOCKET_ERROR)
	{
		//socket error
		nBytesRead = 0;
	}
	else if (nBytesRead == 0)
	{
		//connection closed
		nBytesRead = 0;	
	}
return(nBytesRead);
}

I am checking the buffer returned by the receive function and looking 
for the MSGENDCHAR character. If i didnt find any i will go and call 
receive again after calling the getavailablebytes

My current project is little complex(for me). In that HTTPserver (small 
one i do have to handle GET, POST, Redirect), TCPIP server/client (i do 
have to do messaging. client should request(search cirteria) for 
information and server should respond with matching criteria) and RTSP 
server/client(Audio Streaming).

How i should maintain session with a timeout of 15mins? I mean the user 
will login through a webpage and i should maintain his session in the 
server. If he is idle for say 15mins the session should expire and 
server should not respond to any of the messages?

Have a Great Day!

Sreeram

David wrote:
>   Hello Sreeram,
> 
> 
> On Wed, 13 Aug 2003 13:40:02 UTC, Sreeram <sreeram0425@netscape.net> wrote:
> 
> 
>>Hi David,
>>
>>Yes. That delete was really unneccasry in the code. It didn't flash in 
>>my mind when i wrote the code. But to remove the object from the array i 
>>do have to loop it right?
> 
> 
>   Yes, you have to search for the correct item before you can delete it.
> 
> 
>>Yes fields don't contain the MSGHEADER/MSGENDCHAR or LF.
>>
>>Can you please explain me the read operation in little more detail later 
>>today when you are free?
> 
> 
>   I'm not sure what you need to know, so I'll cover the basics of sockets
> and what you can expect of the interface.  I've included a copy of one of
> my OnReceive methods below.
> 
>   Sockets are a bidirectional pipe that connect two processes.  Both ends
> can send and receive data.  Either end can be the client, server, or a 
> function of both of these roles.  Sockets are part of the TCP/IP protocol.
> IP manages unreliable transmissions between two endpoints and usually
> consists of small (under 2K byte) packets.  Sending does not gaurantee
> reception.  Order is not guaranteed either.  TCP is built on IP and
> provides a reliable, bidirectional communcations pipe between two endpoints.
> TCP and IP and any other logical or physical layers may emply buffering.
> You application can send or receive data from each endpoint.  The underlying
> TCP/IP protocol, however, manages sends in one direction at a time, and 
> uses a limited number of buffers for sending and receiving across IP.
> Bidirectionality and the concept of transferring data larger than one IP
> packet can hold are concepts handled by the TCP/IP interface.
> 
>   Consider a single socket pipe between two machines.  Side A can send a
> 100KB message to Side B.  Each layer of TCP/IP will break this request
> into multiple steps.  On the sending side, TCP will break the message
> into manageable chunks.  Extra data will be transmitted to insure data
> integrity, data order, buffer management, and status/error management.
> The receiver will receive IP packets.  Packets may be delivered out of
> order from what TCP gave them.  Reception is not gauranteed by the 
> receiver.  The receiver's TCP stack will request retransmissions as
> necessary, try to correct errors, assemble messages in their proper
> order, and do overall management of the conversation.  All this 
> complexity, and much more I've not alluded to, causes certain
> properties that may not be entirely obvious to the novice socket user.
> 
>   The first and foremost property is that your reqeusts are broken into
> various packets by multiple layers and other data is contained in this
> new data stream.  Therefore, a request by you to send X bytes does not
> mean that the receiver will receive all X bytes at once.  You cannot
> reasonably predict what size requests will be broken into.  This is
> generally tunable at the socket, TCP, IP, and physical protocol layers.
> Since Side A and Side B may not have the same configurations it is
> difficult to talk about transmission characteristics.
> 
>   TCP sockets are byte streams.  That is, the concept of a record
> is only known by your application.  TCP itself may bundle your single
> send/receive requests with other requests of a similar nature.  So,
> if you send a 1KB packet every second across a socket, you are not
> guranteed they will come in 1KB chunks, or any predictable size, or
> instantanious transmission rate.  The overall rate will probably still
> be 1KB per second provided that any of the underlying layers to not
> experience other traffic, undue retransmissions, and so on.
> 
>   This is why you must provide for sending data to take time and
> that you may receive data while transmitting.  Reception requires
> that you reconstruct any notion of messages at the application layer.
> Since sockets are bidirectional and (small) messages may be in queue in
> both directions, care must be taken when designing your communication
> protocols, their expectations, error conditions, and recovery procedures.
> 
>   There are many good books on TCP/IP Fundamentals.  The CSocket framework
> employed by Microsoft wraps the concept of TCP/IP sockets into yet another
> layer of confusion, expectations, and hidden rules.  Mostly, they tried to
> make TCP/IP fit into the Windows(tm) notion of messaging and programming.
> Thus topics like "when will OnReceive be called" and "when to use Send vs.
> OnSend" are rules developed by Microsoft.  They have little to do with
> TCP/IP.  This is why you may also want to find a good book on programming
> TCP/IP Client/Server applications using Windows.
> 
>   I've covered enough for now.  Let me know how your project is going and
> what other questions you have.
> 
>   David
> 
> =======================================================
> CClientSock inherits from CSocket.
> 
> void CClientSock::OnReceive (int nErrorCode)
> {
>     if (nErrorCode)
>     {   // We are being notified of an error with the socket.
>         logError ("CClientSock::OnReceive -- called with nErrorCode " + 
> IString (nErrorCode));
> 
>         return;
>     }
> 
>     while (1)
>     {   // While we don't know if there is more data to read
>         DWORD dwBytes = 0;
> 
>         if (!IOCtl (FIONREAD, &dwBytes))
>         {   // Error trying to get bytes in input buffer
>             logError ("IOCtl failed; error = " + IString (GetLastError ()));
> 
>             break;
>         }
> 
>         if (dwBytes)
>         {   // There is data to read
>             if (pCNetLink)
>             {
>                  pCNetLink->ProcessPendingRead ();
>             }
>             else
>             {
>                  logError ("pCNetLink is NULL.");
>             }
>         }
>         else
>         {
>             break;
>         }
>     }
> }
> 
> 
> ... CClientSocket::Send (...)
> {   // meta code for Send
>     OnReceive (0);  // called to prevent stalling if/when framework doen't 
> call OnReceive
> 
>     ... // do the normal send logic
> 
>     return ...;
> }
> 
> =======================================================
> 
> 
> 
>>Thanks & G'Night.
>>Sreeram
>>
>>David wrote:
>>
>>>  Good morning Sreeram,
>>>
>>>  I'll make this quick as I'm off to work.
>>>
>>>  Use the dtor to undo what you did in the ctor.  If you
>>>created the CSocket, close and delete it.
>>>
>>>  The dtor loop for finding and removing pObj may be overkill.
>>>Specifically, isn't the value of pObj 'this'.  Thus if you
>>>delete pObj you may be calling the memory destructor twice,
>>>thus leaving you open to referencing part of the current object
>>>when you've actually already deleted it.  Just let the class
>>>dtor free up the memory for 'this'.
>>>
>>>  Yes, it would be best if objArray was protected by a semaphore
>>>or other locking mechanism so that it is thread safe.
>>>
>>>
>>>On Wed, 13 Aug 2003 11:49:00 UTC, Sreeram <sreeram0425@netscape.net> wrote:
>>>
>>>
>>>
>>>>Hi David,
>>>>
>>>>Thanks...
>>>>
>>>>I do have few more doubts:
>>>>
>>>>1. I am deleting the object in the destructor.
>>>>Destructor code:
>>>>// Begin
>>>>lpReq->clientSocket->CloseSocket();
>>>>delete clientSocket;
>>>>delete lpReq;
>>>>
>>>>for (int i = 0; i < objArray.GetSize(); ++i)
>>>>{
>>>>	CTCPClients *pObj = objArray.GetAt(i);
>>>>	if (pObj == this)
>>>>	{
>>>>		objArray.RemoveAt(i);
>>>>		delete pObj;
>>>
>>>
>>>  You probably don't need to delete pObj.  'this' was
>>>created for you by the ctor (before your ctor was called)
>>>and the dtor will free that memory after your dtor is complete.
>>>
>>>
>>>
>>>>		break;
>>>>	}
>>>>}
>>>>// End...
>>>>
>>>>I am just thinking whether i do have to tighten my code little more by 
>>>>implementing CRTICAL_SECTIONS whereever i am acessing the objArray. 
>>>>objArray is a static variable inside the class. I am adding "this" 
>>>>pointer in the constructor and removing it from the destructor. or Any 
>>>>other prefered/better approach than this?
>>>
>>>
>>>  Good idea.
>>>
>>>
>>>
>>>>2. I do have a SOCKET class where i am doing all socket related 
>>>>functions. In my program i do have messaging between the server and 
>>>>client. I am defining my own format for server and client conversation. 
>>>>All of my messages starts from a message header(giving my application 
>>>>name) and ends with 0XFFFFFF. Also each word is separated by a LF character.
>>>>Format: MSGHEADER LF Field 1 LF Field 2 LF etc... MSGENDCHAR.
>>>
>>>
>>>  Okay.  I presume that the fields cannot contain the MSGHEADER/MSGENDCHAR,
>>>or LF.
>>> 
>>>
>>>
>>>>I am receiving data from the socket character by character and checking 
>>>>for the "MSGENDCHAR". If i found a "MSGENDCHAR" i am assuming message 
>>>>has been finished and doing further checking / validity of the message 
>>>>and doing neccassry operations.
>>>>// Begin
>>>>int CMySocket::Receive(char *cBuff, int iBufLen, const char chMsgEnd)
>>>>{
>>>>	BOOL bDone = TRUE;
>>>>	char ch;
>>>>	int nBytes;
>>>>	const int nBYTESTOREAD = 1;
>>>>	int nBytesRead = 0;
>>>>	do
>>>>	{
>>>>		//Read data
>>>>		nBytes = recv(sd, &ch, nBYTESTOREAD, 0);
>>>>		if (nBytes > 0)
>>>>		{
>>>>			//Read till 'MSGENDCHAR'
>>>>			if (ch == chMsgEnd)	
>>>>				bDone = FALSE;
>>>>
>>>>			cBuff[nBytesRead++] = ch;
>>>>			if (nBytesRead >= iBufLen)
>>>>			{
>>>>				nBytesRead = 0; // some error
>>>>				bDone = FALSE;
>>>>			}
>>>>		}
>>>>		else if (nBytes == SOCKET_ERROR)
>>>>		{
>>>>			//socket error
>>>>			nBytesRead = 0;
>>>>			bDone = FALSE;
>>>>		}
>>>>		else if (nBytes == 0)
>>>>		{
>>>>			//connection closed
>>>>			nBytesRead = 0;
>>>>			bDone = FALSE;	
>>>>		}
>>>>		
>>>>	} while(bDone);
>>>>// little more is there i am omitting that...
>>>>}
>>>>// End
>>>>
>>>>Can you please advice me how this can be done in a better way?  Is there 
>>>>any method available to see how many bytes are available in socket and 
>>>>just go and read that much data from there.
>>>
>>>
>>>  Reading one byte at a time may be slower than reading all incoming bytes
>>>and then processing them one at a time within your method.
>>>
>>>  The method you are looking for is CAsyncSocket::IOCtl ().  Use the READ
>>>(not its full name) parameter and it returns the number of bytes in the
>>>socket's incoming buffer.  Remember to check the return code for errors
>>>too.
>>>
>>>  I didn't check all the above code but it seems reasonable with a quick
>>>review.
>>>
>>>  Have a great day,
>>>
>>>  David
>>>
>>>
>>>
>>>>Please advice me.
>>>>
>>>>Thanks
>>>>Sreeram
>>>>
>>>>
>>>>David wrote:
>>>>
>>>>
>>>>> Hi Sreeram,
>>>>>
>>>>>On Tue, 12 Aug 2003 09:09:21 UTC, Sreeram <sreeram0425@netscape.net> wrote:
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>>Hi David,
>>>>>>
>>>>>>Thanks for the detailed explanation. I understand now. :-)
>>>>>>
>>>>>>As i am doing this in Windows OS can i use CArray template to store the 
>>>>>>objects?
>>>>>>static CArray<CTCPClients *, CTCPClients *> objArray;
>>>>>
>>>>>
>>>>> That should be fine.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>>But i am facing a problem if i declare this like this. It is a basic 
>>>>>>thing. But i am not sure how to do that. :-( How i can initialize a 
>>>>>>static "template" class variable?
>>>>>
>>>>>
>>>>> objArray will be initialized where you declare it.  If I recall
>>>>>correctly, this will be a static part of CClientObject.  So, the
>>>>>*.h file will have the definition
>>>>>
>>>>>static CArray<...> objArray;
>>>>>
>>>>> In the *.cpp file, you will declare it as
>>>>>
>>>>>CArray<...> CClientObject::objArray;
>>>>>
>>>>> Hope this helps,
>>>>>
>>>>> David
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>>Thanks
>>>>>>Sreeram
>>>>>>
>>>>>>David wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>>Sreeram,
>>>>>>>
>>>>>>>That's not quite what I mean.  I'll try to give a more complete example.
>>>>>>>Note, the response about using smart pointers could be useful too.
>>>>>>>
>>>>>>>Okay, most people get the concept of a class being able to define and
>>>>>>>manage objects.  Sometimes they overlook the fact that classes themselves
>>>>>>>can have data and methods -- without actually having any objects.  I find
>>>>>>>this useful in making objects that may be considered part of a set, or
>>>>>>>have to rely on some inter-object management, to become self aware.
>>>>>>>
>>>>>>>In your orignal post you asked about where each CClientObject should
>>>>>>>be deleted.  You had a pointer, clientObj, that you used to create an
>>>>>>>object and then call its start method.  Your code fragment didn't tell
>>>>>>>us if you reused clientObj for the next accept, or perhaps intended 
>>>>>>>that to be stored in some collection for later use.  You realized that
>>>>>>>at some point the CClientObject should be deleted to free up its
>>>>>>>resources.  That could be done by your main program when it decides to
>>>>>>>end.  It is also entirely possible to have an object delete itself.
>>>>>>>Threads will clean up after themselves when they exit.
>>>>>>>CClientObject::~CClientObject could simply clean up itself and end the
>>>>>>>threads it owns.  Thus there is nothing you need to do in your main 
>>>>>>>program.
>>>>>>>
>>>>>>>Server applications usually don't like to "just dissapear" on their
>>>>>>>clients.  It might be nice, for instance, for your main program to decide
>>>>>>>to exit and as part of that process, ask each CClientObject to clean
>>>>>>>up its conversation.  That could be as simple as closing the socket.
>>>>>>>Usually it means exchanging messages with the client and cleaning up
>>>>>>>in an orderly fashion.
>>>>>>>
>>>>>>>Okay, now back to my discussion of last night.
>>>>>>>
>>>>>>>I am presuming that CClientObject will operate on its own once it is
>>>>>>>started.  The ctor itself could do this.  You chose to have a 
>>>>>>>StartClientThread method.  I will presume that you also have something
>>>>>>>akin to a message loop (Windows) or main processing thread (Unix).
>>>>>>>This thread processes messages from the socket and responds to them.
>>>>>>>On the client end this is generally an easy task.  A server thread may
>>>>>>>be much more complicated in what it does.  In either case, it is a
>>>>>>>good idea to have a method or datum that you can call/set to indicate
>>>>>>>to the thread that it should clean up and destroy itself.
>>>>>>>
>>>>>>>Given this, and supposing that you saved the clientObj pointers of
>>>>>>>the objects you created, you could then have your main application
>>>>>>>call the terminate, dtor, or other methods of your objects.
>>>>>>>
>>>>>>>My approach last night, was to have the objects manage themselves.
>>>>>>>That is, all you really do most of the time is create objects.  i.e.
>>>>>>>
>>>>>>>{
>>>>>>>// Accept code
>>>>>>>...
>>>>>>>CClientObject *p = new CClientObject (...);
>>>>>>>
>>>>>>>               p = NULL;  // I don't care about them anyway
>>>>>>>}
>>>>>>>
>>>>>>>
>>>>>>>Okay, you may still want to look at these objects again.  Why
>>>>>>>did we not save the pointer to it?  My response is that these
>>>>>>>objects will destroy themselves.  How do you know that the pointer
>>>>>>>to them is still valid?  Also, I wanted a way to manage the whole
>>>>>>>group of server conversations.  After they are started the only
>>>>>>>thing I wanted was to tell them to die.  I suppose we could also
>>>>>>>look for status and other things.  I'll explain how I'd get all
>>>>>>>the objects to kill themselves.
>>>>>>>
>>>>>>>Like you, I'd have CClientThread handle a new conversation
>>>>>>
>>>>>>>from an accept.  I'd probably just let the ctor start the processing
>>>>>>
>>>>>>
>>>>>>>loop but that is my preference.  The dtor would clean itself up
>>>>>>>just like and other good object would.
>>>>>>>
>>>>>>>Classes can have static methods and data.  These are independant
>>>>>>>of the objects a class can create.  I propose that (my) CClientThread
>>>>>>>have the following:
>>>>>>>
>>>>>>>- a set of pointers to all objects created with this class
>>>>>>>I'll call this "IObjectSet" later and it will be a set of
>>>>>>>pointers to objects of this class.
>>>>>>>
>>>>>>>- a method that returns the number of objects that exist
>>>>>>>
>>>>>>>- a method that asks all the objects to terminate (this could also
>>>>>>>just call the dtor for all objects if that will do)
>>>>>>>
>>>>>>>The ctor would be modified to add its pointer (this) to the
>>>>>>>IObjectSet.  The dtor would be modified to remove its pointer (this)
>>>>>>
>>>>>>>from the IObjectSet.
>>>>>>
>>>>>>
>>>>>>>We might have a CClientThread::Count() method (static) that returns
>>>>>>>the number of items in IObjectSet.
>>>>>>>
>>>>>>>We might have a CClientThread::KillAll() method (static) that gets
>>>>>>>rid of all objects of this class.  KillAll just scans through all the
>>>>>>>items in IObjectSet and send them the Terminate or dtor command.
>>>>>>>
>>>>>>>Your code then just has to accept new conversations and create
>>>>>>>CClientThread objects.  When you want to end the program, call
>>>>>>>CClientThread::KillAll() and that is it.
>>>>>>>
>>>>>>>You do want to remember to call KillAll before your program ends.
>>>>>>>Its like a memory/resource leak if you don't.
>>>>>>>
>>>>>>>Class Static data and mehods have lots of uses.  Consider a linked
>>>>>>>list or balance binary tree.  Each node would be an object and perhaps
>>>>>>>the structure of the objects would be its organization.  So, we could
>>>>>>>ask the nodes to delete themselves and the organization to sort/copy/
>>>>>>>list/whatever itself.  I've seen some quite nice array and set 
>>>>>>>management tools made this way.
>>>>>>>
>>>>>>>I hope this gives you some ideas.  Have fun!
>>>>>>>
>>>>>>>David
>>>>>>>
>>>>>>>On Fri, 8 Aug 2003 09:54:31 UTC, "Murthy" <deepakbsmurthy@rediff.com> wrote:
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>>David,
>>>>>>>>
>>>>>>>>Thank you for your reply.
>>>>>>>>
>>>>>>>> >   I generally create a static method to kill all instances.  It knows
>>>>>>>> > how to find all threads and ask them to die (or kill them outright).
>>>>>>>>
>>>>>>>> You mean, to create a static function in the CClientThread class right?
>>>>>>>>and use that in the following way.
>>>>>>>>
>>>>>>>> CClientThread::StaticFn(pArg);   // call from the Main Listening Thread.
>>>>>>>>
>>>>>>>> In this static function,  void CClientThread::StaticFn(pArg) {
>>>>>>>>                                             // create a new
>>>>>>>>CClientThread object by passing pArg as argument.
>>>>>>>>                                     }
>>>>>>>>
>>>>>>>> The constructor(one-argument) of the CClientThread must be private.
>>>>>>>> in the destructor delete the CClientThread object. right?
>>>>>>>>
>>>>>>>> Please correct me if i am going wrong in this.
>>>>>>>>
>>>>>>>>Thanks
>>>>>>>>Sreeram
>>>>>>>>
>>>>>>>>
>>>>>>>>"David" <FlyLikeAnEagle@United.Com> wrote in message
>>>>>>>>news:rOdGr40LMPU3-pn2-kBxBcsDJ7HBw@localhost...
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>>Sreeram,
>>>>>>>>>
>>>>>>>>>My approach would be to have the CClientThread class manage
>>>>>>>>>instances of itself.  This would allow CMainThread access to
>>>>>>>>>the data as well as allowing instances of CClientThread to
>>>>>>>>>know about each other, should that become necessary.  I've
>>>>>>>>>done this many times before.
>>>>>>>>>
>>>>>>>>>CClientThread would need a small group of management methods
>>>>>>>>>and data members.  Basically, you want some form of set (array,
>>>>>>>>>linked list, STL array, etc) of pointers to each instance
>>>>>>>>>of CClientThread.  You would probably want to keep this information
>>>>>>>>>protected and have accessors.
>>>>>>>>>
>>>>>>>>>I generally create a static method to kill all instances.  It knows
>>>>>>>>>how to find all threads and ask them to die (or kill them outright).
>>>>>>>>>
>>>>>>>>>The ctor and dtor of CClientThread manage the set of instance pointers.
>>>>>>>>>Thus when an instance destroys itself everything gets managed on its
>>>>>>>>>own.  You could provide callbacks or virtual overrides to allow the
>>>>>>>>>parent controlling object (main or CMainThread) to be informed when
>>>>>>>>>important events occur.
>>>>>>>>>
>>>>>>>>>I tend to approach the design a bit differently.  Many years ago
>>>>>>>>>I created ISocket, IClientSocket, IServerSocket,
>>>>>>>>>IMultiprotocolServerSocket, and so on as a basis for my communications.
>>>>>>>>>There are also Serial and IPC variants.  From these I derive classes
>>>>>>>>>that embody the conversations.  Take email as an example.  I created
>>>>>>>>>ISMTPClient<> and ISMTPServer<> template classes.  Writing just the
>>>>>>>>>client/server sides is an easy task that can be seperated from the
>>>>>>>>>delivery tasks.  It also allowed several rather novice programmers
>>>>>>>>>to reuse my code to handle conversations in their programs.  We
>>>>>>>>>were doing cross platform communications.  I had used the code under
>>>>>>>>>DOS, Unix, and OS/2.  These people were from the AIX world and didn't
>>>>>>>>>seem to handle protocols too well (just a bit green).  I handed them
>>>>>>>>>the Unix variant of my ISocket class and all the client/server classes
>>>>>>>>>they needed.  Since I was also in charge of the protocol (remote FAXing)
>>>>>>>>>it was a simple matter to write both sides of the conversation at
>>>>>>>>>once -- the IxxxServer<> and IxxxClient<> portions.  They didn't have
>>>>>>>>>to do anything but compile the code.  Saved weeks trying to teach them
>>>>>>>>>TCP/IP, Network Byte Ordering, and all the other things they needed to
>>>>>>>>>know to develop a fully debugged and tested application.  I'll back off
>>>>>>>>>and let you think about how you might do it next time.  All this stuff
>>>>>>>>>didn't come into being overnight.  Porting from the Unix world to Windows
>>>>>>>>>was a bit tough but the classes and templates held together -- only one
>>>>>>>>>class and one template needed changing.
>>>>>>>>>
>>>>>>>>>G'night,
>>>>>>>>>
>>>>>>>>>David
>>>>>>>>>
>>>>>>>>>On Fri, 8 Aug 2003 04:28:04 UTC, Sreeram <sreeram0425@netscape.net> wrote:
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>>Hi,
>>>>>>>>>>
>>>>>>>>>>I am writing a multithreaded server application.
>>>>>>>>>>
>>>>>>>>>>I have written a class to handle thread(CThread , say) activities like
>>>>>>>>>>creating, suspending, resuming threads etc. I have derived a class from
>>>>>>>>>>CThread (CMainThread, say) and a class (CClientThread, say) to handle
>>>>>>>>>>client connection which is also derived from CThread. In CMainThread
>>>>>>>>>>class i am creating a thread and waiting for incomming connections. As
>>>>>>>>>>soon as a connection came i am creating an object for CClientThread and
>>>>>>>>>>call the function to create a thread and so on.
>>>>>>>>>>
>>>>>>>>>>code flow:
>>>>>>>>>>
>>>>>>>>>>1. Start the main listening thread.
>>>>>>>>>>2. wait for a connection.
>>>>>>>>>>3. connection received. start a client thread.
>>>>>>>>>>like this -> clientObj = new CClientThread;
>>>>>>>>>>  clientObj->StartClientThread(pArg);
>>>>>>>>>>4. Go to step 2.
>>>>>>>>>>
>>>>>>>>>>Problem is that, how i can delete the "clientObj" object to aoid memory
>>>>>>>>>>leaks? where this should happen? or any other preferred approach is
>>>>>>>>>>there to do this task correctly. I do not want to write the
>>>>>>>>>>mainlistening thread and client thread code in the same class. I want
>>>>>>>>>>them in separate classes.
>>>>>>>>>>
>>>>>>>>>>Anybody can give me some idea how to do this correctly.
>>>>>>>>>>
>>>>>>>>>>Thanks
>>>>>>>>>>Sreeram
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>
> 
> 

0
8/14/2003 12:27:57 PM
  Hi Sreeram,

  I'm glad to see that you have made some progress.  See below for my
answers.

  David

On Thu, 14 Aug 2003 12:27:57 UTC, Sreeram <sreeram0425@netscape.net> wrote:

> Good Morning David,
> 
> I understand now... I was looking for a way like this. Thanks I have 
> done it. I added a function GetBytesAvailable() in the socket class 
> which will return -1 in case of SOCKET_ERROR else returned the no. of 
> bytes read.
> int CMySocket::GetBytesAvailable()
> {
> 	int dwRet = 0;
> 	DWORD dwBytes = 0;
> 	if (ioctlsocket(sd, FIONREAD, &dwBytes) == SOCKET_ERROR)
> 		dwRet = -1;
> 	else
> 		dwRet = (int)dwBytes;
> 	
> return(dwRet);
> }
> 
> And i changed the Receive function
> int CMySocket::Receive(char *cBuff, int iBufLen)
> {	
> 	int nBytesRead = 0;
> 	//Read data
> 	nBytesRead = recv(sd, cBuff, iBufLen, 0);	
> 	if (nBytesRead == SOCKET_ERROR)
> 	{
> 		//socket error
> 		nBytesRead = 0;
> 	}
> 	else if (nBytesRead == 0)
> 	{
> 		//connection closed
> 		nBytesRead = 0;	
> 	}
> return(nBytesRead);
> }
> 
> I am checking the buffer returned by the receive function and looking 
> for the MSGENDCHAR character. If i didnt find any i will go and call 
> receive again after calling the getavailablebytes
> 
> My current project is little complex(for me). In that HTTPserver (small 
> one i do have to handle GET, POST, Redirect), TCPIP server/client (i do 
> have to do messaging. client should request(search cirteria) for 
> information and server should respond with matching criteria) and RTSP 
> server/client(Audio Streaming).
>
> How i should maintain session with a timeout of 15mins? I mean the user 
> will login through a webpage and i should maintain his session in the 
> server. If he is idle for say 15mins the session should expire and 
> server should not respond to any of the messages?

  Okay, you've mentioned most of the facts.  You will have sessions that
timeout after 15 minutes (presumably of non-use).  You will need to 
think about how you manage sessions.  Where do they start?  End?
Are you going to clean up these sessions after 15 minutes of inactivity?

  Each session will need to include the last time it was used.  At the
ctor (or start) and at each message processed -- record the current time.

  Before processing a message double check that it hasn't been more than
15 minutes since the last message was processed.  If so, reject it as
needed.

  You'll need some kind of cleanup mechanism for resources like sessions.
You might want to set a timer or thread to wake up every minute or so
and check all the sessions to see which ones need cleaned up due to 
inactivity.  This might be a good use for the static methods we talked
about earlier if you have a class for sessions.

> Have a Great Day!
> 
> Sreeram

<big snip of previous conversation>

0
8/18/2003 11:29:38 PM
Hi David,

I am planning to write it as a class.

The user will login to the server using a webpage. I want to start a 
session for him at the time when his login is succeded. His session 
should continue till he log out or expiration of the session (say 15mins 
the user is idle).

But i am little confused how i should do this programatcially? Do i have 
to use Hash table to maintain this? Hash table should contain the client 
IP, session start time, last access time. And a timer or thread should 
run which will get up for every 10mins and update the Hash table. But i 
want to manage this hash table from all parts of the program. The 
session should start when the user logged in to the server and the 
session should be accessible to TCP/Ip server(which will just accept a 
criteria and will send the response. Once it finishes sending the 
response, the client thread will get terminated), RTSP server & the data 
transfer to the client using UDP protocol.

I thought about array instead of Hash table to maintain this. But i 
think it will be expensive or time consuming because everytime if we 
need a update it should go and check one by one and see which one is the 
matching element. right?

Please can you advice how this i have to do this programatically using 
Hash table or with any other better method?

Thank You
Sreeram




David wrote:
>   Hi Sreeram,
> 
>   I'm glad to see that you have made some progress.  See below for my
> answers.
> 
>   David
> 
> On Thu, 14 Aug 2003 12:27:57 UTC, Sreeram <sreeram0425@netscape.net> wrote:
> 
> 
>
>>How i should maintain session with a timeout of 15mins? I mean the user 
>>will login through a webpage and i should maintain his session in the 
>>server. If he is idle for say 15mins the session should expire and 
>>server should not respond to any of the messages?
> 
> 
>   Okay, you've mentioned most of the facts.  You will have sessions that
> timeout after 15 minutes (presumably of non-use).  You will need to 
> think about how you manage sessions.  Where do they start?  End?
> Are you going to clean up these sessions after 15 minutes of inactivity?
> 
>   Each session will need to include the last time it was used.  At the
> ctor (or start) and at each message processed -- record the current time.
> 
>   Before processing a message double check that it hasn't been more than
> 15 minutes since the last message was processed.  If so, reject it as
> needed.
> 
>   You'll need some kind of cleanup mechanism for resources like sessions.
> You might want to set a timer or thread to wake up every minute or so
> and check all the sessions to see which ones need cleaned up due to 
> inactivity.  This might be a good use for the static methods we talked
> about earlier if you have a class for sessions.
> 
> 
>>Have a Great Day!
>>
>>Sreeram
> 
> 
> <big snip of previous conversation>
> 

0
8/20/2003 6:00:16 AM
  Hello Sreeram,

On Wed, 20 Aug 2003 06:00:16 UTC, Sreeram <sreeram0425@netscape.net> wrote:

> Hi David,
> 
> I am planning to write it as a class.
> 
> The user will login to the server using a webpage. I want to start a 
> session for him at the time when his login is succeded. His session 
> should continue till he log out or expiration of the session (say 15mins 
> the user is idle).

  Sounds good.  May I ask a little about how this will work?
I'm working on a web project (my second) and it will probably be a CGI
app on the server.  The clients will probably be simple web forms.
The goal is to have the CGI app (and thus the web user) remote control
a serial device.  I'm still totally open on my design as I'm still
working on the serial device stuff.

  I've been going on the presumption that your clients will be apps
on other machines.  Is your server an extension of the web server
or a seperate entity?  Will the web clients be Java or what?  Maybe
you can give me a few ideas on how I shuld proceed with my project.
Its an outside of work job that is very low priority for me.  I'll
discuss it later.
 
> But i am little confused how i should do this programatcially? Do i have 
> to use Hash table to maintain this? Hash table should contain the client 
> IP, session start time, last access time. And a timer or thread should 
> run which will get up for every 10mins and update the Hash table. But i 
> want to manage this hash table from all parts of the program. The 
> session should start when the user logged in to the server and the 
> session should be accessible to TCP/Ip server(which will just accept a 
> criteria and will send the response. Once it finishes sending the 
> response, the client thread will get terminated), RTSP server & the data 
> transfer to the client using UDP protocol.
>
> I thought about array instead of Hash table to maintain this. But i 
> think it will be expensive or time consuming because everytime if we 
> need a update it should go and check one by one and see which one is the 
> matching element. right?
> 
> Please can you advice how this i have to do this programatically using 
> Hash table or with any other better method?

  Okay, lets find out what your needs are.  Is there an upper limit on
the number of items you want to manage?  By items, I mean the stuff you
want to put in the array or hash table.  How will you look up items?
Will there be a need for more than one key or search item?

  A search of a simple array is generally pretty fast.  An ordered array
can use a binary search to speed things up.  Insertions are slower though.
Since its sounds like you will need to manage items with a 1/2/3 keys
with fast access, have fast insertions, fast finds, and medium to fast
deletes, just about any method sounds reasonable.

  Arrays have upper limits.  Binary (or other) trees are pretty good.
Hash tables can be used to speed up finding things in extremely large
sets.  A database (ISAM, KISAM, SQL, etc) could also be used, but is
generally much slower to access.  Does the STL have an ordered set
for one or more keys that you could use?

  I'd suggest an IBM solution but to my knowledge they didn't port it
to Windows.  They have something called an ISet<> template that is
like the STL Array, but just covers the access methods.  The implementation
can be array, linked list, b-tree, SQL, or whatever.  You combine
the templates to optimize a soluton based on your access needs and
and the access methods.

  I'm going to guess that for your needs any solution is reasonable
for the first cut.  We can optimize add/find/delete/change/etc
operations, but they aren't likely to make a huge difference on your
overall performance unless they are a high percentage of your CPU
time.  Connection setup, inquiry processing, response building,
and so on will be the bulk of it.

  I'd suggest a b-tree if you think time is of the essence.  An
STL Array with Iterator may be okay.  Its very hard to give advice
without knowing how many items were talking about and how often
each operation will be performed on the set and what multi-threaded
requirements there are.
 
> Thank You
> Sreeram
> 
> 
> 
> 
> David wrote:
> >   Hi Sreeram,
> > 
> >   I'm glad to see that you have made some progress.  See below for my
> > answers.
> > 
> >   David
> > 
> > On Thu, 14 Aug 2003 12:27:57 UTC, Sreeram <sreeram0425@netscape.net> wrote:
> > 
> > 
> >
> >>How i should maintain session with a timeout of 15mins? I mean the user 
> >>will login through a webpage and i should maintain his session in the 
> >>server. If he is idle for say 15mins the session should expire and 
> >>server should not respond to any of the messages?
> > 
> > 
> >   Okay, you've mentioned most of the facts.  You will have sessions that
> > timeout after 15 minutes (presumably of non-use).  You will need to 
> > think about how you manage sessions.  Where do they start?  End?
> > Are you going to clean up these sessions after 15 minutes of inactivity?
> > 
> >   Each session will need to include the last time it was used.  At the
> > ctor (or start) and at each message processed -- record the current time.
> > 
> >   Before processing a message double check that it hasn't been more than
> > 15 minutes since the last message was processed.  If so, reject it as
> > needed.
> > 
> >   You'll need some kind of cleanup mechanism for resources like sessions.
> > You might want to set a timer or thread to wake up every minute or so
> > and check all the sessions to see which ones need cleaned up due to 
> > inactivity.  This might be a good use for the static methods we talked
> > about earlier if you have a class for sessions.
> > 
> > 
> >>Have a Great Day!
> >>
> >>Sreeram
> > 
> > 
> > <big snip of previous conversation>
> > 
> 


0
8/21/2003 2:03:08 AM
This is a multi-part message in MIME format.
--------------000908060201090606060508
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Hi David,

I will explain you the steps i am going to do. Actually i have done all 
parts (test programs). Now i am trying to write the code in a better 
way(one major breakthrough is,  to split the server and client portion 
separately and using a class for socket operations, as you suggested. 
earlier everything was inside one classs.) and integrate the whole 
application.

As i said i do have a simple HTTP(Get, Post, redirect) server, TCP/IP 
server & RTSP server (+data transfer(RTP)). All server operations will 
happen inside one EXE.

The user will login to the server through a page which is purely a HTML 
script. Opon the successful login, the server will send another 
page(contains an ActiveX) through which user can do further operations. 
IE will automatically download the ActiveX and register it into the 
client computer if it is not already registered.

I can say there can be maximum of 100clients(But there is no 
restriction) connecting to the server at any given time. Can you please 
advice me how i should proceed with the session management? Which method 
i should use(Hash Table, B-Tree, or simple template array)?

Can i have(if possible) your e-mail address, please?

Thanks
Sreeram

David wrote:

>  Hello Sreeram,
>
>On Wed, 20 Aug 2003 06:00:16 UTC, Sreeram <sreeram0425@netscape.net> wrote:
>
>  
>
>>Hi David,
>>
>>I am planning to write it as a class.
>>
>>The user will login to the server using a webpage. I want to start a 
>>session for him at the time when his login is succeded. His session 
>>should continue till he log out or expiration of the session (say 15mins 
>>the user is idle).
>>    
>>
>
>  Sounds good.  May I ask a little about how this will work?
>I'm working on a web project (my second) and it will probably be a CGI
>app on the server.  The clients will probably be simple web forms.
>The goal is to have the CGI app (and thus the web user) remote control
>a serial device.  I'm still totally open on my design as I'm still
>working on the serial device stuff.
>
>  I've been going on the presumption that your clients will be apps
>on other machines.  Is your server an extension of the web server
>or a seperate entity?  Will the web clients be Java or what?  Maybe
>you can give me a few ideas on how I shuld proceed with my project.
>Its an outside of work job that is very low priority for me.  I'll
>discuss it later.
> 
>  
>
>>But i am little confused how i should do this programatcially? Do i have 
>>to use Hash table to maintain this? Hash table should contain the client 
>>IP, session start time, last access time. And a timer or thread should 
>>run which will get up for every 10mins and update the Hash table. But i 
>>want to manage this hash table from all parts of the program. The 
>>session should start when the user logged in to the server and the 
>>session should be accessible to TCP/Ip server(which will just accept a 
>>criteria and will send the response. Once it finishes sending the 
>>response, the client thread will get terminated), RTSP server & the data 
>>transfer to the client using UDP protocol.
>>
>>I thought about array instead of Hash table to maintain this. But i 
>>think it will be expensive or time consuming because everytime if we 
>>need a update it should go and check one by one and see which one is the 
>>matching element. right?
>>
>>Please can you advice how this i have to do this programatically using 
>>Hash table or with any other better method?
>>    
>>
>
>  Okay, lets find out what your needs are.  Is there an upper limit on
>the number of items you want to manage?  By items, I mean the stuff you
>want to put in the array or hash table.  How will you look up items?
>Will there be a need for more than one key or search item?
>
>  A search of a simple array is generally pretty fast.  An ordered array
>can use a binary search to speed things up.  Insertions are slower though.
>Since its sounds like you will need to manage items with a 1/2/3 keys
>with fast access, have fast insertions, fast finds, and medium to fast
>deletes, just about any method sounds reasonable.
>
>  Arrays have upper limits.  Binary (or other) trees are pretty good.
>Hash tables can be used to speed up finding things in extremely large
>sets.  A database (ISAM, KISAM, SQL, etc) could also be used, but is
>generally much slower to access.  Does the STL have an ordered set
>for one or more keys that you could use?
>
>  I'd suggest an IBM solution but to my knowledge they didn't port it
>to Windows.  They have something called an ISet<> template that is
>like the STL Array, but just covers the access methods.  The implementation
>can be array, linked list, b-tree, SQL, or whatever.  You combine
>the templates to optimize a soluton based on your access needs and
>and the access methods.
>
>  I'm going to guess that for your needs any solution is reasonable
>for the first cut.  We can optimize add/find/delete/change/etc
>operations, but they aren't likely to make a huge difference on your
>overall performance unless they are a high percentage of your CPU
>time.  Connection setup, inquiry processing, response building,
>and so on will be the bulk of it.
>
>  I'd suggest a b-tree if you think time is of the essence.  An
>STL Array with Iterator may be okay.  Its very hard to give advice
>without knowing how many items were talking about and how often
>each operation will be performed on the set and what multi-threaded
>requirements there are.
> 
>  
>
>>Thank You
>>Sreeram
>>
>>
>>
>>
>>David wrote:
>>    
>>
>>>  Hi Sreeram,
>>>
>>>  I'm glad to see that you have made some progress.  See below for my
>>>answers.
>>>
>>>  David
>>>
>>>
>>><big snip of previous conversation>
>>>
>>>      
>>>
>
>
>
>  
>

--------------000908060201090606060508
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
  <title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title></title>
Hi David,<br>
<br>
I will explain you the steps i am going to do. Actually i have
done all parts (test programs). Now i am trying to write the code in a
better way(one major breakthrough is,&nbsp; to split the server and client
portion separately and using a class for socket operations, as you
suggested. earlier everything was inside one classs.) and integrate the
whole application.<br>
<br>
As i said i do have a simple HTTP(Get, Post, redirect) server, TCP/IP
server &amp; RTSP server (+data transfer(RTP)). All server operations
will happen inside one EXE. <br>
<br>
The user will login to the server through a page which is purely a HTML
script. Opon the successful login, the server will send another
page(contains an ActiveX) through which user can do further operations.
IE will automatically download the ActiveX and register it into the
client computer if it is not already registered. <br>
<br>
I can say there can be maximum of 100clients(But there is no
restriction) connecting to the server at any given time. Can you please
advice me how i should proceed with the session management? Which
method i should use(Hash Table, B-Tree, or simple template array)? <br>
<br>
Can i have(if possible) your e-mail address, please?<br>
<br>
Thanks<br>
Sreeram<br>
<br>
David wrote:<br>
<blockquote type="cite"
 cite="midrOdGr40LMPU3-pn2-Y75erPozuONT@localhost">
  <pre wrap="">  Hello Sreeram,

On Wed, 20 Aug 2003 06:00:16 UTC, Sreeram <a
 class="moz-txt-link-rfc2396E" href="mailto:sreeram0425@netscape.net">&lt;sreeram0425@netscape.net&gt;</a> wrote:

  </pre>
  <blockquote type="cite">
    <pre wrap="">Hi David,

I am planning to write it as a class.

The user will login to the server using a webpage. I want to start a 
session for him at the time when his login is succeded. His session 
should continue till he log out or expiration of the session (say 15mins 
the user is idle).
    </pre>
  </blockquote>
  <pre wrap=""><!---->
  Sounds good.  May I ask a little about how this will work?
I'm working on a web project (my second) and it will probably be a CGI
app on the server.  The clients will probably be simple web forms.
The goal is to have the CGI app (and thus the web user) remote control
a serial device.  I'm still totally open on my design as I'm still
working on the serial device stuff.

  I've been going on the presumption that your clients will be apps
on other machines.  Is your server an extension of the web server
or a seperate entity?  Will the web clients be Java or what?  Maybe
you can give me a few ideas on how I shuld proceed with my project.
Its an outside of work job that is very low priority for me.  I'll
discuss it later.
 
  </pre>
  <blockquote type="cite">
    <pre wrap="">But i am little confused how i should do this programatcially? Do i have 
to use Hash table to maintain this? Hash table should contain the client 
IP, session start time, last access time. And a timer or thread should 
run which will get up for every 10mins and update the Hash table. But i 
want to manage this hash table from all parts of the program. The 
session should start when the user logged in to the server and the 
session should be accessible to TCP/Ip server(which will just accept a 
criteria and will send the response. Once it finishes sending the 
response, the client thread will get terminated), RTSP server &amp; the data 
transfer to the client using UDP protocol.

I thought about array instead of Hash table to maintain this. But i 
think it will be expensive or time consuming because everytime if we 
need a update it should go and check one by one and see which one is the 
matching element. right?

Please can you advice how this i have to do this programatically using 
Hash table or with any other better method?
    </pre>
  </blockquote>
  <pre wrap=""><!---->
  Okay, lets find out what your needs are.  Is there an upper limit on
the number of items you want to manage?  By items, I mean the stuff you
want to put in the array or hash table.  How will you look up items?
Will there be a need for more than one key or search item?

  A search of a simple array is generally pretty fast.  An ordered array
can use a binary search to speed things up.  Insertions are slower though.
Since its sounds like you will need to manage items with a 1/2/3 keys
with fast access, have fast insertions, fast finds, and medium to fast
deletes, just about any method sounds reasonable.

  Arrays have upper limits.  Binary (or other) trees are pretty good.
Hash tables can be used to speed up finding things in extremely large
sets.  A database (ISAM, KISAM, SQL, etc) could also be used, but is
generally much slower to access.  Does the STL have an ordered set
for one or more keys that you could use?

  I'd suggest an IBM solution but to my knowledge they didn't port it
to Windows.  They have something called an ISet&lt;&gt; template that is
like the STL Array, but just covers the access methods.  The implementation
can be array, linked list, b-tree, SQL, or whatever.  You combine
the templates to optimize a soluton based on your access needs and
and the access methods.

  I'm going to guess that for your needs any solution is reasonable
for the first cut.  We can optimize add/find/delete/change/etc
operations, but they aren't likely to make a huge difference on your
overall performance unless they are a high percentage of your CPU
time.  Connection setup, inquiry processing, response building,
and so on will be the bulk of it.

  I'd suggest a b-tree if you think time is of the essence.  An
STL Array with Iterator may be okay.  Its very hard to give advice
without knowing how many items were talking about and how often
each operation will be performed on the set and what multi-threaded
requirements there are.
 
  </pre>
  <blockquote type="cite">
    <pre wrap="">Thank You
Sreeram




David wrote:
    </pre>
    <blockquote type="cite">
      <pre wrap="">  Hi Sreeram,

  I'm glad to see that you have made some progress.  See below for my
answers.

  David


&lt;big snip of previous conversation&gt;

      </pre>
    </blockquote>
  </blockquote>
  <pre wrap=""><!---->


  </pre>
</blockquote>
</body>
</html>

--------------000908060201090606060508--

0
8/21/2003 11:19:48 AM
  Hello Sreeram,

  I'd forgotten about the ActiveX stuff.  I have to be platform
independant so I can't use that (can I?).  At least that explains 
how to get the UI and communications at the client all in one 
neat bundle.

  For the server to handle 100 clients plus or minus a bunch doesn't
sound like a big number to me.  I'd stick with a fast simple storage
method like an array.  Use an accessor method to find/add/etc the record
you want.  That way you can change it later if you need to.

  My email address is [Huey dot DLL at GTE dot Net].

  David

On Thu, 21 Aug 2003 11:19:48 UTC, Sreeram <sreeram0425@netscape.net> wrote:

> Hi David,
> 
> I will explain you the steps i am going to do. Actually i have done all 
> parts (test programs). Now i am trying to write the code in a better 
> way(one major breakthrough is,  to split the server and client portion 
> separately and using a class for socket operations, as you suggested. 
> earlier everything was inside one classs.) and integrate the whole 
> application.
> 
> As i said i do have a simple HTTP(Get, Post, redirect) server, TCP/IP 
> server & RTSP server (+data transfer(RTP)). All server operations will 
> happen inside one EXE.
> 
> The user will login to the server through a page which is purely a HTML 
> script. Opon the successful login, the server will send another 
> page(contains an ActiveX) through which user can do further operations. 
> IE will automatically download the ActiveX and register it into the 
> client computer if it is not already registered.
> 
> I can say there can be maximum of 100clients(But there is no 
> restriction) connecting to the server at any given time. Can you please 
> advice me how i should proceed with the session management? Which method 
> i should use(Hash Table, B-Tree, or simple template array)?
> 
> Can i have(if possible) your e-mail address, please?
> 
> Thanks
> Sreeram
> 
> David wrote:
> 
> >  Hello Sreeram,
> >
> >On Wed, 20 Aug 2003 06:00:16 UTC, Sreeram <sreeram0425@netscape.net> wrote:
> >
> >  
> >
> >>Hi David,
> >>
> >>I am planning to write it as a class.
> >>
> >>The user will login to the server using a webpage. I want to start a 
> >>session for him at the time when his login is succeded. His session 
> >>should continue till he log out or expiration of the session (say 15mins 
> >>the user is idle).
> >>    
> >>
> >
> >  Sounds good.  May I ask a little about how this will work?
> >I'm working on a web project (my second) and it will probably be a CGI
> >app on the server.  The clients will probably be simple web forms.
> >The goal is to have the CGI app (and thus the web user) remote control
> >a serial device.  I'm still totally open on my design as I'm still
> >working on the serial device stuff.
> >
> >  I've been going on the presumption that your clients will be apps
> >on other machines.  Is your server an extension of the web server
> >or a seperate entity?  Will the web clients be Java or what?  Maybe
> >you can give me a few ideas on how I shuld proceed with my project.
> >Its an outside of work job that is very low priority for me.  I'll
> >discuss it later.
> > 
> >  
> >
> >>But i am little confused how i should do this programatcially? Do i have 
> >>to use Hash table to maintain this? Hash table should contain the client 
> >>IP, session start time, last access time. And a timer or thread should 
> >>run which will get up for every 10mins and update the Hash table. But i 
> >>want to manage this hash table from all parts of the program. The 
> >>session should start when the user logged in to the server and the 
> >>session should be accessible to TCP/Ip server(which will just accept a 
> >>criteria and will send the response. Once it finishes sending the 
> >>response, the client thread will get terminated), RTSP server & the data 
> >>transfer to the client using UDP protocol.
> >>
> >>I thought about array instead of Hash table to maintain this. But i 
> >>think it will be expensive or time consuming because everytime if we 
> >>need a update it should go and check one by one and see which one is the 
> >>matching element. right?
> >>
> >>Please can you advice how this i have to do this programatically using 
> >>Hash table or with any other better method?
> >>    
> >>
> >
> >  Okay, lets find out what your needs are.  Is there an upper limit on
> >the number of items you want to manage?  By items, I mean the stuff you
> >want to put in the array or hash table.  How will you look up items?
> >Will there be a need for more than one key or search item?
> >
> >  A search of a simple array is generally pretty fast.  An ordered array
> >can use a binary search to speed things up.  Insertions are slower though.
> >Since its sounds like you will need to manage items with a 1/2/3 keys
> >with fast access, have fast insertions, fast finds, and medium to fast
> >deletes, just about any method sounds reasonable.
> >
> >  Arrays have upper limits.  Binary (or other) trees are pretty good.
> >Hash tables can be used to speed up finding things in extremely large
> >sets.  A database (ISAM, KISAM, SQL, etc) could also be used, but is
> >generally much slower to access.  Does the STL have an ordered set
> >for one or more keys that you could use?
> >
> >  I'd suggest an IBM solution but to my knowledge they didn't port it
> >to Windows.  They have something called an ISet<> template that is
> >like the STL Array, but just covers the access methods.  The implementation
> >can be array, linked list, b-tree, SQL, or whatever.  You combine
> >the templates to optimize a soluton based on your access needs and
> >and the access methods.
> >
> >  I'm going to guess that for your needs any solution is reasonable
> >for the first cut.  We can optimize add/find/delete/change/etc
> >operations, but they aren't likely to make a huge difference on your
> >overall performance unless they are a high percentage of your CPU
> >time.  Connection setup, inquiry processing, response building,
> >and so on will be the bulk of it.
> >
> >  I'd suggest a b-tree if you think time is of the essence.  An
> >STL Array with Iterator may be okay.  Its very hard to give advice
> >without knowing how many items were talking about and how often
> >each operation will be performed on the set and what multi-threaded
> >requirements there are.
> > 
> >  
> >
> >>Thank You
> >>Sreeram
> >>
> >>
> >>
> >>
> >>David wrote:
> >>    
> >>
> >>>  Hi Sreeram,
> >>>
> >>>  I'm glad to see that you have made some progress.  See below for my
> >>>answers.
> >>>
> >>>  David
> >>>
> >>>
> >>><big snip of previous conversation>
> >>>
> >>>      
> >>>
> >
> >
> >
> >  
> >
> 

0
8/22/2003 2:30:24 AM
Reply:

Similar Artilces:

Outlook Connector question
I'm using this with Outlook 2007; is there any way to get new messages to show up in Unread Mail instead of just in the email address's Inbox? Thanks, John No, not with the other unreads in your mailbox. You can have a search folder for the msn inbox though. -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Need Help with Common Tasks? http://www.outlook-tips.net/beginner/ Outlook 2007: http://www.slipstick.com/outlook/ol2007/ Outlook Tips by email: dailytips-subscribe-request@lists.outlooktips.net Outlook Tips: http://www.outlook-tips.net/ Out...

Question about menu's.
Hi, I am currently subclassing the main window of the MSN messenger (7 beta). I have added a menuitem to the contextmenu, however I do not get a WM_COMMAND for my added menu item. There are several menuitems that do generate a WM_COMMAND message but some of them don't. For instance: 'Send an instant message' generates a WM_COMMAND message and 'Block' does not. Can anybody tell me what the difference is between these two menuitems that reside in the samen popupmenu, it's a complete mistery to me. I can also Create a complete new menu, but it would be best to ext...

A MultiThreading problem with mfc SendMessage
Hi, i have done my own video player which support lot's of video format.....throught direct show or own source code.... but i have a small problem with a SendMessage... User can perform 3 actions Play, Pause and Stop..... * Play launch a thread and an SetEvent(E_PAUSE) to release it because thread loop begin with a WaitForMultipleObject of E_PAUSE or E_KILL * Pause ResetEvent(E_PAUSE) * Stop ResetEvent(E_PAUSE) and SetEvent(E_KILL) and wait for Thread Terminaison throught a WaitForSingleObject of E_END The thread could be describe as : do { WaitForMultipleObject of E_PAUSE or E_KILL ...

WEEKNUM() Question
The results of WEEKNUM() don't correspond with the calendaring system I want. My first question is simple: How does the WEEKNUM() algorithm work? If I can figure out what it's based on, I can probably match it to the calendar I'm using. It looks like it's anywhere from 1 day off to 2 days off from my calendar, depending on when my week decides to start. The calendar I have starts on Mondays and assigns WEEK-1 to the first week of January with at least 4 days in it. So if WEEKDAY(DATE(YEAR,1,1),2) is 5 (Friday) or greater, then the week doesn't start until WEEKDAY() y...

Outlook 2003 Attachments Question
Good Morning, I'm having a problem in Outlook 2003. When I have the reading pane open I am able to double click on an attachment and it opens normally. If I double click on the email and have it open, THEN try to open the attachment it tells me it cannot find the program associated with this attachment. Another problem is this: If I am in Word and go to File-Send to-EMail recipient as Attachment. It cannot find Outlook to send the message. However I am able to open Outlook and attach the file to a new email and send it. Any help would be greatly appriciated. Thank you! ~Michael ...

Quick Question #3
I have a workbook that is basically made up of 2 worksheets, a importing data sheet and a chart. Everything works great, timely refreshes and its rather simple. The problem I have is this: The chart is basically a seating chart and does not change at all(static). The 2nd worksheet continuosly updates the chart and will sometimes add new values(dynamic). Well, my formulas that I have for each cell points to the dynamic sheet and the values will change on occasion, unpredictablely. How can I write the formulas pointing to certain values in the data sheet(dynamic) and lock the cells. Then any ne...

SendMessage Question/Problem
I have a dialog box with a toolbar subclassed in it that has a combo box. I have embedded the toolbar in a static control on the dialog box. When an item in the combo box is selected, I am getting the OnSelChange message being triggered in the subclassed tool bar class. I then get the item that was selected and call GetOwner()->SendMessage(WM_NOTIFY......) In the dialog box I have a ON_NOTIFY message command for the IDR_TOOLBAR. when I debug the OnSelChange method in the subclassed toolbar class, I can see it setting up the call, but when I place my cursor and do run to cursor in the O...

easy OnLButtonDblClk() question
Hi, I've created a MFC application. It has the standard window where information is displayed. I have recently copied some code from another applcation someone made for me, so that I have a dockable window that attaches to the main document. The code for this class it as follows: class CMarkerZoom : public CWnd { // Construction public: CMarkerZoom(); // Overrides // ClassWizard generated virtual function overrides file://{{AFX_VIRTUAL(CMarkerZoom) file://}}AFX_VIRTUAL // Implementation public: virtual ~CMarkerZoom(); // Generated message map functions protected: file://{{AFX_...

Excel question #11
How do I lock a row of cells so that anything I type in them will be in upper case? ...

Filter Question 05-20-07
I have a List Box from a query on my form that has a field ClientID and 2 other fields and on my form I have one ClientID, How can I filter the list box to only show the two other fields with the same ClientID in the list Box, Thanks for any Help ...

CFileDialog question
I am using CFileDialog and using this is it possible to capture right click? For my requirement I do not want to process right click. Please let me know if there is a way to do this... Thanks Suchrithaa ...

Recursion Question
I have the following simple recursion and as I unwind the call stack (stepping through it), I am trying figure out where the eventual return value is being stored. Any help is appreciated. Thanks Mike namespace Factorial { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { label1.Text = Factorial(5).ToString(); } public int Factorial(int n) { if(n==0) return 1; ...

Multithread (3)
I've been told that if DLL exported function is thread safe (i.e. doesn't use global scope variables), than I can call that DLL function from my EXE using multiple threads. I want now to ask what about internal DLL functions (that exported DLL function calls)? Example: __declspec( dllexport ) int MyDLLFunc(int val) { addOne(val); } int addOne(int val) { return ++val; } What if multiple MyDLLFunc() (multiple threads) calls addOne()? Is this thread safe? yes, it's thread safe.....the only one problem which could occured is that your function change a data stati...

text function question
Hi all, Hopefully my confuse is not so difficult to help. I have text arrays in column A2:A100 (much datas) which are filled with "n" and such as "loose end", "soft". I want in the column A1 is containing the text excludes "n". Due to of my poor function knowledge i tried to use CONCATENATE function but this is too long ..what should i use the proper formula. txs in advance 4 any helps One guess .. Assuming you want to extract text within A2:A100 that does not contain the letter "n", give this set up a try: Put in B2: =IF(A2=""...

RAID Question
Hi, Can you tell me, for a single Exchange 2003 server, which is better? Two drives configured to RAID 1 or RAID 10 for the entire server or RAID 1 on the system partition and RAID 5 for the rest? Thanks Jeff The answer: It Depends.... and Need More Information! It's fairly impossible to simply give a blanket statement that one is better than the other. There are too many other considerations you have to take into account. What you really need to do is get an accurate measurement of IOPS/user that you will need, and then base your storage needs off that (leaving room for grow...

Vlookup question #4
i have on sheet1 a list of reference data in two columns. column a) has a part code in it and column b) has a piece of data relating to it. in a few examples (infact probably the majority of cases) the code in cell a) is repeated a few times but with different data in cell b) on each occasion. is it possible to do a lookup where it finds and brings back data in column b)on all occasions when a code is found in column a, not just the first of last? does that make sense? cheers Rob Hi you may try downloadind Alan Beban's array functions (http://home.pacbell.net/beban) They'll includ...

Searching Column Question
Hi All, I'm having a problem searching some of my excel sheets and hope someone might have a way to do this. I need to search a workbook with multiple sheets for a specific value only in one column. For an example I have a inventory workbook with lots of sheets. All sheets have a column for barcodes (column F) and I need to search this column for a specific barcode number on all sheets simultaneously. Can this be done? Thanks. Brad --- Message posted from http://www.ExcelForum.com/ Brad I don't believe you can limit your search to only one column on each sheet without VB...

Cryptic question
I apologize for not being more descriptive in my first question money would not copy to CD. I was trying to put the file not money itself onto CD. What I would like to do is put the file to CD and then run money off to CD. I hope I'm wording this right as I am new to computers. It will not copy to a floppy disk neither. But it backs up just fine to CD ...

Simple Question?
I have a form with a command button connected to a query that returns a recordset in datasheet view. One of the fields is EmployeeID which corresponds to tblEmployee.EmployeeID which likewise corresponds to tblEmployee.FirstName and tblEmployee.LastName. As it stands, when I run the query, I have field EmployeeID in the returned recordset. I would like to have the employee's concatenated full name instead. Is this possible? Thanks, Tim Just write "Select tblEmployee.FirstName & " " & tblEmployee.LastName from tblEmployee" tbrogdon@gmail.com wrote: >I ...

WritePrivateProfile multithreaded?
I'm curious if the WritePrivateProfileString function works from various threads and processes? If I have multiple threads accessing (reading/writing) a single .ini file, are there any issues? Or, does the OS handle this properly? Thanks DanB As in any multi-threaded solution, it's probably best that you use a critical section or mutex object to synchronize access to the file between the threads. "Dan Baker" <dbmail> wrote in message news:%23Vo4o%23DLGHA.2780@tk2msftngp13.phx.gbl... > I'm curious if the WritePrivateProfileString function works from vari...

REPOST: basic question re select
Sorry, did the accidental keystroke for send message....oops So how do I select records from a table where a field has a value or is null. I tried : select * from tbl_article where lift in(1 ,null) but that didn't work... AussieRules select * from tbl_article where lift =1 OR lift IS NULL "AussieRules" <nospam@nospam.com> wrote in message news:ezTLn6c8KHA.1424@TK2MSFTNGP04.phx.gbl... > Sorry, did the accidental keystroke for send message....oops > > So how do I select records from a table where a field has a value o...

report question
how can we make a report to see the cash sales detail and credit card sales detail? Hope someone can help me.. Try the Tender Summary Report in Reports --> Miscellaneous --> Tender Summary. There is some additional Tender Summary reports in the Reports Library, one includes Transaction Number. You need to have access to Customer Source in order to download the file. Tender Summary with Transaction Number http://tinyurl.com/9gt4d - Report http://tinyurl.com/djreu - Preview (pdf file) Rob "HOJIN" wrote: > how can we make a report to see the cash sales detail and cred...

xmldocument.preservewhite space question
I have an xmldocument and need to set the preservewhitespace to true as after processing the document I want to write out the xml (using outerxml/innerxml) and have the identations preserved for readability. However with the preservewhite space option set code such as: foreach (XmlNode node in ParentNode) { } fails as the whitespace seems to confuse the parser, how can I work around this? robert@orisoft.co.uk wrote: > I have an xmldocument and need to set the preservewhitespace to true as > after processing the document I want to write out the xml ...

Online Banking Question
I just installed Money 2004 and learned that although my bank has online banking it is not on their list of Financial Institutions. Does anyone know how who I might contact to get it added? Thanks! In microsoft.public.money, Sheesh wrote: >I just installed Money 2004 and learned that although my >bank has online banking it is not on their list of >Financial Institutions. Does anyone know how who I might >contact to get it added? Thanks! You could refer your bank to http://www.microsoft.com/money/partner/default.asp Who is the bank? Hi... it doesn't have to be on...

Notes question
Hello, Which form is GP Notes based on? If you attach a external file, where does it get stored? SQL Server or to a file system with a pointer in the database? Thanks, gsvi OLE notes are stored in a file in the OLE folder under your GP folder. The folder address is defined in the INI file on each workstation and, for them to work correctly, should all be pointing to the same shared folder. -- Richard L. Whaley Author / Consultant / MVP http://www.AccoladePublications.com Documentation for Software Users "gsvi" wrote: > Hello, > > Which form is GP Notes based on?...