Spring 91 - MACTCP COOKBOOK: CONSTRUCTING NETWORK-AWARE APPLICATIONS
MACTCP COOKBOOK: CONSTRUCTING NETWORK-AWARE
APPLICATIONS
STEVE FALKENBURG
The Macintosh is now a full-fledged player in the world of TCP/IP networking.
MacTCP, an implementation of TCP/IP for the Macintosh, lets applications take
advantage of a protocol suite that is used extensively by many makes of computers.
This article attempts to demystify the process of MacTCP programming and provides a
library of calls that can be used easily by anyone familiar with Macintosh
programming.
TCP/IP, which stands for Transmission Control Protocol/Internet Protocol, was
developed by the U.S. Department of Defense Advanced Research Products Agency
(DARPA) and used initially on the ARPANET, a national research network created by
DARPA in the late 1960s. Although the ARPANET no longer exists, the TCP/IP
protocols are used on many large-scale networks. Many of these networks are
interconnected and are known collectively as the Internet.
The TCP/IP protocol stack, shown in Figure 1, is composed of several layers. At the
lowest layer, the Internet Protocol (IP) handles transmitting packets of information
from one host to another. Above this network level, TCP/IP provides two transport
layer protocols: Transmission Control Protocol (TCP) and User Datagram Protocol
(UDP). TCP provides reliable connection-based service, while UDP is not connection
based. The MacTCP ® driver gives the programmer interfaces to TCP and UDP, but not
to the lower-level IP. This article deals only with TCP programming. For information
on MacTCP UDP programming, consult the MacTCP Programmer's Guide.
Several application-level protocols use TCP to provide user-level service. The
Simple Mail Transfer Protocol (SMTP) is used to send electronic mail, the Network
News Transfer Protocol (NNTP) is used to transfer and post news, the File Transfer
Protocol (FTP) is used to transfer files between machines, and the Finger protocol is
used to retrieve user information. MacTCP does not include programming interfaces or
implementations for any of these application-level protocols.
With connection-based protocols, such as TCP, a connection is defined as a
bidirectional open line of communication between two hosts. Data is guaranteed to be
received in the same order as it was sent, and in TCP, data reliability is ensured. To
open a connection between two computers, the initiating program sends an open
command containing the network address of the remote computer to MacTCP. If the
remote computer is listening for a connection, it acknowledges the connection, and data
can then be transferred on the connection stream. If the remote computer is not
listening for a connection, the open command fails. Once all transactions have been
completed, the connection may be closed by either computer.
Figure 1 TCP/IP Protocol Stack
Network addressing is essential to this process. Each device connected to a TCP/IP
network is assigned a unique 4-byte address, also known as the IP number, as shown
in Table 1. A unique name can also be assigned to each network entity. MacTCP provides
a name service mechanism, called the Domain Name Resolver (DNR), which translates
network names to addresses and vice versa. In addition, there's a 2-byte port number
associated with TCP connections. Each port number is usually mapped to the type of
application-level service the sender is requesting. For example, the NNTP protocol
always operates on TCP port 119. This mapping is shown in Table 2. MacTCP does not
provide a service for translating between service name and port number.
Table 1 Network Name to Address Mapping
Network Name IP Number
sol.engin.umich.edu 141.212.4.65
mondo.engin.umich.edu 141.212.68.14
freebie.engin.umich.edu 141.212.68.23
daneel.engin.umich.edu 141.212.68.30
maize.engin.umich.edu 141.212.10.56
Table 2 Service to Port Number Mapping
ftp 21
telnet 23
smtp 25
finger 79
nntp 119
MACTCP PROGRAMMING BASICS
This section focuses on the basics of MacTCP programming, while the remainder of the
article discusses high-level, easy-to-use routines. MacTCP is a driver for the
Macintosh that provides access to TCP/IP through the Device Manager. By making
standard Device Manager PBControl calls, programs can access TCP/IP transport
protocols to control and maintain connections. The standard TCP parameter block,
defined in TCPPB.h, is shown here:
char fill12[12];
TCPIOCompletionProc ioCompletion;
/* address of completion routine */
short ioResult; /* result of call (>0 = incomplete) */
char *ioNamePtr;
short ioVRefNum;
short ioCRefNum; /* MacTCP driver reference number */
short csCode; /* command code */
StreamPtr tcpStream; /* pointer to stream buffer */
struct TCPCreatePB create; /* var for TCPCreate,TCPRelease */
struct TCPOpenPB open;
/* var for TCPActiveOpen,TCPPassiveOpen */