It guarantees only “best-effort”delivery,so packets may vanish
or be reordered during transport.Every participating computer is specified using a
unique IP number.The Transmission Control Protocol (TCP), layered on top of IP,pro-
vides reliable connection-ordered transport.It permits telephone-like connections to
be established between computers and ensures that data is delivered reliably and in
order
DNS Names
Because itis easier to remember names than numbers, the Domain Name Service (DNS) associates names
such as www.codesourcery.comwith computers’ unique IP numbers. DNS is implemented by a world-
names in your programs.
Internet socket addresses contain two parts:a machine and a port number.This infor-
mation is stored in a struct sockaddr_invariable.
Set the sin_familyfield to AF_INET
to indicate that this is an Internet namespace address.The sin_addrfield stores the
Internet address of the desired machine as a 32-bit integer IP number.A port number
distinguishes a given machine’s different sockets.Because different machines store
multibyte values in different byte orders,use htonsto convert the port number to
network byte order.See the man page for ipfor more information.
To convert human-readable hostnames,either numbers in standard dot notation
(such as 10.0.0.1) or DNS names (such as www.codesourcery.com) into 32-bit IP
numbers,you can use gethostbyname.This returns a pointer to the struct hostent
structure;the h_addrfield contains the host’s IP number.
See the sample program in Listing 5.12.
Listing 5.12 illustrates the use of Internet-domain sockets.The program obtains the
home page from the Web server whose hostname is specified on the command line.
(socket-inet.c) Read from a WWW Server
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
/* Print the contents of the home page for the server’s socket.
Return an indication of success. */
void get_home_page (int socket_fd)
{
char buffer[10000];
ssize_t number_characters_read;
/* Send the HTTP GET command for the home page. */
sprintf (buffer, “GET /\n”);
write (socket_fd, buffer, strlen (buffer));
/* Read from the socket. The call to read may not
return all the data at one time, so keep
trying until we run out. */
while (1) {
number_characters_read = read (socket_fd, buffer, 10000);
if (number_characters_read == 0)
return;
/* Write the data to standard output. */
fwrite (buffer, sizeof (char), number_characters_read, stdout);
}
}
int main (int argc, char* const argv[])
{
int socket_fd;
struct sockaddr_in name;
struct hostent* hostinfo;
/* Create the socket. */
socket_fd = socket (PF_INET, SOCK_STREAM, 0);
/* Store the server’s name in the socket address. */
name.sin_family = AF_INET;
/* Convert from strings to numbers. */
hostinfo = gethostbyname (argv[1]);
if (hostinfo == NULL)
return 1;
else
name.sin_addr = *((struct in_addr *) hostinfo->h_addr);
/* Web servers use port 80. */
name.sin_port = htons (80);
/* Connect to the Web server */
if (connect (socket_fd, &name, sizeof (struct sockaddr_in)) == -1) {
perror (“connect”);
return 1;
}
/* Retrieve the server’s home page. */
get_home_page (socket_fd);
return 0;
}
This program takes the hostname of the Web server on the command line (not a
URL—that is,without the “http://”).It calls gethostbynameto translate the hostname
into a numerical IP address and then connects a stream (TCP) socket to port 80 on
that host.Web servers speakthe Hypertext Transport Protocol (HTTP),so the program
issues the HTTP GETcommand and the server responds by sending the text of the
home page.
Standard Port Numbers
By convention, Web servers listen for connections on port 80. Most Internet network services are associ-
ated with a standard port number. For example, secure Web servers that use SSL listen for connections
on port 443, and mail servers (which speak SMTP) use port 25.
On GNU/Linux systems, the associations between protocol/service names and standard port numbers are
listed in the file /etc/services. The first column is the protocol or service name. The second column
lists the port number and the connection type: tcpfor connection-oriented, or udpfor datagram.
If you implement custom network services using Internet-domain sockets, use port numbers greater
than 1024.
For example,to retrieve the home page from the Web site www.codesourcery.com,
invoke this:
% ./socket-inet www.codesourcery.com
<html>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1”>
...
Socket Pairs
As we sawpreviously,the pipefunction creates two file descriptors for the beginning
and end of a pipe.Pipes are limited because the file descriptors must be used by
related processes and because communication is unidirectional.The socketpairfunc-
tion creates two file descriptors for two connected sockets on the same computer.
These file descriptors permit two-way communication between related processes.