-
April 9th, 2004, 04:54 PM
#1
Using CAsyncSocket to communicate!
Hi!
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):
Code:
void CRoteiroDlg::OnBtConnect()
{
CRoteiroSockets sockRoteiro;
if (sockRoteiro.Create(0,SOCK_DGRAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE) == 0)
AfxMessageBox("ceate error");
if (sockRoteiro.Connect("localhost",6000) == 0)
AfxMessageBox("connect error");
char pbuf[500];
strcpy(pbuf,"<Robot Name=\"Roteiro\" Id=\"1\"></Robot>");
sockRoteiro.Send(pbuf,500,0);
}
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.
Ah! Nao ser eu toda a gente e toda a parte!
-
April 9th, 2004, 09:06 PM
#2
It is very difficult to debugger network applications without pretty much having complete access to the all code.
I highly recommend raw winsock.
Kuphryn
-
April 10th, 2004, 03:03 PM
#3
-
April 10th, 2004, 03:52 PM
#4
I know about as much as you do but I might be able to help. Please try my code in my Receiving UDP Using CAsyncSocket and let me know if it works.
-
April 12th, 2004, 11:49 AM
#5
Hi!
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?
Thanks for your attention.
Ah! Nao ser eu toda a gente e toda a parte!
-
April 12th, 2004, 12:58 PM
#6
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.
-
April 12th, 2004, 01:15 PM
#7
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.
-
April 16th, 2004, 07:26 PM
#8
Firstly : why use UDP and not TCP/IP ?
Secondly : CAsyncSocket is unreliable at best so I would use WinSock instead.
Darwen.
-
April 16th, 2004, 07:31 PM
#9
Originally posted by darwen
Firstly : why use UDP and not TCP/IP ?
Because the server I wich to comunicate uses UDP messages. Why? Because it's a real time process.
Secondly : CAsyncSocket is unreliable at best so I would use WinSock instead.
Already started to do that .
Ah! Nao ser eu toda a gente e toda a parte!
-
April 16th, 2004, 08:04 PM
#10
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.
-Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|