|
Internet Domain Sockets
UNIX- domain sockets can be used only for communication between two processes on the same computer. Internet-domain sockets, on the other hand, may be used to connect processes on different machines connected by a network.
Sockets connecting processes through the Internet use the Internet namespace represented by PF_INET. The most common protocols are TCP/IP. The Internet Protocol (IP), a low-level protocol, moves packets through the Internet, splitting and rejoining the packets, if necessary. Since TCP/IP protocol ensures the right packet contents to reach the other end, some packets may vanish or reordered during transport but without affecting the final quality of packet reaching at the other end. Every participating computer is specified using a unique IP number. A reliable and connection-ordered transport is provided by the Transmission Control Protocol (TCP), which itself is layered on top of IP. Telephone-like connections are established between computers and reliable data delivery is ensured.
DNS Names
Because it is easier to remember names than numbers, the Domain Name service (DNS) associates names such as http://www.science-tips.org with computers’ unique IP numbers. DNS is implemented by a world wide hierarchy of name servers, but you need to understand to use internet host names in your programs.
Internet socket addresses contain two parts: a machine and a port number. This information is stored in a struct sockaddr_in variable. Set the sin_family field to AF_INET to indicate that this is an Internet namespace address. The sin_addr field 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 htons to convert the number to network byte order. Programmers can also see the man page for ip for more information.
To convert human-readable hostnames, either numbers in standard dot notation (such as a 10.0.0.1) or DNS names (such as http://www.science-tips.org) into 32-bit IP numbers, you can use gethostbyname. This returns a pointer to the struct hostent structure; the h_addr field contains the host’s IP number.
Standard Port Numbers
By convention, Web servers listen for connection on port 80. Most Internet network services are associated with a standard port number. For example, port 443 is used by secure web servers to listen for connections, while port 25 is used by mail servers. Generally, the file /etc/services is used to list the associations between protocol names and standard port numbers. The first column is the protocol or service name. The second column lists the port number and the connection type: tcp for connection-oriented, or udp for datagram.
If you implement custom network services using Internet-domain sockets, use port numbers greater than 1024.
An IP address-port number pair constitutes a socket. Both server and client have a socket each, and communication takes place through a pair of sockets. This socket pair is sometimes called a connection. No two connections can have the same set of sockets if two users with the same IP address request a Web page from the same server, their socket will be different because the port numbers they will be using at the client end will be random and different.
Socket Pairs
Two file descriptors are created for both beginning and end of a pipe by the pipe function. Pipes are limited because the file descriptors must be caused by related processes and because communication is unidirectional. Two file descriptors are created for two connected sockets on the same computer using the socketpair function. Two-way communication is permitted between related processes by these file descriptors. Its first three parameters are the same as those of the socket call: They specify the domain, connection style, and protocol. The last parameter is a two-integer array, which is filled with the file descriptions of the two sockets, similar to pipe. When you call socketpair, you must specify PF_LOCAL as the domain.
|