CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2004
    Posts
    5

    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!

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    It is very difficult to debugger network applications without pretty much having complete access to the all code.

    I highly recommend raw winsock.

    Kuphryn

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]

  4. #4
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  5. #5
    Join Date
    Apr 2004
    Posts
    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!

  6. #6
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  7. #7
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  8. #8
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    Firstly : why use UDP and not TCP/IP ?

    Secondly : CAsyncSocket is unreliable at best so I would use WinSock instead.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  9. #9
    Join Date
    Apr 2004
    Posts
    5
    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!

  10. #10
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556
    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
  •  





Click Here to Expand Forum to Full Width

Featured