Inter-Process Communication Linux # # # socket (socket)

Socket (socket) is a communications mechanism, by virtue of this mechanism, client / server (that is, to carry out the process of communication) development of the system can be carried out both on a local stand-alone, can also be carried out across the network. Which means it can not allow the same computer processes on the computer to communicate but connected through a network. Because of this, the socket clearly distinguish the client and the server area.

A socket can be seen as inter-process endpoint (endpoint) communication, the name of each socket is unique (the only meaning is self-evident), other processes can discover, connect and communicate. Domain for explaining a communication protocol of socket communication, different communication domains have different communication protocols and address socket structures, etc., therefore, when creating a socket, to indicate that a communication domain. More common is the Unix domain socket (using socket mechanism to achieve inter-process communication within a single machine) and Internet communications domain.

The customer side and service side. When the need for communication between two applications SOCKET, between two applications need to first (on the same machine may also be located on different machines) SOCKET connection established.

  • The party initiating the call connection request to the client side: client side before the call connection request, it must know where the service party. So it is necessary to know the IP address or machine name where the service side of the machine, if the customer side and service side there is a prior agreement like this agreement is PORT (port number). In other words, the client side can be an IP address or machine name of the machine through the service side and the only way to determine the port number to call the service side.

  • Party accepts the call connection request to become the service side: Before calling the customer side and service side must be in a listening state, whether there listening to customer requirements to establish a connection. Upon receiving the connection request, the serving or deny the connection may be established according to the situation. When a message arrives at the customer service side of the side port, it will automatically trigger an event (event), as long as the service side to take over the event, can accept a message from the client side.

 

Socket communication process: start to start the server. Server initialized Socket, then the port binding (bind), listens to port (listen), accept blocked calls, waiting for client connections. At this point if you have a client initiates a Socket, then connect to the server (connect), if the connection is successful, then connect the client and the server is established. The client sends a data request, the server receives the request and processes the request, and then sends the response data to the client, the client reads the data, and finally close the connection, ends of an interaction.

 

There are three characteristics of the attribute determining socket, they are: domain (Domain), the type (type), and protocol (protocol). Socket also used the address as its name. With the format of the address field (also known protocol suite, protocol family) differs. Each protocol suite and can use one or more address family defined address format.

 

Protocol field (domain):  also known as protocol family (family). There are common protocols family AF_INET, AF_INET6, AF_LOCAL (also known AF_UNIX, Unix domain socket) and the like (AF address family is short).

  • AF_INET: IPv4 address (32 bit) and port number (16 bits). It refers to the Internet. When a customer uses a socket connection across the network, it uses an IP address and port of the server computer to specify a particular service on a networked machine, so use socket as an endpoint of communication, server applications a port must be bound before the start of communication, the server waits for a client connection in the specified port. AF_INET domain socket, its address structure described by sockaddr_in.
struct sockaddr_in {
  __kernel_sa_family_t	sin_family;	/* Address family		*/
  __be16		sin_port;	/* Port number			*/
  struct in_addr	sin_addr;	/* Internet address		*/

  /* Pad to size of `struct sockaddr'. */
  unsigned char		__pad[__SOCK_SIZE__ - sizeof(short int) -
			sizeof(unsigned short int) - sizeof(struct in_addr)];
};
/* Internet address. */
struct in_addr {
	__be32	s_addr;
};

The AF_INET6 : IPv6 address ( 128-bit ) and port number (16 bits). In addition to differences on the address, the other consistent with AF_INET. AF_INET6 domain sockets, the structure is described by its address to sockaddr_in6. 

struct sockaddr_in6 {
	unsigned short int	sin6_family;    /* AF_INET6 */
	__be16			sin6_port;      /* Transport layer port # */
	__be32			sin6_flowinfo;  /* IPv6 flow information */
	struct in6_addr		sin6_addr;      /* IPv6 address */
	__u32			sin6_scope_id;  /* scope id (new in RFC2553) */
};
  • AF_LOCAL : also known as AF_UNIX, represents the UNIX file system, it is the file input / output, and its address is the file name. AF_UNIX domain sockets, its address is described by structures sockaddr_un.
struct sockaddr_un {
	__kernel_sa_family_t sun_family; /* AF_UNIX */
	char sun_path[UNIX_PATH_MAX];	/* pathname */
};

Protocol domain (domain) Linux5.4.6 version is defined as follows:

