MFC Sockets

Currently I am developing an application using MFC Socket programming. I want to get notification for the events occured for the connected sockets. I have derived my socket class from CAsyncSocket and added virtual callback functions using ClassWizard. But I couldn't get notified on certain events like OnConnect, OnSend...Why? Thanks in advance.
kbnbabuAsked:
Who is Participating?
 
NT_ProgrammerConnect With a Mentor Commented:
You will not get OnSend( ) and other forms of notification depending on whether you are using blocking or non-blocking sockets.

If your socket is a blocking socket when you call Send( ) it loops until it gets the transmit finished.  If it is non-blocking and you call Send( ), it returns imediately if it cant transmit all of the data and when the socket layer frees up to do the transmit your OnSend( ) will get called to let you know to try to send the data again.

If you use CSocket your OnConnect never gets called.  Basically the same thing. When you call Connect( ) it waits unti the connection either fails or succeeds.  If you were using CAsyncSocket Connect would return imediatly and you would get a OnConnect( ) call whenever the socket connection actually went through or failed.

I can send you a MFC sample app if you want that uses CSocket to do both ends of the connection.
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

While creatign the socket, have u specified the proper flags for
'lEvent ' parameter? like

FD_READ
FD_WRITE
FD_OOB
FD_ACCEPT
FD_CONNECT
FD_CLOSE  

Before that, is ur socket created successfully?
Have u called AfxInitSocket() before creating?

U can use GetLatsError() to get the error identification.

For Ur calls which is derived from CAsyncSocket, by using class wizard u can map the functions like OnConnect(), OnSend(), OnReceive()...

Try it out.

0
 
sun307Commented:
Hi Kbn!
use the function ASyncSelect for that.
This is a member function of CASncSelect class. This has to be called in your program before using the Connect() like this :
ASncSelect(FD_READ | FD_CONNECT | FD_WRITE);

pass all the notification flags as shown above and it will work. You can use it any time you want to in your application and thus change your options any where in the program. You will receive the notifications for only the events that you pass as parameters to thois function.

Best of Luck
Sun307

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kbnbabuAuthor Commented:
The Create() function itself contains these vaules..Any way, I tried this. But it didn't work. I am getting all the return codes as successful. But it is not going into OnConnect().
0
 
cyberfrankCommented:
Hi!

Have You tried to define following functions too -- just for test?
      virtual int Receive(void* lpBuf, int nBufLen, int nFlags = 0);
      virtual int Send(const void* lpBuf, int nBufLen, int nFlags = 0);
Did they called?

CF
0
 
manloreCommented:
┬┐Have your program (or Thread that has the CAsyncSocket object) Message Loop?
If you are programing console aplication
you need to add:
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

If you make a Thread to manage your socket whitout use AfxBeginThread and CWinThread you need to add the code before too.
0
 
srinivas_gariCommented:
u should pass the flags to which u want get event notification. like
FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT

sekhar
0
 
srinivas_gariCommented:
in create method u should pass the flags to which u want get event notification. like
FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT

0
All Courses

From novice to tech pro — start learning today.