Re: Again with CAsyncSocket
On Nov 28, 12:17 pm, clinisbut <clinis...@gmail.com> wrote:
I noticed that the OnConnect event doesn't fires on any connection. I
suspect I'm doing wrong each client connection.
I think I solved partially, but just for one client connection.
I got this:
class MySocket : public CAsyncSocket
{
// Attributes
public:
// Operations
public:
int numclients;
MySocket* clients;
MySocket();
virtual ~MySocket();
void Open( MySocket* clients);
void enviarDatos();
[...]
void MySocket::Open( MySocket* client)
{
this->clients = client;
this->numclients = 0;
int ret = this->Create( 15000 );
if( Listen()==FALSE )
{
UINT uErr = GetLastError();
TCHAR szError[256];
wsprintf(szError, "Server Receive Socket Create()
failed: %d", uErr);
AfxMessageBox(szError);
}
}
void MySocket::OnAccept(int nErrorCode)
{
if( Accept( *this->clients ) )
{
AfxMessageBox("YEAH");
}
else
{
AfxMessageBox( "ERROR");
}
CAsyncSocket::OnAccept(nErrorCode);
}
void MySocket::SendMyData()
{
int count = this->clientes->Send("C", 2, 0);
if( count==SOCKET_ERROR )
{
AfxMessageBox("BAD");
}
else
{
AfxMessageBox( "OK");
}
}
void MySocket::OnReceive(int nErrorCode)
{
AfxMessageBox("OnReceive");
int iRecvd;
char pBuff[100];
iRecvd = Receive(&pBuff, 100);
//did we recieve anything?
if(iRecvd == SOCKET_ERROR)
{
AfxMessageBox("Socket Error!");
}
else
{
AfxMessageBox("NO socket error");
}
CAsyncSocket::OnReceive(nErrorCode);
}
}
In my Dialog I have two instances of MySocket:
MySocket* server;
MySocket* clients;
In OnInitDialog : clients = new MySocket();
To test all thing:
void CCasyncDlg::OnButtonOpen() //A button to open the server
{
server = new MySocket();
server->Open( this->clientes );
}
void CCasyncDlg::OnSendData() //A button to send data to test
{
server->SendMyData();
}
I think I'm starting to understand the whole thing. But I still have
some questions:
1#
To implement multiplie clients I think I should:
Change the definition of:
"MySocket* clients"
to
"MySocket** clients"
And implement a way to insert/remove new clients every time someone
connects to server.
I tried a CList but (including #include <afxtempl.h> and typing in
MySocket class:
typedef CList <MySocket*, MySocket*> ClientList;
CList clients;
but this throws an error:
error C2955: 'CList' : use of class template requires template
argument list
c:\XXXXXXXXXXXX\vc98\mfc\include\afxtempl.h(653) : see
declaration of 'CList'
2# I inspected the data sended ("C") And the client received (In Hex)
43 00. I suppose that in my test I'm sending a String, but in the
final program I should send an unsigned char array. Should I use the
same function?
Bear in mind that I should be able to send null characters (0x00)
without any complication.