/* Supported address families. */
#define AF_UNSPEC	0
#define AF_UNIX		1	/* Unix domain sockets 		*/
#define AF_LOCAL	1	/* POSIX name for AF_UNIX	*/
#define AF_INET		2	/* Internet IP Protocol 	*/
#define AF_AX25		3	/* Amateur Radio AX.25 		*/
#define AF_IPX		4	/* Novell IPX 			*/
#define AF_APPLETALK	5	/* AppleTalk DDP 		*/
#define AF_NETROM	6	/* Amateur Radio NET/ROM 	*/
#define AF_BRIDGE	7	/* Multiprotocol bridge 	*/
#define AF_ATMPVC	8	/* ATM PVCs			*/
#define AF_X25		9	/* Reserved for X.25 project 	*/
#define AF_INET6	10	/* IP version 6			*/
#define AF_ROSE		11	/* Amateur Radio X.25 PLP	*/
#define AF_DECnet	12	/* Reserved for DECnet project	*/
#define AF_NETBEUI	13	/* Reserved for 802.2LLC project*/
#define AF_SECURITY	14	/* Security callback pseudo AF */
#define AF_KEY		15      /* PF_KEY key management API */
#define AF_NETLINK	16
#define AF_ROUTE	AF_NETLINK /* Alias to emulate 4.4BSD */
#define AF_PACKET	17	/* Packet family		*/
#define AF_ASH		18	/* Ash				*/
#define AF_ECONET	19	/* Acorn Econet			*/
#define AF_ATMSVC	20	/* ATM SVCs			*/
#define AF_RDS		21	/* RDS sockets 			*/
#define AF_SNA		22	/* Linux SNA Project (nutters!) */
#define AF_IRDA		23	/* IRDA sockets			*/
#define AF_PPPOX	24	/* PPPoX sockets		*/
#define AF_WANPIPE	25	/* Wanpipe API Sockets */
#define AF_LLC		26	/* Linux LLC			*/
#define AF_IB		27	/* Native InfiniBand address	*/
#define AF_MPLS		28	/* MPLS */
#define AF_CAN		29	/* Controller Area Network      */
#define AF_TIPC		30	/* TIPC sockets			*/
#define AF_BLUETOOTH	31	/* Bluetooth sockets 		*/
#define AF_IUCV		32	/* IUCV sockets			*/
#define AF_RXRPC	33	/* RxRPC sockets 		*/
#define AF_ISDN		34	/* mISDN sockets 		*/
#define AF_PHONET	35	/* Phonet sockets		*/
#define AF_IEEE802154	36	/* IEEE802154 sockets		*/
#define AF_CAIF		37	/* CAIF sockets			*/
#define AF_ALG		38	/* Algorithm sockets		*/
#define AF_NFC		39	/* NFC sockets			*/
#define AF_VSOCK	40	/* vSockets			*/
#define AF_KCM		41	/* Kernel Connection Multiplexor*/
#define AF_QIPCRTR	42	/* Qualcomm IPC Router          */
#define AF_SMC		43	/* smc sockets: reserve number for
				 * PF_SMC protocol family that
				 * reuses AF_INET address family
				 */
#define AF_XDP		44	/* XDP sockets			*/

#define AF_MAX		45	/* For now.. */

Type (type):  Specifies the socket type. Commonly used socket types, SOCK_STREAM, SOCK_DGRAM, SOCK_RAW etc.

  • SOCK_STREAM : stream sockets (stream) specified by SOCK_STREAM type, which are connected implemented via TCP / IP in AF_INET domain, but also commonly used AF_UNIX socket type. Is an ordered, reliable, bi-directional byte stream socket connection stream supplied, so that the data transmission can be ensured not lost, duplicated or arrive out of order, but it also has a certain error retransmission mechanism.
  • SOCK_DGRAM : datagram socket (Datagram) specified by SOCK_DGRAM type, it does not need to establish a connection and maintain a connection, they are generally achieved by UDP / IP protocol in the AF_INET. Its length of data that can be transmitted is limited, the data packet is transmitted as a separate message network, it may be lost, copied or disorder arrival, UDP is not a reliable protocol, but its relatively high speed, because it does a always we need to establish and maintain a connection.
  • The SOCK_RAW : raw socket (RAW) specified by the SOCK_RAW type, different from a SOCK_STREAM, SOCK_DGRAM socket, which implements the system core. However, what raw sockets do about it? First of all, the general sockets can not handle ICMP, IGMP and other network packets, and SOCK_RAW can; secondly, SOCK_RAW can also handle special IPv4 packets; In addition, the use of raw sockets, socket option can IP_HDRINCL IP header constructed by the user. Overall addition, the SOCK_RAW network can handle normal packets can also handle some special operating IP protocol packets and the data layer and above.

Type (type) Linux5.4.6 version is defined as follows:

