Socket stops receiving - Programmer

This is a discussion on Socket stops receiving - Programmer ; Hi I have a pair of applications which communicate via TCP/IP, implemented using the CAsyncSocket class from MFC. The application which acts as the server streams data to the client application, while data is only sent in the other direction ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: Socket stops receiving

  1. Socket stops receiving

    Hi
    I have a pair of applications which communicate via TCP/IP,
    implemented using the CAsyncSocket class from MFC. The application
    which acts as the server streams data to the client application, while
    data is only sent in the other direction on command from the user. The
    server always runs on Win NT 4.0, and seems to work correctly.

    The client application runs fine on Win NT 4.0, but if it is run on
    Win 2000, it stops receiving data after a short time. The socket does
    not seem to be closed (it can still send data the other direction),
    but the OnReceive method just stops getting called. None of the calls
    to Receive before this happens seem to return an error.

    The Win 2000 machines I've tried it on (where it doesn't work) are
    considerably higher performance than the Win NT boxes (where it does
    work), and the data rates are the same.

    I tried running a packet sniffer, and the last receive event on the
    client side returns a DATA NOT ACCEPTED error.

    Any suggestions as to what the problem might be are very welcome !

    Thanks
    Bill

  2. Re: Socket stops receiving

    William Hunt wrote:
    >
    > Hi
    > I have a pair of applications which communicate via TCP/IP,
    > implemented using the CAsyncSocket class from MFC. The application
    > which acts as the server streams data to the client application, while
    > data is only sent in the other direction on command from the user. The
    > server always runs on Win NT 4.0, and seems to work correctly.
    >
    > The client application runs fine on Win NT 4.0, but if it is run on
    > Win 2000, it stops receiving data after a short time. The socket does
    > not seem to be closed (it can still send data the other direction),
    > but the OnReceive method just stops getting called. None of the calls
    > to Receive before this happens seem to return an error.
    >
    > The Win 2000 machines I've tried it on (where it doesn't work) are
    > considerably higher performance than the Win NT boxes (where it does
    > work), and the data rates are the same.
    >
    > I tried running a packet sniffer, and the last receive event on the
    > client side returns a DATA NOT ACCEPTED error.
    >
    > Any suggestions as to what the problem might be are very welcome !
    >
    > Thanks
    > Bill


    One possibility: Check for OnReceive reentering. If you pump Windows
    messages in something called by OnReceive then it can reenter. If you
    don't handle the reentry (i.e. if you fail to call Receive at a later
    time) there will be no more OnReceive's.

    --
    Scott McPhillips [VC++ MVP]

  3. Re: Socket stops receiving

    Scott McPhillips wrote in message news:<3F272292.7676110A@mvps.org>...
    > One possibility: Check for OnReceive reentering. If you pump Windows
    > messages in something called by OnReceive then it can reenter. If you
    > don't handle the reentry (i.e. if you fail to call Receive at a later
    > time) there will be no more OnReceive's.


    Thanks very much for the suggestion. Assuming my method of checking
    for reentrancy is valid, that doesn't seem to be the problem.

    I defined a static variable within OnReceive, initialized to zero. It
    gets incremented as the first operation in OnReceive, and decremented
    as the last operation. I would assume that if OnReceive was
    reentering, the variable would hit a value greater than one, but it
    doesn't.

    If there is a better way of checking for this, I'd appreciate hearing
    about it.

    Thanks again

    Bill

  4. Re: Socket stops receiving

    William Hunt wrote:
    >
    > Scott McPhillips wrote in message news:<3F272292.7676110A@mvps.org>...
    > > One possibility: Check for OnReceive reentering. If you pump Windows
    > > messages in something called by OnReceive then it can reenter. If you
    > > don't handle the reentry (i.e. if you fail to call Receive at a later
    > > time) there will be no more OnReceive's.

    >
    > Thanks very much for the suggestion. Assuming my method of checking
    > for reentrancy is valid, that doesn't seem to be the problem.
    >
    > I defined a static variable within OnReceive, initialized to zero. It
    > gets incremented as the first operation in OnReceive, and decremented
    > as the last operation. I would assume that if OnReceive was
    > reentering, the variable would hit a value greater than one, but it
    > doesn't.
    >
    > If there is a better way of checking for this, I'd appreciate hearing
    > about it.
    >
    > Thanks again
    >
    > Bill


    That sounds good for detecting reentrance. If my speculation is right
    that you are failing, somehow, to acknowledge an OnReceive by calling
    Receive you might contrive a check for this, such as a variable or TRACE
    statements so you can determine which happened last when it freezes up.
    Or, to check the speculated theory itself you might contrive a way
    (temporary button) to call Receive when it freezes up. If the socket is
    simply waiting for you to call Receive then injecting one such call
    should resume normal operation.

    Make sure you are using and checking the return value from Receive: A
    common mistake. The byte counts you will get are probably different on
    the faster machine, which might affect your message reassembly code.

    --
    Scott McPhillips [VC++ MVP]

  5. Re: Socket stops receiving

    William Hunt wrote:
    > Thanks once again, that gives me a few leads to chase down, though the
    > byte counts returned from Receive seem OK.
    >
    > I'm a little unclear on exactly what the requirements are for the
    > OnReceive method - does it have to perform at least one call to
    > Receive() ? One and only one call ? Does it have to read all
    > available data in the buffer ? If it only reads part of the data
    > available, should another OnReceive() be generated as soon as the
    > first one exits ?
    >
    > The MSDN documentation I have doesn't address these questions at all,
    > as far as I can see.
    >
    > Thanks again
    >
    > Bill


    OnReceive is a notification from winsock that there is something to
    read. It will not send another until after you respond by calling
    Receive. That's pretty much the whole story: It pings, you pong. If
    you somehow miss one the game stops.

    You can wait as long as you want and call Receive from wherever you
    want. It doesn't care how many bytes you read or how many times you
    call Receive. (Although if you call more than once you may catch some
    OnReceive notifications that were in a queue even if you have taken all
    the data.)

    Yes, if you only read part of the data another OnReceive will be
    generated shortly after the first one exits. But OnReceive is a Windows
    message handler and comes in through the same queue as mouse and
    painting and all, so there is no "as soon as" guarantee.

    For additional reading see any winsock book, or search MSDN for
    FD_READ. OnReceive is the winsock FD_READ notification, communicated as
    a Windows message in your message queue, converted into a C++ virtual
    function call. I'm out of guesses on your problem, but rest assured that
    it can work very well.

    --
    Scott McPhillips [VC++ MVP]

  6. Re: Socket stops receiving

    Once again, thanks for taking the time to answer my questions - that
    saves me a bit of time going in the wrong direction trying to track
    this problem down. I'm just a poor embedded systems guy who's been
    press ganged into fixing this Windows app I didn't write, so I
    apologize for the pretty basic Windows questions.

    I think I'm going to take a step back and just write a simple program
    to read the data from the socket - there's a lot of other stuff going
    on in this program which may or may not have anything to do with the
    problem, so that may help me narrow it down.

    Thanks again

    Bill

  7. Re: Socket stops receiving problem solved

    Scott McPhillips wrote in message news:<3F29B64D.94C59111@mvps.org>...

    > For additional reading see any winsock book, or search MSDN for
    > FD_READ. OnReceive is the winsock FD_READ notification, communicated as
    > a Windows message in your message queue, converted into a C++ virtual
    > function call. I'm out of guesses on your problem, but rest assured that
    > it can work very well.



    I did search MSDN for FD_READ, and found the following in the
    documentation for the call WSAAsyncSelect:

    If an application issues multiple recv calls in response to a single
    FD_READ, it can receive multiple FD_READ messages. Such an application
    can need to disable FD_READ messages before starting the recv calls by
    calling WSAAsyncSelect with the FD_READ event not set.


    So, I called the method CASyncSocket::AsyncSelect(0) at the beginning
    of my OnReceive, and CASyncSocket::AsyncSelect(FD_READ) at the end,
    and everything works as it should. Thanks Scott for all the help.

    Hope this helps someone who may have the same problem,

    Bill

+ Reply to Thread