CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 1999
    Location
    India
    Posts
    2

    Implementing non-blocking sockets



    Hi Friends,


    By default member functions( e.g. Connect(), Receive() ) of CSocket class operate in blocking mode, how do I change it to non-blocking mode.


    Also is there any way I can specify timeout value for such type of functions, especially Receive() function ??


    Regards,

    Navin.

  2. #2
    Join Date
    May 1999
    Location
    Glendale Heights, Illinois, USA
    Posts
    44

    Re: Implementing non-blocking sockets



    To make it non-blocking, use CAsyncSocket instead. CSocket is derived from CAsyncSocket, and what it adds is the blocking code.

  3. #3
    Join Date
    Apr 1999
    Posts
    18

    Re: Implementing non-blocking sockets



    Dear navin,

    use a derived class from CSocket and try, a sample can be like this

    class NavinSocekt:ublic CSocket

    {

    public:

    NavinSocket();

    ~NavinSocket();

    /*all these function are the local implementation of the virtuall callback functions

    provided in the CAsyncSocket*/

    void OnReceive(int nErrorCode); //this function willbe called when paket arrives

    void OnSend(int nErrorCode); // will be called when a packet is sent

    void OnConnect(int nErrorCode); // will be called when gets connected

    void OnAccept(int nErrorCode); // will be called when accepted

    void OnError(int nErrorCode); // will be calle dwhen erro happpends

    void OnClose(int nErrorCode); //

    }

    // all the virtual call back function should be locally implemented depending on your need

    void NavinSocekt::OnReceive(int nErrorCode)

    {

    /*will be called when a packet arrives*/

    if(nErrorCode)

    printf("error in on receive of NAvin socket"

    char buffer[max_size];

    /* this function call won't be a blocking call*/

    int recdBytes = receive(buffer,max_size,.....);

    /*received packet is available at buffer*/

    return;

    }

    with this you don't have to poll for the packet arrival, instead it will call back your virtual functions

    whenevera packet is arrived.

    hope this will solv your issue.

    thanks

    bsmanoj





  4. #4
    Join Date
    Apr 1999
    Posts
    13

    Re: Implementing non-blocking sockets

    this makes the socket non-blocking
    or else AsyncSocket will do....

    ioctl (Socket, FIONBIO, &action);


  5. #5
    Join Date
    May 1999
    Posts
    8

    Re: Implementing non-blocking sockets

    see Timeout with MFC sockets here


  6. #6
    Join Date
    May 1999
    Location
    Mass, USA.
    Posts
    103

    Re: Implementing non-blocking sockets


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