Thread: Winsock, CAsyncSocket Derivative.

  1. #1
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Winsock, CAsyncSocket Derivative.

    I can connect to a computer and successfully send Data, however I cannot get the Callbacks working correctly(such as OnClose, OnReceive, OnListen, OnConnect, OnAccept etc..). I checked out MSDN and they do something like this:
    Code:
    CMyAsyncSocket::OnClose(int nErrorCode)
    {
    //Their Notification Code here.
    }
    I have not used Derivatives before so I tried to create one of my own. I was connecting by using an Object named 'sock'. This is how I declared my object, then tried to declare the Derivatives:

    Code:
    CAsyncSocket sock;
    class sock : public CAsyncSocket
    {
    virtual void OnClose(int nErrorCode);
    };
    So then I can still make my connections using the 'sock' functions, so then I tried this:

    Code:
    sock::OnClose(int nErrorCode)
    {
    //My Notifications here, yet they are never executed because this function is never called for some reason.
    }
    So when the connection is Closed it is susposed to use the Callback event and notify me by running the function sock::OnClose, however for some reason, it does not.

    I have never used a Derivative of a class like this before, so I am somewhat confused. Any help would be appreciated, I have been using MFC. Thanks.

  2. #2
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Got it.

    I got it working my friends. Wow, I am so happy for some reason. Okay, I'll tell you what I did so that you can know how in the future if you currently don't.

    My First problem was getting confused about CObject and how I was actually putting that in the class, second was that my class was private, only in the scope of a button to make the connection(DUH!) I didnt think about that until... well now. So what I did was something like this:

    Code:
    class MySocket : public CAsyncSocket
    {
    public:
    virtual void OnClose(int nErrorCode);
    virtual void OnConnect(int nErrorCode);
    };
    
    MySocket Sock;
    
    void MySocket::OnClose(int nErrorCode)
    {
    //Connection has been closed.
    }
    
    void MySocket::OnConnect(int nErrorCode)
    {
    //You have connected to your destination.
    }
    
    //Then you just use Sock as the regular Object.
    So like: Sock.Create(etc...);
    Sock.Connect(etc...);
    Then it will run your OnConnect and OnClose functions! Doesn't that make you very happy! I am very happy! (although I will be tired when I wake up) Yay!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  4. weird winsock output and winsock 2 not working
    By whackaxe in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-09-2004, 11:10 AM
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM