July 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Palm OS Communications Primer: Net Library

  • August 11, 2004
  • By Alex Gusev
  • Send Email »
  • More Articles »

Establishing a Connection

Once a socket is opened and configured, you either can establish a connection to the remote host and/or send/receive data or setup the socket to listen to incoming connections. First, let's take a look at the client code. Depending on the socket type, the work flow will be a bit different. Stream sockets in fact require establishing a connection before any transmission operations. In case of UDP, you can provide the host address as one of parameters. If the UDP socket was previously connected, you'll get an error if you specify a host address. Hence, you may change the next code snippet according to your needs or skip it in case of UDP or Raw sockets:

NetIPAddr addr = NetLibAddrAToIN(libRefnum, textIP);

// Connect the socket to its destination
NetSocketAddrINType destAddr;
MemSet(&destAddr, sizeof(destAddr), 0);
destAddr.family = netSocketAddrINET;    // This should match the
                                        // second argument to
                                        // NetLibSocketOpen
destAddr.port = 8080;
destAddr.addr = addr;
Err error = 0;

result =
   NetLibSocketConnect(libRefnum,                     // Network
                                                      // library
                       socket,                        // Socket
                                                      // reference
                       (NetSocketAddrType*)&destAddr, // Destination
                                                      // address
                       sizeof(destAddr),              // Length of
                                                      // destAddr
                       -1,                            // Timeout
                       &error                         // Error result
                       );
if (result == -1)
{
   // handle error
   goto CloseSocket;
}

If you develop a server application, a set of calls will be like the following sample:

Err SetupServerSocket(NetSocketRef socket)
{
   Err error;
   NetSocketAddrINType addr;
   addr.family = netSocketAddrINET;
   addr.port = NetHToNS(8080);
   addr.addr = NetHToNL(0);

   if (NetLibSocketBind(libRefnum, socket, &addr, sizeof(addr), -1,
                        &error) != -1)
      error = errNone;

   if (NetLibSocketListen(libRefnum, socket, 5, -1, &error) != -1)
      error = errNone;

      return error;
}

To retrieve the host address, you may use NetLibGetHostByName and analogous functions.

Transmitting the Data

Let's say several words about sending/receiving. There is a rich family of such functions:

Int16 NetLibSendPB(UInt16 libRefNum, NetSocketRef socket,
                   NetIOParamType *pbP, UInt16 flags,
                   Int32 timeout, Err *errP);
Int16 NetLibSend(UInt16 libRefNum, NetSocketRef socket,
                 void *bufP, UInt16 bufLen, UInt16 flags,
                 void *toAddrP, UInt16 toLen,
                 Int32 timeout, Err *errP);
Int16 NetLibReceivePB(UInt16 libRefNum, NetSocketRef socket,
                      NetIOParamType *pbP, UInt16 flags,
                      Int32 timeout, Err *errP);
Int16 NetLibReceive(UInt16 libRefNum, NetSocketRef socket,
                    void *bufP, UInt16 bufLen, UInt16 flags,
                    void *fromAddrP, UInt16 *fromLenP,
                    Int32 timeout, Err *errP);
Int16 NetLibDmReceive(UInt16 libRefNum, NetSocketRef socket,
                      void *recordP, UInt32 recordOffset,
                      UInt16 rcvLen, UInt16 flags,
                      void *fromAddrP, UInt16 *fromLenP,
                      Int32 timeout, Err *errP);

As noted above, for each socket type you will or will not specify a host IP address, timeout period, and data buffer. Please pay attention that the stream socket allows sending a single buffer that the caller provides. For UDP sockets, you have to send a single packet at a time (1536 bytes). Otherwise, no data is sent at all. Raw sockets (supported in Palm OS version 3.0 and higher) must construct the entire IP header, including the destination address, before data can be sent; thus, the address is taken from the data to be sent. In the sample project, you will find some simple examples of different calls.

NetLibDmReceive is a Palm OS-specific function that has no equivalent in a standard network API. It receives data from a socket directly into a database record. You only provide the record's start address and offset. In all other aspects, it behaves similarly to a NetLibReceive call.





Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel