CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: CAsyncSocket

  1. #1
    Join Date
    Jul 1999
    Posts
    3

    CAsyncSocket

    Hi,
    I have derived from CAsyncsocket and created a Datagram socket on some port, and implemented OnReceive, and i have tried sending data from other application, Only thing i want to know whether OnReceive message is posted for Datagram socket, if so, this is not working in my app, can you people put some points where i may gone wrong in implementing it.

    Thanks,
    M.Srinivas


  2. #2
    Join Date
    Jul 1999
    Location
    Pasadena, CA - USA
    Posts
    351

    Re: CAsyncSocket

    Hi,

    This is from the online help...

    Example



    void CMyAsyncSocket::OnReceive(int nErrorCode) // CMyAsyncSocket is
    // derived from CAsyncSocket
    {
    static int i=0;

    i++;

    TCHAR buff[4096];
    int nRead;
    nRead = Receive(buff, 4096);

    switch (nRead)
    {
    case 0:
    Close();
    break;
    case SOCKET_ERROR:
    if (GetLastError() != WSAEWOULDBLOCK)
    {
    AfxMessageBox ("Error occurred");
    Close();
    }
    break;
    default:
    buff[nRead] = 0; //terminate the string
    CString szTemp(buff);
    m_strRecv += szTemp; // m_strRecv is a CString declared
    // in CMyAsyncSocket
    if (szTemp.CompareNoCase("bye") == 0 )
    ShutDown();
    }
    CAsyncSocket::OnReceive(nErrorCode);
    }






    Regards,

    Paul Belikian

  3. #3
    Join Date
    Jul 1999
    Posts
    3

    Re: CAsyncSocket

    Hi

    Thanks for it,, but my problem is iam not getting the notification OnReceive() itself,.,,, infact
    i kept the breakpoint at the begining of OnReceive which never occurs.

    M.Srinivas



  4. #4
    Join Date
    Jul 1999
    Location
    Pasadena, CA - USA
    Posts
    351

    Re: CAsyncSocket

    Oh,

    How did you create the socket? And are you sure both applications are 'talking' on the
    same port number? There really isn't much you need to do in the UDP server side.




    Regards,

    Paul Belikian

  5. #5
    Join Date
    Jul 1999
    Location
    Pasadena, CA - USA
    Posts
    351

    Re: CAsyncSocket

    Hi again,

    here is a code fragment out of one of my old apps. After the two functions,
    your socket should be able to get data from a client.



    // m_nDGramServerPort = port number, i.e. 0x3f
    // m_pDGramSocket-> = Derived CAsyncSocket class



    if(m_pDGramSocket->Create(m_nDGramServerPort, SOCK_DGRAM, 0)==0)
    {
    TRACE("CSockServer:: Initialize(...) Create Failed.\n");
    return -1;
    }
    if(m_pDGramSocket->AsyncSelect(FD_READ|FD_WRITE)==0)
    {
    TRACE("CSockServer:: Initialize(...) AsyncSelect Failed.\n");
    return -1;
    }




    This application worked fine. Check again that both sides are on the same port number.



    Regards,

    Paul Belikian

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