Greetings,

Any answers or suggestions to books, papers or samples would be most
appreciated.

The following code finds and opens the network library, opens a socket, sets
the socket to non-blocking, and then connects to the server.

The 2nd (processSocket) function reads the socket.

When there is no more data the NetLibReceive() function in processSocket()
blocks.

Why?  Any ideas would be appreciated.

Thanks,

Jon

=========  Code snippet ===============
static BOOL SetupNetwork()
{
 BOOL bRet = true;
 Err error;
 UInt16 ifErrs;
 NetSocketAddrINType *inetAddrP;
 BOOL blockFlag = false;

// Get the Network library reference #

 error = SysLibFind("Net.lib", &AppNetRefNum);
 if( error == sysErrLibNotFound )
 {
  MessageBox( "Can not find net.lib" );
  FrmGotoForm(MainForm);
  return(false);
 }

// Open the Network Library

 error = NetLibOpen(AppNetRefNum, &ifErrs);
 if( !error || error == netErrAlreadyOpen ) // Could be previously opened
 {
 }
 else
 {
  MessageBox( "Can not open net.lib" );
  FrmGotoForm(MainForm);
  return(false);
 }

 AppNetTimeout = 1 * SysTicksPerSecond(); // one second timeouts

// Open the streaming TCP socket

 AppNetSocketRef = NetLibSocketOpen( AppNetRefNum,
     netSocketAddrINET,
     netSocketTypeStream,
     0,
     AppNetTimeout,
     &ifErrs);

 if( !AppNetSocketRef )
 {
  MessageBox( "Can not open Socket" );
  FrmGotoForm(MainForm);
  return(false);
 }

// Set the socket to not block on read

 if( NetLibSocketOptionSet( AppNetRefNum,
    AppNetSocketRef,
    netSocketOptLevelSocket,
    netSocketOptSockNonBlocking,
    &blockFlag,
    sizeof(blockFlag),
    AppNetTimeout,
    &ifErrs) )
 {
  MessageBox( "Could not set non-blocking on socket" );
  FrmGotoForm(MainForm);
  return(false);
 }

// Check it just in case

 if( blockFlag )
  MessageBox("blockFlag returned set");

// Create a connection to 10.0.0.24:3000

 inetAddrP = (NetSocketAddrINType *) &AppNetSockAddr;
 inetAddrP->family = netSocketAddrINET;
 inetAddrP->port = 3000;
 inetAddrP->addr = 0x0a000018;

// Connect to server

 if( NetLibSocketConnect( AppNetRefNum,
    AppNetSocketRef,
    &AppNetSockAddr,
    sizeof(AppNetSockAddr),
    AppNetTimeout,
    &ifErrs ) )
 {
  MessageBox( "Can not connect to Socket" );
  FrmGotoForm(MainForm);
  return(false);
 }

// Done

 return(true);
}

////////////////////////////////////////////////////////////////////////////
/////////////////////////

static UInt16 processSocket(char *inBuffer, UInt16 inBufSize)
{
 Int16 bytesRead;
 Err  error;

// Receive a datablock

 bytesRead = NetLibReceive( AppNetRefNum,
  AppNetSocketRef,
  inBuffer,
  inBufSize,
  0,
  NULL,
  NULL,
  AppNetTimeout,
  &error);

// Was there an error?  (must deal with a '0' meaning closed socket)

 if( bytesRead < 0 )
 {
  MessageBox("Would have Blocked?");
  bytesRead = 0;
  gWaitCount++;
 }

 return( bytesRead );
}



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

Reply via email to