Curriculum Communications
From Real Software Documentation
Aim
We develop a very simple yet useful application to demonstrate the use of TCP ports. The application sends a text message to another computer running the same application.
Contents |
TCP/IP
Just as we have looked at protocols in other situations, there are different protocols for computers to talk to each other over a network. The protocol we will be using is TCP/IP.
This is the main protocol for the Internet. As with the calculator, the underlying implementation of the protocol hides complexities from the software that makes use of it.
To send information to a computer via the Internet[note 1], you need to know two things:
- The IP Address of the computer on which the program is running; and
- The Port Number on which the program you are trying to communicate with is listening.
You’ve seen IP Addresses before. They consist of four numbers between 0 and 255 with periods in between (like this: 210.18.155.12). It contains all the information needed to locate a computer on the Internet.
A port number is an integer between 0 and 65535[note 2]This is a bit like a box number at an address; it is because connections to a computer use different port numbers that you can have multiple programs running on the same computer communicating with various Internet servers, all at the same time. Every packet of information that is sent between two computers contains a specification for the port number on which it will be received.
How do the two programs that are communicating with each other know which ports to use in the first place?
There are three ways:
- There are certain accepted standards that are usually used for certain services. Web servers, for example, usually use port 80. So when you type www.realsoftware.com into your web browser, it translates www.realsoftware.com into an IP address[note 3], then sends a Hypertext Transport Protocol message to port 80 at that address.
- When a program receives a connection with another machine, it will often ask its operating system for a random unused port number, and ask the program at the other end to respond to that port number.
- Internet addresses (technically called Universal Resource Locators, or URLs) can specify the port number after the DNS name or IP address of the server (separated from that address with a colon). It would be possible for Real Software to run a second web server program on the same computer used for the default server, and to link to it from the default port 80 server by using an address; something like this: http://www.realsoftware.com:8080.
The information sent through the Internet is broken up into short packets and might arrive from the Internet out of order or needing retransmission due to an error, but this all happens at a lower layer than TCP provides[note 4]. The TCP service guarantees that the message will arrive intact and the parts of it will arrive in order. However, the information won’t necessarily all arrive at the same time. We need to keep this in mind whenever we write networked software.
With this in mind, let’s look at the project we are going to create: it sends a text message to the same program running on another computer. This is actually quite a useful program!
The Project
- Stop and think for a bit about what a suitable protocol[note 5] for this task might look like.
The only real issue to cover is how the receiving program can know that the entire message has been received.
- Examine the code in the included project, MessageSender.
You should read the built-in language reference about the TCPSocket object. Note all we need to do: we create a receiving socket and tell it to Listen. And if we want to send a message, we ask the sending socket to send.
- Examine a little the enclosed copy of RFC 821, describing one of the most important Internet protocols: SMTP.
It’s called a Request for Comment because that’s how these standards start: someone puts out an idea and asks people to comment on it; it’s put out by the Internet Engineering Taskforce (IETF), who define most of the standards (mostly protocols) by which the internet functions.
It is very important that the protocols for sending and receiving email and web pages, and most of the types of information going over the Internet, are available for anyone to understand and implement. The IETF makes all of the RFCs defining how these standard services work available for free. If you want to write a web server, a web browser, an email server, or a program that works with any of the other Internet standards, you are free to.
- Examine the Port property of the TCPSocket of the built-in Language Reference.
- Focus on the numerous possible errors that can occur when two separate computers are communicating via a network.
We haven’t tried to handle errors such as these to keep the example simple. Most of the work in implementing programs that communicate via the Internet is often in handling all of these errors.
Further Exercises
The project can be extended in a variety of ways:
- Handle sending styled text (set the TextArea’s Styled property to True, and send both the text and the TextStyleData property);
- Allow for dragging and dropping things (files, clippings) onto the window and then off from the window in the other computer.
Any such extensions of the project will, of course, require you to extend the protocol.
References
- Notes
- ↑ Using the most commonly-used protocol for internet communication, TCP (Transmission Control Protocol).
- ↑ Or it might be 65537. After several hours of searching on the internet, the writer could not find out for sure.
- ↑ Using another standard service on a standard port number called the Domain Name Service. You may have had to enter DNS server address, which specifies which computer can provide this translation for you, into your computer when you were setting up your internet connection.
- ↑ An Internet communication service is built up in layers; the TCP layer which provides reliable delivery of packets in order does this using a lower layer called UDP (Universal Datagram Protocol) that provides neither of these guarantees. This is important because for some purposes, speed is more important than correctness (transmitting audio or video, or playing a real-time game, for example), so some programs will need to use UDP, which is much faster.
- ↑ To make sure that all this talk of protocols is clear on one point: it is usual to build protocols on top of other protocols. A lower level in the internet communications protocols just gets packets where they need to go. Another protocol sits ‘on top’ of this to correct transmission errors from the lower level, and to assemble larger messages out of the packets from the lower level, by putting them together in order (this gives us TCP). We will use that level to implement a higher-level protocol of our own invention to send messages
