PowerBASIC Gazette CyberEdition #15 =================================== January Thirtieth 2001 From: Bob Zale, President, PowerBASIC, Inc. Our latest Win32 compilers, PB/CC 2 and PB/DLL 6, offer full support for TCP and UDP communications. They'll let you send an email from any program, in just a few lines of code. You can add attachments, check incoming email, even access an FTP site or download a web page! Great stuff... But how does it really work? You'll get all the inside information, right here in the PowerBASIC Gazette. I've asked Tony Jones to prepare a series of articles for you on just these topics. First an overview, then discussions in depth, complete with simple, easy to use examples of each of the protocols. Stay tuned right here, and you'll be a TCP/IP expert in no time at all! If you haven't yet upgraded to PB/CC 2, or PB/DLL 6, now is certainly the time to do so. Check out all of the product details at www.powerbasic.com and join in the fun! {smile} Pricing and ordering information can be found at the very end of this Gazette. Questions? Just email info@powerbasic.com We'll do our very best to help. Regards, Bob Zale, President PowerBASIC, Inc. ============================================================== ============================================================== Introduction To The TCP/IP Protocol By Tony Jones, PowerBASIC, Inc. Both the PowerBASIC DLL Compiler 6.0 and the PowerBASIC Console Compiler 2.0 offer built-in support for client-server network communications using TCP and UDP. However, some of you may not be familiar with this aspect of programming, so in this article we'll take a tour of network communications beginning with the TCP/IP protocol. TCP/IP In A Nut Shell The Transmission Control Protocol/Internet Protocol (TCP/IP) is a protocol that provides methods for communication over a network. The TCP/IP protocol is designed to be both software and hardware independent and is comprised of three different layers. The Network Layer, Transport Layer and the Application Layer, each of which I'll briefly describe. The TCP/IP protocol begins at the Network Layer. This layer is responsible for the transmission and routing of packets from one location to another across the network. The method used to route these packets is determined by the protocol that is being used, and in the case of TCP/IP, it is the Internet Protocol (IP). The IP protocol is known as a connectionless, "best-effort" service that utilizes numeric addresses to send data across a network. The IP protocol doesn't wait for a response when sending a packet, it simply sends the packet on it's way and forgets about it. It is left up to the Transport Layer to perform any error checking or retransmission. Next, there is the Transport Layer. This layer is comprised of the transporting methods that are used for sending data across a network. This is where the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP) enter the picture. On the sending end of the network, the Transport Layer encapsulates the data with a header and trailer information which together form a packet. On the receiving end of the network, the header and trailer is removed from the packet, leaving just the data, which is then passed on to the Application Layer. The Application Layer sits at the very top the TCP/IP protocol ladder. This layer provides the services that are required by application programs in order to implement the client-server model. So, for TCP/IP services such as FTP and Telnet, there is a Client (the application), and a Server, which serves up the request of the client. While not really a part of the TCP/IP protocol, the Datalink Layer (the Physical Layer) deserves mention. The Datalink Layer is the actual physical connection on a network. As I mentioned earlier, TCP/IP is both software and hardware independent, so the Network, Transport, and Application layers are able to operate independently of the Datalink Layer. This makes it possible for TCP/IP to work on a variety of networks, including Ethernet, Token Ring, ISDN, and of course the Internet. TCP The Transmission Control Protocol (TCP) is known as a reliable connection protocol. Residing on top of the IP protocol, it was designed to provide each application with a reliable and dependable method of transmitting data across a network. TCP has the ability to detect errors and missing data, and can retransmit data when necessary. The TCP protocol provides services which listen for an incoming connection request, request a connection, send and receive data, and end a session gracefully. TCP can also be thought of as a "two way" communication, meaning that there is handshaking and acknowledgements on both the sending and receiving ends, to ensure that the data packet arrives free of errors. UDP The User Datagram Protocol (UDP) is known as a connectionless protocol. Like TCP, it also resides on top of the IP protocol. While UDP does provide basic data integrity by allowing an application to detect data corruption, it does not provide for any protection against data loss or for the retransmission of data. UDP can also be thought of as a "one way" communication method, meaning that it expects that the data packet being sent will arrive at its destination. Ports TCP/IP communications take place over what are called ports. The use of ports allows multiple software clients to co-exist on a single machine that has been assigned a single IP address. Each packet that arrives at a given machine is assigned to a specific port. TCP/IP clients register the use of a specific port either symbolically, through the use of a name, or with a specific port number in the range 0 through 65535. In the case of symbolic names, the TCP/IP software looks up the name within an index to determine the port number. There are also a number of "well known" ports that TCP/IP uses. A well known port is a port that has been preassigned a specific number by the Internet Assigned Numbering Authority (IANA) to identify these well known services. These include such services as FTP, SMTP and Telnet. The well known ports begin at 0 and go up to 1023. Listed below are just a few of most common ports that TCP/IP services offer: Port 20, FTP-DATA: File Transfer Protocol (FTP) Data Port Port 21, FTP: File Transfer Protocol (FTP) Control Port Port 23, Telnet: Telnet Service Port 25, SMTP: Simple Mail Transer Protocol (SMTP) Port 42, NameServer: Nameserver Resolution Port 80, HTTP: HyperText Transfer Protocol (HTTP) You may notice that the FTP service on the above list is assigned to two different ports. Why? Because the FTP service uses Port 20 for sending and receiving data, and port 21 is for sending the FTP commands that set up and control the transmission of the data. In addition to the well known ports, there are also registered ports, starting at 1024 and going up to 49151. The IANA does not control the use of these ports, but it does maintain a list of the registered ports as a convenience. Most of these registered ports are used by commercial applications such as RealPlayer. There's nothing to keep you from using one of these registered ports for your own application, but of course you will run the risk of interfering with existing software that relies on these ports. Finally, the reserved ports occupy the remaining ports between 49152 and 65535. This range of ports is used dynamically by custom client applications and are rarely assigned to a server. Establishing Communications Establishing a TCP connection using either PowerBASIC DLL 6.0 or PowerBASIC Console Compiler 2.0 is actually very simple, and is illustrated in the following lines of code. TCP OPEN PORT PortNum& AT "someserver.com" AS hTCP& TCP LINE INPUT hTCP&, Buffer$ TCP PRINT hTCP&, "Some Text" TCP CLOSE hTCP& On the first line, we open a connection with the host, here known as "someserver.com", on port number PortNum&. In the second line of code, we store the response from the port in the variable Buffer$. The next line of code sends a line of text to the open port, and the final line simply ends our session by closing the connection gracefully. Pretty simple, right? Establishing a UDP connection is just as easy. UDP OPEN AS hTCP& UDP SEND hTCP&, AT IpAddr&, Port&, Buffer$ UDP RECV hTCP&, FROM IpAddress&, PortNum&, Buffer$ UDP CLOSE hTCP& Again, on the first line, we open a UDP socket for communications. The second line sends the data contained in the variable Buffer$ to the port specified by Port& at the IP address in IpAddr&. The third line receives data into the variable Buffer$, and the IP address that sent the data stored in the variable IpAddress&, along with the destination port number placed in the variable PortNum&. The final line simply closes the UDP socket. Which Protocol Should I Use? Good question! It really depends on the type of application you develop, and what you are trying to accomplish. For applications such as FTP, Telnet and SMTP clients, TCP would be the obvious choice for several reasons. One reason is that the TCP/IP protocol suite provides built-in services for these types of applications. Also, if you will recall, TCP has the capability to detect errors and resend packets. For applications such as e-mail and FTP clients, this is desireable. For instance, assume you were downloading a binary file with an FTP client. In such a situation, the loss of a packet would likely result in file corruption, since a part of the download would be missing. Therefore, use of TCP would ensure the lost packet could be retransmitted. However, if you are developing an application that involves time-sensitive data, then UDP may be the better choice. Why? Remember that UDP doesn't provide for protection against data loss, simply because it doesn't retransmit lost packets. In a time-sensitive application it would be pointless to retransmit any lost or corrupted packets, because the lost packet would be worthless to a real-time application. Real-time multimedia applications with streaming audio and video are prime examples of cases where the UDP protocol is used extensively. Of course, the downside to using UDP is that a response may not be received at all, requiring a complete retransmission. In conclusion Hopefully, this article has provided you with a better understanding of TCP/IP and network communications. In the coming installments, we'll be discussing specific implementations along with a good deal of PowerBASIC source code. Using the TCP and UDP protocols, you'll find you can develop a wide variety of applications, encompasing everything from e-mail clients to real-time multimedia applications. With the built-in TCP and UDP commands of PB/DLL 6.0 and PB/CC 2.0, your job is made even easier, because PowerBASIC provides you with the essential tools you need to accomplish your goals! ==================================================================== Order online at https://www.powerbasic.com/shop/ or just send an email with all pertinent information to sales@powerbasic.com We'll take it from there! ------------------------------------------------------------------- Most PowerBASIC products (those without printed books) can now be delivered by electronic mail. No wait for a package to arrive... No high shipping costs... For just $6 per order, no matter how many products, we'll deliver directly to your computer. If you're outside the U.S., savings might be greater. You won't pay taxes or duties to a freight company or postal service, because they aren't involved in the delivery. Check your tax code to be sure, but some countries charge no tax at all on transactions of this type. It could just be your lucky day! ==================================================================== Is your PowerBASIC Gazette Electronic Edition subscription coming to you at home or work? If you don't want to miss a single issue, why not subscribe from both email addresses? Send your subscription request to email@powerbasic.com and please include your name and all email addresses you'd like to add as well as your Zip or Postal Code. Did you know that there is also a paper edition of the PowerBASIC Gazette? That's right, you can get a newsletter with articles detailing all of the products available from PowerBASIC, Inc. Full of useful code tidbits, book reviews and more. Why not get your free subscription today? Just send your name and postal address to sales@powerbasic.com If you know someone else who would enjoy this newsletter please forward a copy to them so they can subscribe. ==================================================================== All contents Copyright (c) 2001 PowerBASIC, Inc. All Rights Reserved. PowerBASIC is a registered trademark of PowerBASIC, Inc. PB/CC, PB/DLL, and PowerTREE are trademarks of PowerBASIC, Inc. All other brand names are trademarks or registered trademarks of their respective owners. ==================================================================== PowerBASIC Gazette - Electronic Edition Volume 1 - Issue 15 PowerBASIC, Inc. (800) 780-7707 Sales 1978 Tamiami Trail S. #200 (941) 408-8700 Voice Venice, FL 34293-5006 (941) 408-8820 Fax Visit us on the World Wide Web at www.powerbasic.com Email Sales: sales@powerbasic.com This newsletter is only sent to email addresses in our subscription list. If you have received this newsletter by mistake or no longer wish to receive it, please send a simple unsubscribe request to support@powerbasic.com with your name and zip/postal code. This newsletter is best viewed with a fixed-width font. ====================================================================