Click to See Complete Forum and Search --> : CAsyncSocket
M Srinivas
July 29th, 1999, 12:02 PM
Hi,
I have derived from CAsyncsocket and created a Datagram socket on some port, and implemented OnReceive, and i have tried sending data from other application, Only thing i want to know whether OnReceive message is posted for Datagram socket, if so, this is not working in my app, can you people put some points where i may gone wrong in implementing it.
Thanks,
M.Srinivas
Paul Belikian
July 29th, 1999, 12:20 PM
Hi,
This is from the online help...
Example
void CMyAsyncSocket::OnReceive(int nErrorCode) // CMyAsyncSocket is
// derived from CAsyncSocket
{
static int i=0;
i++;
TCHAR buff[4096];
int nRead;
nRead = Receive(buff, 4096);
switch (nRead)
{
case 0:
Close();
break;
case SOCKET_ERROR:
if (GetLastError() != WSAEWOULDBLOCK)
{
AfxMessageBox ("Error occurred");
Close();
}
break;
default:
buff[nRead] = 0; //terminate the string
CString szTemp(buff);
m_strRecv += szTemp; // m_strRecv is a CString declared
// in CMyAsyncSocket
if (szTemp.CompareNoCase("bye") == 0 )
ShutDown();
}
CAsyncSocket::OnReceive(nErrorCode);
}
Regards,
Paul Belikian
M Srinivas
July 29th, 1999, 05:07 PM
Hi
Thanks for it,, but my problem is iam not getting the notification OnReceive() itself,.,,, infact
i kept the breakpoint at the begining of OnReceive which never occurs.
M.Srinivas
Paul Belikian
July 29th, 1999, 10:16 PM
Oh,
How did you create the socket? And are you sure both applications are 'talking' on the
same port number? There really isn't much you need to do in the UDP server side.
Regards,
Paul Belikian
Paul Belikian
July 29th, 1999, 10:24 PM
Hi again,
here is a code fragment out of one of my old apps. After the two functions,
your socket should be able to get data from a client.
// m_nDGramServerPort = port number, i.e. 0x3f
// m_pDGramSocket-> = Derived CAsyncSocket class
if(m_pDGramSocket->Create(m_nDGramServerPort, SOCK_DGRAM, 0)==0)
{
TRACE("CSockServer:: Initialize(...) Create Failed.\n");
return -1;
}
if(m_pDGramSocket->AsyncSelect(FD_READ|FD_WRITE)==0)
{
TRACE("CSockServer:: Initialize(...) AsyncSelect Failed.\n");
return -1;
}
This application worked fine. Check again that both sides are on the same port number.
Regards,
Paul Belikian
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.