Re: Using CAsyncSocket as a server.
- From: "AliR" <AliR@xxxxxxxxxxxxxxxx>
- Date: Wed, 18 May 2005 14:33:05 GMT
Just to add to Scott's comment's (since I am bored)
Here is what my listen socket looks like:
CListenSocket::CListenSocket()
{
}
CListenSocket::~CListenSocket()
{
}
// CListenSocket member functions
//LSMsgReceiver is an abstract class that
//has methods I need to call as a result
//of some socket operations like OnAccept, OnReceive....
BOOL CListenSocket::Listen(UINT Port, LSMsgReceiver *Parent,DWORD &Error)
{
m_Parent = Parent;
m_Port = Port;
if (CSocket::Create(Port))
{
if (CSocket::Listen())
{
return TRUE;
}
}
Error = MAKELONG(1,GetLastError());
return FALSE;
}
void CListenSocket::OnAccept(int nErrorCode)
{
if (m_Parent)
{
CSocket Socket;
if (Accept(Socket))
{
//Note that in my OnAccept I detach the socket handle before sending
it to the NewUser method.
//that's because I don't know which CSocket Derived class NewUser
want's to create. I let him
//create it.
SOCKET hSocket = Socket.Detach();
m_Parent->NewUser(m_Port,hSocket);
}
}
CSocket::OnAccept(nErrorCode);
}
AliR.
"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news:eHndzT6WFHA.3760@xxxxxxxxxxxxxxxxxxxxxxx
> TonyG wrote:
>
> > I have used CAsyncSocket a bunch of times as a client. Now I want to use
it
> > as a server.
> >
> > My plan is make a new class using CAsyncSocket as a base. Then I will
create
> > one instance of my class and listen on the port I need. When someone
> > connects to me I will start sending data using that instance of the
class.
> >
> > Then immediately I will create a second instance of my CAsyncSocket
based
> > class and I will listen on the same port for someone else to connect. If
> > someone connects I will start sending data using that instance. I will
then
> > create a third instance and listen again.
> >
> > Is this the general way that I should be doing this?
>
> No, it does not work that way. One socket does nothing except listen.
> The listening socket does not send or receive data. When each
> connection request is received by the listening socket it calls your
> OnAccept override. Follow the directions for OnAccept.
>
> --
> Scott McPhillips [VC++ MVP]
>
.
- References:
- Using CAsyncSocket as a server.
- From: TonyG
- Re: Using CAsyncSocket as a server.
- From: Scott McPhillips [MVP]
- Using CAsyncSocket as a server.
- Prev by Date: Re: How to start another program by clicking on CButton
- Next by Date: Re: how to set time out in Casyncsocket
- Previous by thread: Re: Using CAsyncSocket as a server.
- Next by thread: Strange CListCtrl problem
- Index(es):