From: bernd on
On 3 Jun., 18:08, bernd <bernd.schuste...(a)googlemail.com> wrote:
> > 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

ok I`ve installed the third cpp class (HttpConnThread) and I get the
socket handle to this thread (after a successful accept()-method); but
after the accept()-method the OnConnect()-method won`t be called to
receive the recieved http packets....

class ConnSoc : public CAsyncSocket
virtual void OnAccept(int nErrorCode); //accept-method
virtual void OnConnect(int nErrorCode);

void ConnSoc::OnConnect(int nErrorCode)
{
TRACE("connect method not called so far");

}

void ConnSoc::OnAccept(int nErrorCode)
{
/* only some code is shown without error handling */

HttpConnThread* pThread =
(HttpConnThread*)AfxBeginThread(RUNTIME_CLASS(HttpConnThread),

THREAD_PRIORITY_NORMAL,
0,

CREATE_SUSPENDED);

pThread->SetSocket(soc.Detach());
pThread->ResumeThread();

CAsyncSocket::OnAccept(nErrorCode);
}


BOOL HttpConnThread::InitInstance()
{
sock.Attach(mysocket);
return TRUE;
}

best regards
Bernd
From: Joseph M. Newcomer on
See below...
On Thu, 3 Jun 2010 09:08:57 -0700 (PDT), bernd <bernd.schuster12(a)googlemail.com> wrote:

>> 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?
****
No, with CAsyncSocket, you don't need multiple threads; usually a single thread suffices.
In the case of a single thread, there is no need to get the raw SOCKET object.

