Linux close

{

//https://www.cnblogs.com/blankqdb/archive/2012/08/30/2663859.html

1. send resolved

 sockfd: socket descriptor specifies the transmitting side.

 buff: storing data to be transmitted buffer

 nbytes: actual number of bytes of data to improve

 flags: typically set to 0

 1) send the first comparison nbytes length transmission data and the transmission buffer of the socket sockfd, if nbytes> sockfd socket transmit buffer length, the function returns of SOCKET_ERROR is;

 2) If nbtyes <= length of the socket sockfd of the send buffer, the send check whether the protocol is transmission data in the buffer sockfd, if it is waiting for sending the data to the protocol, if the protocol does not yet start transmission transmit buffer in transmit buffer sockfd sockfd data or no data, then send the remaining space and relatively nbytes sockfd of the send buffer

 3) If nbytes> the length of the socket sockfd of the send buffer free space, send the protocol waits for the transmit buffer with the data in the socket sockfd been transmitted

 4) If the transmit buffer nbytes <socket sockfd remaining space, send to only copy the data in buf to the remaining space (Note that not send data to the transmit buffer transmitted in the socket connector sockfd other end, but the transmission protocol, send only the data in buf transmit buffer copy to the remaining space in the socket sockfd).

 5) If the copy send function succeeds, it returns the number of bytes actually copy, send an error occurs while the copy data, the send of SOCKET_ERROR is returned; if disconnected from the network while waiting for data transfer protocol, send function returns SOCKET_ERROR.

 After 6) send function to copy the data to the buff is successfully improved the remaining space of the buffer sockfd it returns, but not necessarily immediately then the data is passed to the other end of the connection. If the network protocol error occurs in the subsequent transfer process, then the function returns a socket SOCKET_ERROR. (Data for each socket function in addition to send the send buffer beginning of the implementation of the total must first wait for the socket to be passed in order to continue the agreement is completed, if the network error occurred while waiting for the socket function then returns SOCKET_ERROR)

 7) In unix system, if disconnected from the network while waiting to send a data transfer protocol, the calling process will send a SIGPIPE signal is received, the signal processing process is a process to terminate.

2.recv function

sockfd: socket descriptor receiving end

Recv function buffer used to store the received data: buff

nbytes: the length specified in the buff

flags: typically set to 0

 Data. 1) recv wait s send buffer has been completed the protocol transfer, if an error occurs while the network data transfer protocol in the transmit buffer in the sock, then the function returns SOCKET_ERROR recv

 2) If the send buffer of the socket sockfd data or no data is sent successfully completed the protocol, to check the recv socket sockfd the receive buffer, the receive buffer sockfd if no data is receiving data or protocol, so recv would wait together until the data have been received. When the data receiving buffer protocol data has been received, s recv function put in the copy to the buff (Note that the protocol of the received data is greater than the length of the buff, so in this case to call several times before the recv function data in the receive buffer sockfd End .recv copy function only copy the data, the received data is the real protocol done)

 . 3) recv function returns the number of bytes of actual copy, copy if an error occurs while recv, it returns SOCKET_ERROR. If recv function while waiting for the protocol to receive the data network is interrupted, it returns 0.

 4) In unix systems, if recv function while waiting for the protocol to receive data off the network, then calling recv process will receive a SIGPIPE signal processing on the signal of the default process is the process terminates.

close system call functionality is straightforward, it is to close an already open file. Function Prototype:

int Close (int FD);

    . 1

FD is, before using open () to get a file descriptor.

In the kernel, the file is open maintains a reference count, each reference close () will count by one file, the file reference count is decremented to 0 in the resource will be released from the kernel.

After a successful close () returns 0, otherwise it returns -1, while the reason for the failure will be recorded in errno. Common mistakes reasons:
EBADF: fd is not a valid file descriptor
EINTR: close () is a signal handler interrupts
EIO: occurred while closing the file IO error

call close () does not check the return value of the code is very common, but strictly speaking, this is actually a serious programming error, because before the write () operation may also result in failure close (), and if so ignore close () operation results may lead to loss of data, in NFS or in particular, there is a common disk quota.

A successful close () does not always guarantee that all data will be flushed to disk up, because the kernel will be delayed write. Need to ensure that all data has been saved to disk if you want to close (), to use the fsync () system call.

Also, note that the operation object close () is a file descriptor, which is an integer that can be reused, so if a file needs to operate in a multi-thread, multi-thread with that in this file descriptor the application file is not a good idea, if a thread closed the file, and then re-open another file to reuse the same integer file descriptor, then another thread will operate to the wrong file.

In addition, if you want to use the close () operation on a socket, you need to make sure that no other thread is blocked in the socket on the recv (), because the socket is closed after another thread will never receive any news, so there is always blocked. In this case, it should first of all to end the connection with the shutdown () system call.

}

 

//https://www.cnblogs.com/CodingUniversal/p/7593210.html

Note: On Unix systems, if the recv function to receive information in wait protocol disconnected from the network, then calling recv process will receive a SIGPIPE signal processing on the signal of the default process is the process terminates.

///////////////////////// process default processing on the signal of the process is terminated. ///////////////////////// process default processing on the signal of the process is terminated. ///////////////////////// process default processing on the signal of the process is terminated.

 

Guess you like

Origin www.cnblogs.com/YZFHKMS-X/p/12319743.html
Recommended