enum sock_type {
	SOCK_STREAM	= 1,
	SOCK_DGRAM	= 2,
	SOCK_RAW	= 3,
	SOCK_RDM	= 4,
	SOCK_SEQPACKET	= 5,
	SOCK_DCCP	= 6,
	SOCK_PACKET	= 10,
};

Protocol (protocol): common protocols have, IPPROTO_TCP, IPPTOTO_UDP the like, which correspond to the TCP protocol, UDP transport protocol. can not be freely combined type and protocol, as are the links SOCK_SEQPACKET SOCK_STREAM and oriented type protocol should be selected so oriented link protocol tcp. When the protocol is 0, it will automatically select a corresponding default protocol type.

  • IPPROTO_TCP : TCP transport protocol.
  • SOCK_DGRAM : UDP transport protocol.

Protocol (protocol) Linux5.4.6 version is defined as follows:

/* Standard well-defined IP protocols.  */
enum {
  IPPROTO_IP = 0,		/* Dummy protocol for TCP		*/
#define IPPROTO_IP		IPPROTO_IP
  IPPROTO_ICMP = 1,		/* Internet Control Message Protocol	*/
#define IPPROTO_ICMP		IPPROTO_ICMP
  IPPROTO_IGMP = 2,		/* Internet Group Management Protocol	*/
#define IPPROTO_IGMP		IPPROTO_IGMP
  IPPROTO_IPIP = 4,		/* IPIP tunnels (older KA9Q tunnels use 94) */
#define IPPROTO_IPIP		IPPROTO_IPIP
  IPPROTO_TCP = 6,		/* Transmission Control Protocol	*/
#define IPPROTO_TCP		IPPROTO_TCP
  IPPROTO_EGP = 8,		/* Exterior Gateway Protocol		*/
#define IPPROTO_EGP		IPPROTO_EGP
  IPPROTO_PUP = 12,		/* PUP protocol				*/
#define IPPROTO_PUP		IPPROTO_PUP
  IPPROTO_UDP = 17,		/* User Datagram Protocol		*/
#define IPPROTO_UDP		IPPROTO_UDP
  IPPROTO_IDP = 22,		/* XNS IDP protocol			*/
#define IPPROTO_IDP		IPPROTO_IDP
  IPPROTO_TP = 29,		/* SO Transport Protocol Class 4	*/
#define IPPROTO_TP		IPPROTO_TP
  IPPROTO_DCCP = 33,		/* Datagram Congestion Control Protocol */
#define IPPROTO_DCCP		IPPROTO_DCCP
  IPPROTO_IPV6 = 41,		/* IPv6-in-IPv4 tunnelling		*/
#define IPPROTO_IPV6		IPPROTO_IPV6
  IPPROTO_RSVP = 46,		/* RSVP Protocol			*/
#define IPPROTO_RSVP		IPPROTO_RSVP
  IPPROTO_GRE = 47,		/* Cisco GRE tunnels (rfc 1701,1702)	*/
#define IPPROTO_GRE		IPPROTO_GRE
  IPPROTO_ESP = 50,		/* Encapsulation Security Payload protocol */
#define IPPROTO_ESP		IPPROTO_ESP
  IPPROTO_AH = 51,		/* Authentication Header protocol	*/
#define IPPROTO_AH		IPPROTO_AH
  IPPROTO_MTP = 92,		/* Multicast Transport Protocol		*/
#define IPPROTO_MTP		IPPROTO_MTP
  IPPROTO_BEETPH = 94,		/* IP option pseudo header for BEET	*/
#define IPPROTO_BEETPH		IPPROTO_BEETPH
  IPPROTO_ENCAP = 98,		/* Encapsulation Header			*/
#define IPPROTO_ENCAP		IPPROTO_ENCAP
  IPPROTO_PIM = 103,		/* Protocol Independent Multicast	*/
#define IPPROTO_PIM		IPPROTO_PIM
  IPPROTO_COMP = 108,		/* Compression Header Protocol		*/
#define IPPROTO_COMP		IPPROTO_COMP
  IPPROTO_SCTP = 132,		/* Stream Control Transport Protocol	*/
#define IPPROTO_SCTP		IPPROTO_SCTP
  IPPROTO_UDPLITE = 136,	/* UDP-Lite (RFC 3828)			*/
#define IPPROTO_UDPLITE		IPPROTO_UDPLITE
  IPPROTO_MPLS = 137,		/* MPLS in IP (RFC 4023)		*/
#define IPPROTO_MPLS		IPPROTO_MPLS
  IPPROTO_RAW = 255,		/* Raw IP packets			*/
#define IPPROTO_RAW		IPPROTO_RAW
  IPPROTO_MAX
};

 

Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/103731076
Recommended