WHat you need in your thread is a CAsyncSocket* for each active socket; arrays and lists
are not bad ideas to use to keep this.
****
>
>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).
****
Note that the socket creation/binding/listening is asynchronous, so once you have created
the socket, you just go away and wait for the OnAccept notfications. How you package
these into .cpp files is a minor implmenetation decision.
****
>And now, I`ll need one more cpp file to handle the thread in the
>accept() method?
****
If you feel you need a thread (this would be required if the socket messages impose
serious computing burdens you would not want to sustain in the main GUI thread). But in
many cases, you don't need a separate thread. This decision is based on either measured
or expected performance issues. In one implementation, the messages took so little effort
I had only one thread to handle dozens of simultaneous connections; in another, because of
the tremendous processing required for each message, each socket ran in a separate thread.
So you need to know why you want separate threads and base your decisions on this
reasoning.

I'm not sure why you are concerned with file counts. Files are used to manage how you
organize your code, and file counts really are completely irrelevant to project design. I
typically work in systems with > 300 files and often > 400 files. So never, EVER let the
concept of "one more file" dictate how you write your code, unless it is the case of "I
can separate this out nicely if I create just one more file". But you seemed to be
reacting as if the number of files increasing is somehow *bad* (it never is, as far as I
can tell).
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: Joseph M. Newcomer on
See below...
On Thu, 3 Jun 2010 10:35:36 -0700 (PDT), bernd <bernd.schuster12(a)googlemail.com> wrote:

>On 3 Jun., 18:08, bernd <bernd.schuste...(a)googlemail.com> wrote:
>> > 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
>
>ok I`ve installed the third cpp class (HttpConnThread) and I get the
>socket handle to this thread (after a successful accept()-method); but
>after the accept()-method the OnConnect()-method won`t be called to
>receive the recieved http packets....
>
>class ConnSoc : public CAsyncSocket
>virtual void OnAccept(int nErrorCode); //accept-method
>virtual void OnConnect(int nErrorCode);
>
>void ConnSoc::OnConnect(int nErrorCode)
>{
> TRACE("connect method not called so far");
>
>}
>
>void ConnSoc::OnAccept(int nErrorCode)
>{
>/* only some code is shown without error handling */
>
>HttpConnThread* pThread =
>(HttpConnThread*)AfxBeginThread(RUNTIME_CLASS(HttpConnThread),
>
>THREAD_PRIORITY_NORMAL,
> 0,
>
>CREATE_SUSPENDED);
>
>pThread->SetSocket(soc.Detach());
****
What is the type of soc, where is it declared, etc.?
***
>pThread->ResumeThread();
>
>CAsyncSocket::OnAccept(nErrorCode);
>}
>
>
>BOOL HttpConnThread::InitInstance()
>{
> sock.Attach(mysocket);
> return TRUE;
>}
>
****
OK, what is the type of sock, where is it declared, etc.? I don't see that here...
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
On 3 Jun., 21:15, Joseph M. Newcomer <newco...(a)flounder.com> wrote:
> See below...
>
>
>
>
>
> On Thu, 3 Jun 2010 10:35:36 -0700 (PDT), bernd <bernd.schuste...(a)googlemail.com> wrote:
> >On 3 Jun., 18:08, bernd <bernd.schuste...(a)googlemail.com> wrote:
> >> > 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
>
> >ok I`ve installed the third cpp class (HttpConnThread) and I get the
> >socket handle to this thread (after a successful accept()-method); but
> >after the accept()-method the OnConnect()-method won`t be called to
> >receive the recieved http packets....
>
> >class ConnSoc : public CAsyncSocket
> >virtual void OnAccept(int nErrorCode);        //accept-method
> >virtual void OnConnect(int nErrorCode);
>
> >void ConnSoc::OnConnect(int nErrorCode)
> >{
> >    TRACE("connect method not called so far");
>
> >}
>
> >void ConnSoc::OnAccept(int nErrorCode)
> >{
> >/* only some code is shown without error handling */
>
> >HttpConnThread* pThread =
> >(HttpConnThread*)AfxBeginThread(RUNTIME_CLASS(HttpConnThread),
>
> >THREAD_PRIORITY_NORMAL,
> >                                                            0,
>
> >CREATE_SUSPENDED);
>
> >pThread->SetSocket(soc.Detach());
>
> ****
> What is the type of soc, where is it declared, etc.?
> ***>pThread->ResumeThread();
>
> >CAsyncSocket::OnAccept(nErrorCode);
> >}
>
> >BOOL HttpConnThread::InitInstance()
> >{
> >     sock.Attach(mysocket);
> >     return TRUE;
> >}
>
> ****
> OK, what is the type of sock, where is it declared, etc.?  I don't see that here...
>                         joe

sock is declard in class HttpConnThread : public CWinThread

public:
void SetSocket(SOCKET h) { mysocket = h; }
SOCKET mysocket;
CAsyncSocket sock;


Bernd

From: Joseph M. Newcomer on
See below...
On Thu, 3 Jun 2010 23:21:10 -0700 (PDT), bernd <bernd.schuster12(a)googlemail.com> wrote:

>On 3 Jun., 21:15, Joseph M. Newcomer <newco...(a)flounder.com> wrote:
>> See below...
>>
>>
>>
>>
>>
>> On Thu, 3 Jun 2010 10:35:36 -0700 (PDT), bernd <bernd.schuste...(a)googlemail.com> wrote:
>> >On 3 Jun., 18:08, bernd <bernd.schuste...(a)googlemail.com> wrote:
>> >> > 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
>>
>> >ok I`ve installed the third cpp class (HttpConnThread) and I get the
>> >socket handle to this thread (after a successful accept()-method); but
>> >after the accept()-method the OnConnect()-method won`t be called to
>> >receive the recieved http packets....
>>
>> >class ConnSoc : public CAsyncSocket
>> >virtual void OnAccept(int nErrorCode); � � � �//accept-method
>> >virtual void OnConnect(int nErrorCode);
>>
>> >void ConnSoc::OnConnect(int nErrorCode)
>> >{
>> > � �TRACE("connect method not called so far");
>>
>> >}
>>
>> >void ConnSoc::OnAccept(int nErrorCode)
>> >{
>> >/* only some code is shown without error handling */
>>
>> >HttpConnThread* pThread =
>> >(HttpConnThread*)AfxBeginThread(RUNTIME_CLASS(HttpConnThread),
>>
>> >THREAD_PRIORITY_NORMAL,
>> > � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �0,
>>
>> >CREATE_SUSPENDED);
>>
>> >pThread->SetSocket(soc.Detach());
>>
>> ****
>> What is the type of soc, where is it declared, etc.?
>> ***>pThread->ResumeThread();
>>
>> >CAsyncSocket::OnAccept(nErrorCode);
>> >}
>>
>> >BOOL HttpConnThread::InitInstance()
>> >{
>> > � � sock.Attach(mysocket);
>> > � � return TRUE;
>> >}
>>
>> ****
>> OK, what is the type of sock, where is it declared, etc.? �I don't see that here...
>> � � � � � � � � � � � � joe
>
>sock is declard in class HttpConnThread : public CWinThread
>
>public:
> void SetSocket(SOCKET h) { mysocket = h; }
> SOCKET mysocket;
> CAsyncSocket sock;
>
>
>Bernd
****
A CWinThread object is allocated on the heap, so the address of mysocket and the address
of sock will both be heap addresses.
joe
****
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm