I have to develop an application that communicates with a server (running on the same machine) with UDP datagrams. I started with a dialog based application and derived a class from CAsyncSocket wich I called CRoteiroSockets, in order to receive the data in the overriden OnReceive(int errorCode) function.
I can connect to the server using this code (it sends a XML message):
I know that the server receives this message and responds with other message but I never get anything in my client. The CRoteiroSockets::OnReceive(int nErrorCode) function is never executed. Anyone can explain me this? How can I receive the data sent by the server?
Thanks for your attention!
Last edited by rpOliveira; April 9th, 2004 at 05:01 PM.
Sam, I already had visited the page you recommended (found it through google) and it was a good help to start. What troubles me is that I think I'm doing everything right and I can't receive data. Because it's my first time using sockets in Visual C++, please tell me If something is wrong here:
1) I created a dialog based application through mfc wizard.
2) I derived a class form CAsyncSocket wich I called CRoteiroSockets.
3) I Put a button on the dialog and created the code I presented in my first post in this thread : void CRoteiroDlg::OnBtConnect().
4)I overrode the function OnReceive - void CRoteiroSockets::OnReceive(int nErrorCode) and wrote there AfxMessageBox("message");
The client ant the server are in the same machine. I used the address "localhost" and the port 6000 (where the server is listening). I know that the server receives the message my client sends and I know he answers with another message. Shouldn't the OnReceive function be executed when data arrives to the client and so a Message Box with the text "message" appear?
As far as I know, that should work. I don't know enough to be able to help much. I might try writing my own versio of what you have done, since I do want to learn more too. I have been very tempted to do more like that. Hopefully one of the experts will be able to help you, but if not, then perhaps I will try something.
Look at the SO_REUSEADDR option specified with CAsyncSocket::SetSockOpt. The CAsyncSocket::SetSockOpt documentation seems to indicate that the SO_REUSEADDR option would not apply to your situation, but it is something I found when I was trying to use non-MFC to receive something. It did not help me, but you can try it in case it helps you.
It looks like your OnBtConnect() function creates a CRoteiroSockets object on the local stack and not on the heap. So, as soon as OnBtConnect() finishes its call to CRoteiroSockets::Send(), the function completes and the CRoteiroSockets object is destroyed before it has an opporunity to receive anything from the server.
Create the CRoteiroSockets object on the heap, or make it a member object of CRoteiroDlg.