PocketCNetLib Sample Code

PocketCNetLib API Test Program 1.0.0



© 2003 Serg Koren & VisualNewt Sofware.
All rights reserved.


// PocketCNetLib test // This is a simple PocketC program to test the PocketCNetLib. // PocketCNetLib is a native library that allows PocketC to connect to the internet. // // PocketCNetLib // VisualNewt Software // http://www.VisualNewt.com/ // Serg@VisualNewt.com // // PocketCNetLib is (c) 2003 Serg Koren & VisualNewt Software. // All rights reserved. // PocketCNetLib is $10. // // use the 'library' statement to get access to the library library "PocketCNetLib" // app stuff @cid "VNNL"; @name "PocketCNetLib test"; @dbname "PCNL test"; @ver "1.0.0"; // globals int refnum; int socketrefnum; int err; // For returned strings...some of the library calls return a pointer to a string. pointer p; @doc "Translate an error code into a string."; ConvertError(int err) { // pass in the error code returned by the library p = nlErrorString(err); puts(*p + " " + (err - 5000) +  "\n"); alert(*p + " " + (err - 5000)); exit(); } @doc "Releases the NetLib"; CleanupLibrary() { // close the library err = nlNetLibClose(refnum,0); if (err > 5000) { ConvertError(err); } puts("Library closed\n"); } @doc "Gets a reference to the NetLib and opens it."; SetupLibrary() { // get the refnum to library refnum =  nlGetNetLibNumber(); if (err > 50000) { ConvertError(refnum); } puts("NetLib Reference number: " + refnum + "\n"); // set timeout to 5 seconds err = nlSetAppNetTimeout(5); if (err > 5000) { CleanupLibrary(); ConvertError(err); } puts("Timeout set\n" ); // open the library err = nlNetLibOpen(refnum); if (err >  5000) {         CleanupLibrary(); ConvertError(err); } puts("Library opened\n"); } @doc "Opens a socket."; ObtainSocket() { // open a socket socketrefnum  = nlNetLibSocketOpen(refnum,2,1,5000); if (socketrefnum > 5000) { CleanupLibrary(); ConvertError(err); } puts("Socket number : " + socketrefnum + "\n"); } @doc "Closes the socket."; ReleaseSocket() { // close the socket err = nlNetLibSocketClose(refnum,socketrefnum,5000); if (err > 5000) { CleanupLibrary(); ConvertError(err); } puts("Socket closed.\n"); } @doc "Connects to either the dottedIP or host name at the specified port."; ConnectSocketToAddress(string dottedIP, string port) { string buf = dottedIP; buf = buf + " translated to "; // Get a socket reference number by opening a socket ObtainSocket(); // look up our test DNS name's IP address... puts("Checking " + dottedIP + "\n"); // see if the first char is a number...if so it's a dotted IP, otherwise its a DNS name; // if its a DNS name we need to get the dotted IP equivalent. dottedIP = strlwr(dottedIP); if ((dottedIP@[0] >= "a") && (dottedIP@[0] <= "z")) { p = nlNetLibGetHostByName(refnum,dottedIP,5000); puts(dottedIP + " - " + *p + "\n"); // length of 4 indicates an error code if (strlen(*p) == 4) { // convert the error in string form to an interger err = *p; ReleaseSocket(); CleanupLibrary(); ConvertError(err); } } // connect the socket // note you can also just pass in the DNS name instead of the IP address. // ( www.visualnewt.com instead of xxx.xxx.xxx.xxx since nlNetLibSocketConnect //   does its own NetLibGetHostByName if needed.) err = nlNetLibSocketConnect(refnum,socketrefnum,*p,port,5000); if (err == 5003) { // The demo library is locked into www.visualnewt.com alert("This is a demo copy of PocketCNetLib: " + (err - 5000)); puts("This is a demo copy!\n"); ReleaseSocket(); CleanupLibrary(); exit(); } else if (err > 5000) { ReleaseSocket(); CleanupLibrary(); ConvertError(err); } puts("Socket connected: " + *p + ":" + port + "\n"); } @doc "Sends the specified data to our socket."; SendData(string what) { err = nlNetLibSend(refnum,socketrefnum,what,0,"","",5000); if (err > 5000) { ReleaseSocket(); CleanupLibrary(); ConvertError(err); } puts("Socket sent data! Bytes: " + err + "\n"); } @doc "Receives data from our socket."; GetData() { puts("Receiving response.\n"); p =  nlNetLibReceive(refnum,socketrefnum,0,5000); // length of 4 indicates an error code if (strlen(*p) == 4) { // convert error in string form to integer err = *p; ConvertError(err); } puts("RECEIVED:\n"); puts (*p + "\n"); puts("END RECEIVED\n"); } @doc "Make sure the NetLib is installed."; CheckAvailability() { err = nlCheckInstalled(); if (err > 5000) { alert("NetLib not installed or available: " + (err - 5000)); puts("NetLib not installed or available: " +  (err - 5000)); exit(); } puts("NetLib is available.\n"); } // The main routine // This is structured into high-level functionality. @doc "Main routine."; main() { // Clear the output clear(); // Display a title with the PocketCNetLib version. // It should not return a 0 p = nlVersion(); if (*p == 0) { alert("Cannot connect to library."); exit(); } title("PocketCNetLib Test - " + *p); puts("Version: " + *p + "\n"); // Make sure the NetLib is installed and available. CheckAvailability(); // Set up the Net library SetupLibrary(); // Connect a socket to an address, port number // The demo library only connects to www.visualnewt.com // Register to get an unlocked version. ConnectSocketToAddress("www.VisualNewt.com","80"); // Now send data to our socket -- in this case we call a web page SendData("GET /Products/Palm/PocketCNetLIb/test.html HTTP/1.0\r\n\r\n"); // Receive data from our socket GetData(); // Close the socket we have open ReleaseSocket(); // Tear down the Net library CleanupLibrary(); puts("Done!"); alert("Done!"); }


Support:

PocketCNetLib support is a development tool. As such, only the tool is supported, not any code you attempt to develop using the tool. It is up to you to determine whether the problem is in your code or the library's. Support will be provided to registered users only, and priority will be given to registered users for enhancement requests.

Bugs and enhancement requests should be sent to: Serg@VisualNewt.com

©2008, 2009, 2010 Serg Koren & VisualNewt Software.  All rights reserved.