I am observing a problem in the TCP/IP stack when attempting to write data
to a non-blocking TCP/IP stream socket immediately after NetLibSelect
indicates that the socket is writeable (following a connect).  In this case
NetLibSend returns with err = netErrSocketNotConnected;  If, however, I
introduce a 20-tick delay (via SysTaskDelay(20)) between the NetLibSelect
call that indicated completion of connection-establishment and the first
NetLibSend call, then everything seems to work.  I am observing this on both
PalmOS v3.1 and v3.5.

The code snippets are at the end.


Thank you,


Vitaly


void test (void)
{
...

  s = PrvTCPSocketOpenNB (&err);

  PrvSocketConnect (s, ipAddrNetByteOrder, portNetByteOrder, &err);
  netFDZero (&readFDs);
  netFDZero (&writeFDs);
  netFDZero (&exceptFDs);

  netFDSet (netIfP, s, &writeFDs);

  result = NetLibSelect (gP->netLibRefNum, netFDSetSize,
                                                   NULL, writeFDsP, NULL,
timeout,
                                                   &err);
  // Check for error
  if (result < 0)
        {
          goto Exit;
        }

  // Check for timeout
  if (result == 0)
        {
          goto Exit;
        }

  if (!netFDIsSet(s, &writeFDs))
        {
          goto Exit; // writeable is not set, therefore no connection
        }

  // SysTaskDelay(20);  // <-- UNCOMMENTING THIS APPEARS TO FIX THE PROBLEM

  numSent = NetLibSend (gP->netLibRefNum, socket,
                                                bufP, bufLen, 0 /*flags*/,
                                                NULL /*toAddrP*/, 0
/*toLen*/,
                                                -1 /*timeout*/, &err);

  // here we usually end up with netErrSocketNotConnected, unless the
SysTaskDelay(20) call
  // above is enabled.
}


// Create a non-blocking socket
static NetSocketRef
PrvTCPSocketOpenNB (Err* errP)
{
  NetSocketRef  s = -1;
  Boolean               nonBlockingOn;

  *errP = 0;


  // Create a TCP/IP socket
  s = NetLibSocketOpen (gP->netLibRefNum, netSocketAddrINET,
                                                netSocketTypeStream, 0, -1
/*timeout*/, errP);

  if (s < 0)
        {
          return (s);
        }
  
  // Place socket into non-blocking mode
  nonBlockingOn = true;
  NetLibSocketOptionSet (gP->netLibRefNum, s, netSocketOptLevelSocket,
        
netSocketOptSockNonBlocking, &nonBlockingOn,
                                                 sizeof(nonBlockingOn), -1
/*timeout*/, errP);
  return (s);

} // PrvTCPSocketOpenNB 


// Connect a non-blocking socket
static Int16
PrvSocketConnect (NetSocketRef socket,
                                        NetIPAddr inet, UInt16 port, Err*
errP)
{
  Int16 result;
  NetSocketAddrINType addrIN;

  *errP = 0;

  // Initiate connection
  addrIN.family = netSocketAddrINET;    // hbo
  addrIN.addr = inet;                                   // nbo
  addrIN.port = port;                                   // nbo
  result = NetLibSocketConnect (gP->netLibRefNum, socket,
        
(NetSocketAddrType*)&addrIN, sizeof(addrIN),
                                                                -1
/*timeout*/, errP);
  if (result < 0)

  return (result);
} // PrvSocketConnect


-- 
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