Linux Network Programming Reading Notes (9)

xiaoxiao2021-03-06  22

Chapter 13 Unix Domain Settings and Pred - creation Techniques for Computerization Servers

· UNIX domain set

The Linux operating system provides a communication method between the UNIX domain protocol, which cannot be applied to the network, which can be used in communication between the two processes. It is convenient to deliver a file descriptor between the two non-relative relationships, the effect is similar to the transmission between the parent and child process. UNIX domain sleeve is more efficient when interacting with the local process because it does not need to handle network exceptions.

Address structure:

Struct sockaddr_un {

Short int sun_family;

CHAR Sun_Path [104];

}

Use example:

Listen_fd = socket (AF_UNIX, SOCK_STREAM, 0); // UNIX Domain Set

Serv_addr.sun_family = AF_UNIX; // UNIX Domain Protocol Cluster

STRCPY (serv_addr.sun_path, "/ tmp / myfile"); // Create a binding file

Unbind (serv_addr.sun_path); // First release the binding of the specified file

Ret = bind (listen_fd, (strunt sockaddr *) & serv_addr,

Strlen (serv_addr.sun_path) sizeof (serv_add.sun_family); // Bind the specified file path

Listen (listen_fd, listening queue length);

· Transfer and receive file descriptors using SendMSG and Recvmsg

It mainly uses UNIX domain socket, sendMSG and RECVMSG two functions. You need to define a consortium you before sending additional data. Such additional data will be placed in the Struct CMSGHDR structure after memory, and the CMSGHDR related macro definition can work.

Sample code:

Void UNIX_SEND_FD (int unix_fd, int conn_fd)

{

Struct msghdr msg;

Struct Iovec IOV [1];

Char C;

int R;

Union {

Struct CMSGHDR CM;

Char Control [cmsg_space (sizeof (int))];

} Control_un; // Define the consortium, after placing additional data in the Struct CMSGHDR structure

Struct CMSGHDR * CMPTR;

/ / The message socket uses the field, and the address domain fills in the stream type socket.

Msg.msg_name = null;

Msg.msg_namelen = 0;

/ / Fill in the buffer of ordinary data

IOV [0] .iov_base = & c;

Iov [0] .iov_len = 1;

/ / Perform a normal data buffer

msg.msg_iov = IOV;

Msg.msg_iovlen = 1;

/ / Pointer to set additional data

Msg.msg_control = control_un.control;

Msg.msg_controllen = sizeof (control_un.control);

/ / Get the pointer to structural struct cmsghdr

Cmptr = CMSG_FirstHDR (& MSG);

/ / Get the length of this structure

Cmptr-> cmsg_len = cmsg_len (sizeof (int));

// Fill in the level of control data

CMPTR-> CMSG_LEVEL = SOL_SOCKET;

// Fill in the type of control data

Cmptr-> CMSG_TYPE = SCM_RIGHTS;

// Fill in the content of the control data

* (int *) CMSG_DATA (CMPTR) = conn_fd; ret = sendmsg (unix_fd, & msg, 0);

IF (RET <0)

Error_Proc ();

// Receive is the above reverse process, first preset the reception buffer, then cmptr = cmsg_firsthdr (& msg);

// Final return * (int *) CMSG_DATA (CMPTR);

}

· Pred - construction technology for concurrent servers

Pre - construction technology can solve the following significant disadvantages for concurrent servers:

1 Provide services to newly connected customers as soon as possible to eliminate the delay of new processes

2 Don't have to build / release resources for each customer service process

3 sub-process repeated utilization

4 Dynamic management of child processes by passing file descriptors

UNIX socket pair

Int SocketPari (int Family, Int Type, int protocol, int suckfd [2]);

// Establish two connected sockets, you can use it like pipelines

Advantages with "full duplex, you can pass file descriptor to any process) compared to the system

Chapter 14 Original Socket

· 14.1 Original socket

The use of original sockets:

1 IP_FD = Socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); // Only superuser builds and use

// Sock_RAW Creates the original socket, the third parameter is the protocol, you can use 0 or other

2 Bind and Connect functions on raw sockets

The original socket works below the transport layer so there is no port concept. After the address of the original socket, the core only sends the destination address to the IP package of the original socket, ignores other. When CONNECT is called, the core will pass the source address of the Connect address to this original socket. If there is no Bind and Connect original socket, the core will pass all the IP packets that match all protocols to it.

IP socket option

GetSockOpt () function: IP_TTL Gets the survival period, IP_HDRINCL filled the IP Baotou (forge)

The use of original sockets:

1 IP_FD = Socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); // Only superuser builds and use

// Sock_RAW Creates the original socket, the third parameter is the protocol, you can use 0 or other

2 Bind and Connect functions on raw sockets

The original socket works below the transport layer so there is no port concept. After the address of the original socket, the core only sends the destination address to the IP package of the original socket, ignores other. When CONNECT is called, the core will pass the source address of the Connect address to this original socket. If there is no Bind and Connect original socket, the core will pass all the IP packets that match all protocols to it.

IP socket option

GetSockOpt () function: IP_TTL Gets the survival period, IP_HDRINCL filled the IP Baotou (forge)

转载请注明原文地址:https://www.9cbs.com/read-57286.html

New Post(0)