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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
NT_ProgrammerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.