none
Miltiple Listening Sockets in One Application

    Question

  •  

    Hi All,

     

        I am looking to build 3 listening sockets in one application. I want to with CAsyncSocket. I could not found any information on the topic from thet net.

     

        Can the seniors help me in this regads,

     

    Thanks

    Thursday, January 24, 2008 11:52 AM

Answers

  • This seems to be pretty straightforward to me.  Just declare three CAsyncSocket instances, and initialize each.  Make sure you bind them to different ports with the Bind function, or they'll bind automatically to randomized ports.  After binding, call Listen to wait for an incoming connection.  When a connection is recieved, the OnAccept method will be invoked.

     

    To handle the OnAccept method, you're going to need to inherit from CAsyncSocket, like so:

     

    Code Snippet

    class MySocket:

      public CAsyncSocket

    {

       void OnAccept(int nErrorCode);

    };

     

    void MySocket::OnAccept(int nErrorCode)

    {

      CAsyncSocket clientSocket;

      this->Accept(clientSocket);

      printf("Accepted a new client!\n");

    }

     

    int main(int argc, const char* argv[])

    {

      MySocket soc1, soc2, soc3;

      sock.Bind(1234);

      sock.Listen();

     

      // ...initialize other sockets as needed...

      while(true)

        Sleep(100);

    }

     

     

    Let me know if this helps.

    Friday, January 25, 2008 4:22 PM