Prev: open .chm file from VC++ - with Table Of Contents hidden - HTM
Next: CButton DrawItem never be called
From: bernd on 3 Jun 2010 06:20 Hi, how is it possible to get the CAsyncSocket-Handle in the OnAccept- method? I don`t want to install one thread for each connection - I`ve one thread for all http-connections. void ConnSoc::OnAccept(int nErrorCode) { //doing some error stuff // New connection is being established CAsyncSocket soc; //using threads / detach / attach if(!Accept(soc)) //error-handling CAsyncSocket::OnAccept(nErrorCode); } If you install a thread for each connection, a new socket is installed with detach / attach handling. But what`s a suitable solution using only one thread? class ConnSoc : public CAsyncSocket class SocketThread : public CWinThread At the moment I`ve installed a protected function within SocketThread virtual ConnSoc * GetSocket() {ASSERT(FALSE); return NULL; } best regards Bernd
From: Scott McPhillips [MVP] on 3 Jun 2010 10:28 "bernd" <bernd.schuster12(a)googlemail.com> wrote in message news:3f3ea884-e2dc-41de-92c7-541cbb20f085(a)z17g2000vbd.googlegroups.com... > Hi, > > how is it possible to get the CAsyncSocket-Handle in the OnAccept- > method? I don`t want to install one thread for each connection - I`ve > one thread for all http-connections. > > > void ConnSoc::OnAccept(int nErrorCode) > { > //doing some error stuff > > // New connection is being established > CAsyncSocket soc; //using threads / detach / attach > > if(!Accept(soc)) //error-handling > > CAsyncSocket::OnAccept(nErrorCode); > } > > If you install a thread for each connection, a new socket is installed > with detach / attach handling. But what`s a suitable solution using > only one thread? > > class ConnSoc : public CAsyncSocket > class SocketThread : public CWinThread > > At the moment I`ve installed a protected function within SocketThread > virtual ConnSoc * GetSocket() {ASSERT(FALSE); return NULL; } > > best regards > Bernd In the OnAccept function of the listener socket you need to create a new object derived from CAsyncSocket, like CDataSoc, and pass it to the listener socket Accept: void ConnSoc::OnAccept(int nErrorCode) { CDataSoc* pSocket = new CDataSoc(); Accept(*pSocket); This new socket will run in the current thread. -- Scott McPhillips [VC++ MVP]
From: Stephen Myers on 3 Jun 2010 10:34 bernd wrote: > Hi, > > how is it possible to get the CAsyncSocket-Handle in the OnAccept- > method? I don`t want to install one thread for each connection - I`ve > one thread for all http-connections. > > > void ConnSoc::OnAccept(int nErrorCode) > { > //doing some error stuff > > // New connection is being established > CAsyncSocket soc; //using threads / detach / attach > > if(!Accept(soc)) //error-handling > > CAsyncSocket::OnAccept(nErrorCode); > } > > If you install a thread for each connection, a new socket is installed > with detach / attach handling. But what`s a suitable solution using > only one thread? > > class ConnSoc : public CAsyncSocket > class SocketThread : public CWinThread > > At the moment I`ve installed a protected function within SocketThread > virtual ConnSoc * GetSocket() {ASSERT(FALSE); return NULL; } > > best regards > Bernd Please note that in your example, soc will go out of scope at the end of OnAccept at which time it will detach and disappear. How about something like the following. (Please note I've extracted it from working code and may have removed something important.) You create a new instance of the client socket class. It can be a separate class or just a new instance of ConnSoc. I've called it CClientSocket. Once you've got the pointer to the new instance you can PostMessage it to a window, store it in a list, ignore it. The new instance can then handle OnConnect and OnSend in order to start sending stuff to the connected socket. Handle OnReceive to retrieve data from the opened socket. void ConnSoc::OnAccept(int nErrorCode) { // check nErrorCode CClientSocket *pSocket = new CClientSocket(); if(!Accept(*pSocket)){ // Error handling } CAsyncSocket::OnAccept(nErrorCode); } HTH Steve
From: Joseph M. Newcomer on 3 Jun 2010 10:59 See below... On Thu, 3 Jun 2010 03:20:49 -0700 (PDT), bernd <bernd.schuster12(a)googlemail.com> wrote: >Hi, > >how is it possible to get the CAsyncSocket-Handle in the OnAccept- >method? I don`t want to install one thread for each connection - I`ve >one thread for all http-connections. > > >void ConnSoc::OnAccept(int nErrorCode) >{ > //doing some error stuff > > // New connection is being established > CAsyncSocket soc; //using threads / detach / attach > > if(!Accept(soc)) //error-handling **** See my essay on UI thread on my MVP Tips site, where I explicitly cover this issue. joe **** > > CAsyncSocket::OnAccept(nErrorCode); >} > >If you install a thread for each connection, a new socket is installed >with detach / attach handling. But what`s a suitable solution using >only one thread? > >class ConnSoc : public CAsyncSocket >class SocketThread : public CWinThread > >At the moment I`ve installed a protected function within SocketThread >virtual ConnSoc * GetSocket() {ASSERT(FALSE); return NULL; } **** Actually, you want to do a Detach() because you are probably going to hand this socket handle over to a secondary thread to deal with it, and it will need to do an Attach() to get the socket in *its* handle table (this is what I cover in my essay on UI threads) joe **** > >best regards >Bernd Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: bernd on 3 Jun 2010 12:08 > Actually, you want to do a Detach() because you are probably going to hand this socket > handle over to a secondary thread to deal with it, and it will need to do an Attach() to > get the socket in *its* handle table (this is what I cover in my essay on UI threads) > joe Using only one thread to create and to establish the socket is not a good choice? I don`t need one thread for each connection, do I? I`ve one cpp: HttpSoc.cpp to create the socket - socket.create() and socket.listen(); moreover I`ve one cpp: ConnSoc.cpp to do all the stuff which is equal to all sockets (OnReceive, Accept, Send - methods). And now, I`ll need one more cpp file to handle the thread in the accept() method? best regards Bernd
|
Next
|
Last
Pages: 1 2 Prev: open .chm file from VC++ - with Table Of Contents hidden - HTM Next: CButton DrawItem never be called |