10.3.1 Overview of TCP/IP.............................................
TCP/IP is quite complex, so the overview here will be something of an over-
simplification, but we’ll cover enough for you to understand what R’s socket
functions are doing.
For our purposes here, the termnetworkrefers to a set of computers con-
nected together locally, without going through the Internet. This typically
consists of all the computers in a home, all the computers in a smaller busi-
ness, and so on. The physical medium between them is usually an Ethernet
connection of some form.
The Internet, as its name implies, connects networks. A network in the
Internet is connected to one or more other networks viarouters, which are
special-purpose computers that connect two or more networks together.
Every computer on the Internet has an Internet Protocol (IP) address. This
is numeric, but it can be stated in characters, as inwww.google.com, which is
then translated into the numeric address by the Domain Name Service.
However, the IP address is not enough. When A sends a message to
B, there may be several applications at computer B that are receiving Inter-
net messages, such as web browsing, email service, and so on. How does
the operating system at B know to which of these to send the message from
A? The answer is that A will specify aport numberin addition to the IP address.
The port number indicates which program running at B is intended as the
recipient. And A will also have a port number so that the response from B
reaches the correct application at A.
When A wishes to send something to B, it writes to a software entity
called asocket, using a system call syntactically similar to the one for writing
to a file. In the call, A specifies B’s IP address and the port number to which
A wishes to send a message. B has a socket, too, and it writes its responses
to A in that socket. We say there is aconnectionbetween A and B via those
sockets, but that doesn’t mean anything physical—it’s just an agreement
between A and B to exchange data.
Applications follow aclient/servermodel. Say a web server is running at
B, at the standard port for the Web, port 80. The server at B islisteningat
port 80. Again, this term should not be taken literally; it just means that the
server program has made a function call that notifies the operating system
that the server program is willing to have connections at port 80. When
network node A requests such a connection, the function call at the server
returns, and the connection is set up.
If you are a nonprivileged user and write some kind of server program—
say in R!—you must assign a port number above 1024.
NOTE If a server program is taken down or crashes, there may be a few seconds’ delay before
the same port is reusable again.
10.3.2 Sockets in R....................................................
A very important point to keep in mind is that all the bytes sent by A to B
during the time the connection between them exists are collectively consid-
eredone big message. Say A sends one line of text of 8 characters and then
Input/Output 247