Mystery solved: When I do a NetLibSocketClose(), there are still data 
in the socket. I had to put in code like this (based on code someone
posted last year ...)

Note that the NetLibReceive() must be there. Just doing the linger=0,
NetLibSocketShutdown(), NetLibSocketClose() is not sufficient.

This with OS 3.5.

I think I'd be screwed if I try to close a streaming socket where
the server will not stop sending me data :-( anyway, I am not up to
that part yet ....

- Ioi


static SWord
SocketCloseForcefully(int sock, int delay, Err * errp) {
    SWord retVal;
    Err err;
    NetSocketLingerType ltype;

    while (1) {
        char c;
        retVal = NetLibReceive(AppNetRefnum, sock, &c, 1, 0, NULL, NULL,
                               10, errp);
        if (retVal != 1) {
            break;
        } else {
            putchar('_');
            putchar(c);
        }
    }


    //set the socket's SO_LINGER option to "0", so it'll close immediately
    //when we call NetLibSocketClose()
    ltype.onOff = true;
    ltype.time = 0;
    retVal = NetLibSocketOptionSet(AppNetRefnum, sock,
                                   netSocketOptLevelSocket,
                                   netSocketOptSockLinger, &ltype,
                                   sizeof(ltype), -1, &err);

    // ASSERT(retVal == 0);

    //gracefully try and shutdown the socket by sending TCP_FIN on the socket
    retVal = NetLibSocketShutdown(AppNetRefnum, sock, netSocketDirBoth, -1,
                                  errp);

    // ASSERT(retVal == 0);

    //give a delay here to allow the server time to acknowledge the TCP_FIN
    //before we force the socket close
    delay = 0;
    if (delay != 0) {
        SysTaskDelay(delay * SysTicksPerSecond());
    }

    //forcefully close the socket
    retVal = NetLibSocketClose(AppNetRefnum, sock, -1, errp);

    return retVal;
}



Ioi Lam wrote:
> 
> I have an app that makes many TCP socket connections to a server. If
> I run on POSE, the app runs fine with thousands of sockets opened
> and closed. However, on an actual device, using Direct Serial
> with mochaPPP, the app hangs after about 60~100 connections.
> 
> It seems the app hangs while inside NetUTCPOpen(). The socket
> was established (my server's accept() function returns) but
> no data seem to get through.
> 
> At this point, if I quit my test app and try to use another
> app that also makes a TCP connection, the second app also hangs.
> 
> To fix it, I have to Disconnect at the Palm side and restart MochaPPP on
> the PC side. Nothing less than that seem to work.
> 
> Does this sound familiar to anyone? Is this a MochaPPP problem?
> How should I debug this?
> 
> I have been using POSE with redirected TCP/IP. I guess I can
> try to run a null modem to connect POSE to mochaPPP. Does anyone
> know if the behavior of POSE with null modem is similar to a
> real device running mochaPPP?
> 
> Thanks!

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to