July 27, 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 »

Typical Scenarios

Before all the story, let's just declare that basically all network applications may be classified either as servers or clients. You certainly may develop some program that will combine both of them simultaniously. But, in most cases, with a Palm device you will need to create clients rather than servers. There are well-known schemas for each type of application. You can find then in any book. Here we place them for reference only:

Client:

  • Perform name resolution to obtain a list of addresses to communicate with (NetLibGetHostByName, NetLibAddrAToIN, and so forth)
  • Open a socket with the NetLibSocketOpen function using the properties returned in the above call
  • Attempt to connect to the server with the NetLibSocketConnect function
  • Exchange data with a server, using the send/recv functions
  • For connection-oriented sockets, shut down the socket with the NetLibSocketShutdown function
  • Close the socket with the NetLibSocketClose function

Server:

  • Perform name resolution to obtain a list of addresses to serve on
  • Create a list of serving sockets, one for each address, using the NetLibSocketOpen function
  • Name the sockets with the NetLibSocketBind function
  • For TCP stream sockets, listen for incoming client connections with the NetLibSocketListen function
  • Wait for incoming data (connectionless) or connections (connection-oriented)
  • For TCP, accept a client connection with the NetLibSocketAccept function
    The accept function creates a new socket. The original socket opened by the server continues to listen and can be used to accept more connections until closed. Server applications must close the listening socket, in addition to any sockets created, by accepting a client connection
  • Exchange data with a server, using the send/recv functions
  • For applications that require compatibility with both connection-oriented and connectionless sockets, always use sendto and recvfrom
  • For connection-oriented sockets, shut down the socket with the NetLibSocketShutdown function
  • When data exchange between the server and client ends, close the socket with the NetLibSocketClose function.
    An application should call NetLibSocketShutdown before calling NetLibSocketClose

Below we will consider some aspects of each application type.

Creation and Setup

In the case of Palm OS, you must call the NetLibSocketOpen function to create the socket:

// Open a socket
UInt16 libRefnum = 0;
Err error = errNone;
...
NetSocketRef socket =
         NetLibSocketOpen(libRefnum,              // Network library
                          netSocketAddrINET,      // Address domain
                          netSocketTypeStream,    // Socket type
                          netSocketProtoIPTCP,    // Protocol
                          -1,                     // Timeout
                          &error                  // Error result
                          );
if (error)
{
// Handle it somehow
}

Because it's written in SDK's help, Net Library supports INET or Raw address domains. You may specify either TCP/IP, UDP, or Raw socket types. For simplicity, we will not discuss Raw sockets, just couple of comments; this is long story by itself.

Well, here are a few words about raw sockets anyway. Since Palm OS 3.0, the following types are supported:

  • Raw sockets in the netSocketAddrINET domain: In this case, you must bind the socket to an IP address using NetLibSocketBind, passing a NetSocketAddrINType structure for the socket address. The port field is ignored.
    For applications that use raw sockets in the INET domain, the net library checks the destination IP address of all incoming packets to see if it matches any of those raw sockets. If it does, the packet is enqueued directly into the matching socket and is not passed to the protocol stack.
    When an application sends data through raw sockets in the IP domain, the net library packages the data into a packet and passes it directly to the interface's send routine. You are responsible for forming the entire IP header, including any necessary checksums, source and destination IP address, and so on.
  • Raw sockets in the netSocketAddrRaw domain with no protocol: In this case, you must bind the socket to an interface using NetLibSocketBind, passing a NetSocketAddrRawType structure for the socket address. The instance and creator specify from which interface the caller wants to receive raw packets.
    When an interface is bound to a raw socket with no protocol, the net library places that interface into raw mode. In raw mode, the interface passes all incoming packets, no matter what the link layer protocol, to its raw receive function.
    When an application sends data through a raw socket with no protocol, the net library packages the data into a packet and passes it directly to the interface's send routine.
    The interface remains in raw mode until the raw socket is closed

Thus, the main headaches are on you.

If we've succeeded to open a socket of the desired type, we have several opportunities. Your can get or set a lot of socket options by calling NetLibSocketOptionSet; for example, linger behavior, OOB data processing, blocking mode, and so forth. The next snippet demonstrates how to cancel the blocking mode:

UInt8 bNonBlocking = 1;
Int16 nRes =
      NetLibSocketOptionSet(libRefnum, socket,
                            netSocketOptLevelSocket,
                            netSocketOptSockNonBlocking,
                            &bNonBlocking,sizeof(bNonBlocking),
                             -1, &err);

As you can see, in the meantime all the stuff is pretty similar to UNIX or Windows.





Page 2 of 4



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel