Linux System Learning notes: Sockets
Yeolar 2012-05-18 14:22 Linux System Learning notes Linux System Learning notes: interprocess communication
The previous article summarizes some of the classic mechanisms of interprocess communication in Linux, and this article summarizes the methods of interprocess communication using sockets. The advantage of sockets is that it uses the same interface to handle communication between computers and different computers, typically used for network interprocess communication, in which UNIX domain sockets can be implemented as Full-duplex pipelines.
Directory socket interface Socket descriptor addressing byte order address format address query binding address establish connection data transfer socket Options Out-of-band data UNIX domain sockets using sockets example connection-oriented Ruptime connectionless ruptime socket interface .
A socket interface is a set of functions that combine interprocess communication with UNIX I/O functions, which are implemented on most systems, including a variety of UNIX variants, Windows, and Mac systems.
Socket interface Socket descriptor
Sockets are abstractions of communication endpoints, use socket descriptors to access sockets, Linux uses file descriptors to implement socket descriptors, and many functions that process file descriptors can also be used for socket descriptors.
Use the socket function to create a socket.
1 #include <sys/socket.h>
2
3/* Create socket
4 * @return successfully return file descriptor, error return-1
/5 int socket (int domain, int type, int protocol);
Parameter description: Domain
Identify the characteristics of the communication, including the address format, and each domain has its own address format. POSIX.1 The specified domain includes: Af_inet:ipv4 address field. Af_inet6:ipv6 Address field. Af_unix:unix domain. Af_unspec: Not specified, can represent any domain. Type
Determines the type of socket. The socket types defined by POSIX.1 are: sock_seqpacket: fixed-length, orderly, reliable connection-oriented message delivery. Sock_stream: Orderly, reliable, bidirectional connection-oriented byte throttling. Sock_dgram: Length fixed, unreliable transmission of connectionless messages. Datagram interface for SOCK_RAW:IP protocol. Protocol is typically 0, which means that the default protocol is selected by the given domain and socket type. In the case of domain and type given, if there are multiple protocols, you can specify the protocol with protocol.
In the af_inet communication domain, the default protocol for the Sock_seqpacket socket type is SCTP, and the default protocol for the SOCK_STREAM socket type is the default protocol for the TCP,SOCK_DGRAM socket type is UDP.
Connection-oriented protocol communication can be likened to a telephone call. Before exchanging data, it is required to establish a logical connection between the local socket and the remote socket, that is, the connection is an end-to-end communication channel. The session does not contain address information, which is implied in the connection.
SOCK_STREAM sockets provide a byte throttling service that may require multiple function calls when reading data from a socket. The Sock_seqpacket socket provides message services, and the amount of data received from the socket is consistent with that of the other sender.
The datagram provides a connectionless service, which is a self-contained message that can be compared to sending a datagram. Many datagrams can be sent, but they are not guaranteed in order, and may be lost, and the datagram contains the receiving address.
The SOCK_RAW socket provides a datagram interface for direct access to the network layer (the IP layer) and uses it to construct the protocol header itself. Super user privileges are required to create SOCK_RAW sockets.
Although a socket descriptor is a file descriptor, not all functions that use file descriptors can handle socket descriptors, the following are support for functions:
| Close |
Releasing sockets |
| DUP dup2 |
Normal replication |
| Fcntl |
Support for commands such as F_DUPFD, F_GETFD, F_GETFL, F_getown, F_setfd, F_SETFL, F_setown |
| Fstat |
Supports some stat struct members, defined by the implementation |
| Ioctl |
Supports partial commands, depending on the underlying device driver |
| Poll |
Normal use |
| Read Readv |
Recv equivalent to no sign bit |
| Select |
Normal use |
| Write Writev |
Equivalent to no sign bit of send |
Close does not release the network endpoint until the last descriptor of the socket closes.
You can use the SHUTDOWN function to prevent input/output on sockets.
1 #include <sys/socket.h>
2
3/* Close socket input/output
4 * @return successfully returned 0, error return-1/
5 int shutdown (int sockfd, int how);
How can I take: shut_rd, close the read end, that is, can not read data from the socket. SHUT_WR, close the write end, that is, the socket can not send data. Shut_rdwr, turn off read and write, while unable to read and send data. Addressing
The process identity determines the target communication process, which has two parts: the computer's network address determines the computer, and the service determines the specific process on the computer. byte order
The CPU has a big-end byte sequence and a small-endian byte-order in the sequence of two bytes. To enable different computers to exchange information normally, the network protocol specifies the byte order.
The TCP/IP protocol stack uses a big endian byte sequence. You can use the following function to handle the conversion between host byte order and network byte order.
1 #include <arpa/inet.h>
2
3/* Convert the 32-bit integer number of the host byte sequence to the network byte order
/4 uint32_t htonl (uint32_t hostlong);
5/* Converts the 16-bit integer number of host byte order to network byte order
/6 uint16_t htons (uint16_t hostshort);
7/* Converts the network byte order 32-bit integer to the host byte order
/8 uint32_t Ntohl (uint32_t netlong);
9/* Converts the 16-bit integer of the network byte order to the host byte order
/uint16_t Ntohs (uint16_t netshort);
address format
An address identifies a socket endpoint in a specific communication domain, and the address format is associated with a specific communication domain. To be compatible with different formats of addresses, addresses are coerced into a generic address structure sockaddr, which is defined in the Linux system as follows:
1 struct sockaddr {
2 sa_family_t sa_family; /* Address Type * *
3 char sa_data[14]; /* Variable length address *
4};
Internet addresses are defined in <netinet/in.h>.
In the af_inet domain, the socket address structure is as follows:
1 struct IN_ADDR {2 in_addr_t s_addr;
/* IPV4 Address * * 3}; 4 5