These articles are excerpted from various 1996 issues of PDA Developers magazine. Copyright (C) 1996 by Creative Digital, Inc. All Rights Reserved.

For a sample copy of PDA Developers, contact Creative Digital at info© cdpubs.com or visit their web site at http://www.cdpubs.com.

NS BASIC CORNER: A BIT OF THE NIE
John Schettino
GTE Laboratories, Inc.
js12© gte.com



Copyright(C) 1996 by Creative Digital Publishing Inc. All rights reserved.

If you've been using the Newton Internet Enabler (NIE) to run Internet-savvy applications, you've no doubt wondered how to use it with NS BASIC. With the addition of the BASIC Internet Tool (BIT) recently released by NS BASIC, you can now easily access TCP/IP-based services in your programs. BIT is a free application that provides a simple NIE interface. You can now open a connection and exchange text-based information with standard information servers.

To give BIT a workout, in this article I write a bare-bones Internet news reader in NS BASIC. It's a good project because I need to use files, windows, the BIT, and lots of string manipulation functions. The program will take more than one article to complete.

A BIT OF THE NET
BIT is a small (15K) package that installs as an extension. You also need to have the NIE installed and configured to use BIT. For best results on a MessagePad 120 you should install BIT, the NIE, and NS BASIC on the internal store, and remove the storage card before connecting to the Internet. Once everything is installed, you can use BIT to open a single connection at a time to an Internet address and port.

I assume that you know something about how the Internet works. If you're a bit rusty or just getting started, you can download the NIE Developers Toolkit at http://devworld.apple.com/dev/newton/tools/nie.html. You access BIT by first getting a reference to the BIT extension:

BIT:=getroot().|bit:nsbasic|
You use the value in the BIT variable to access values and call functions. Once you call a BIT function, the extension processes the request in the background, leaving your program free to do additional work. It's usually a good idea not to do too much work, since you're sharing the Newton CPU with BIT, and there's not too much CPU to go around.

Once you're ready to access the results of your request, you enter a wait loop, checking for either specific data or status flags.

Starting a Request
Before you can talk to something on the Internet, you need to connect to its address and port. You do this by setting two variables in BIT and then calling mConnect:

BIT.fConfigAddr := "any.internet.address"
BIT.fConfigRPort := 119 // or any other port
U.BIT:mConnect()

The fConfigAddr variable specifies a string containing the host name or IP address for the connection. fConfigRPort identifies which port to use for the connection. Here's a handy performance tip: if you know the IP address, use it instead. It saves a bit of time by skipping the step where the NIE looks up the address from the name.

You actually open the connection by calling mConnect. Recall that the call returns right away. You need to check the variable fendPointState to see when the connection completes:

do while bit.fEndpointState<>1
	wait 1000
loop

Sending and Receiving Text
Once connected, you can send text using the Send function and read received text using the Receive function. If you make a request and want to cancel it, use the CancelRequest function.

Whenever you send text with Send the receive buffer is cleared. This means BIT is really designed to be used with Internet servers that have a request/reply textual protocol (fortunately, that's most of them). The Send function is also smart enough to add a newline character to the end of your text for you.

Whenever new text is received by BIT it is appended to a buffer stored in fReceiveBuffer. You can examine the text in this buffer to see if all of your data is received. Once what you're looking for is in the buffer, calling Receive returns it and resets the buffer.

Finishing Up When you are all done using a connection, it's very important that you close the connection with the CleanUp function. This releases the connection and the Internet link. The modem may or may not hang up, depending on the user's NIE preferences.

NNTP (THAT'S NEWS TO ME)
Internet news uses what is called the Network News Transfer Protocol (NNTP). NNTP servers are found at port 119. They often have catchy names such as "newshost" (that's what my news host is called, anyway).

The protocol to fetch and post articles is really quite simple. To retrieve news from a newsgroup, you have the following dialog with the server:

200 news.gte.com InterNetNews NNRP server
INN 1.4unoff4 05-Mar-96 (gtel)
ready (posting ok).

group comp.sys.newton.misc

211 617 67657 72148 comp.sys.newton.misc

xhdr subject 72148-72149

221 subject fields follow
72148 Re: NCU - SYNCH :-(
72149 FS: Newton 120


Return to the NS Basic Corner
Return to the product information main page