Shutdown () and close () functions of Linux network programming

Source: Internet
Author: User

Shutdown () and close () functions of Linux network programming

Reference TCPIP network programming and UNP;

The shutdown function cannot close the socket, can only close the input and output streams, and then send EOF, assuming that the socket is a, then this function closes all and a related sockets, including the copy, and close closes the socket directly.

1.close () function

[CPP]View PlainCopyprint?
    1. <span style="FONT-SIZE:13PX;" > #include <unistd.h>
    2. int close (int sockfd); //Return success to 0, error -1.</span>

The default behavior of the close one socket is to mark the socket as closed and then immediately return to the calling process, which can no longer be used by the calling process, which means it can no longer be the first parameter of read or write, but TCP will attempt to send any data that has been queued for sending to the peer. The normal TCP connection termination sequence occurs when the send is complete.

In a multi-process concurrent server, the parent-child process shares the socket, the socket descriptor reference count records the number of processes that are shared, and when the parent process or a child process close the socket, the descriptor reference count is reduced by one, and when the reference count is still greater than zero, This close call does not raise the four-way handshake disconnection process for TCP.

2.shutdown () function

[CPP]View PlainCopy print?
    1. <span style="FONT-SIZE:13PX;" > #include <sys/socket.h>
    2. int shutdown (int sockfd,int howto); //Return success to 0, error -1.</span>

The function's behavior depends on the value of the HOWTO

1.SHUT_RD: The value is 0, close the read half of the connection.

2.SHUT_WR: The value is 1, which closes the connected write half.

3.shut_rdwr: The value is 2, and the read and write of the connection is closed.

The common way to terminate a network connection is to call the close function. But using shutdown can better control the disconnection process (using the second parameter).

3. The difference between two functions
The difference between close and shutdown is mainly manifested in:
The close function closes the socket ID, and if there are other processes that share the socket, it is still open, and the connection can still be used for reading and writing, and sometimes it is very important, especially for multi-process concurrent servers.

While shutdown will sever all connections to the process-shared socket, regardless of whether the socket's reference count is zero, those attempts to read the process will receive the EOF identity, and those attempting to write will detect the sigpipe signal, The second parameter of the shutdown can also be used to select the method of disconnection.

A sample fragment of the client is shown below to illustrate the different results of using close and shutdown:

The client has two processes, parent and child processes, and the child processes are forked after the parent process and the server are connected, and the child process sends the standard input terminal keyboard input data to the server side, knowing that the EOF identity is received and the parent process accepts the response data from the server side.

[CPP]View PlainCopyprint?
  1. /* First Sample client fragment,
  2. * Redundant code and variable declarations have been slightly */
  3. S=connect (...);
  4. if (fork ()) {/ * the child, it copies it stdin to the socket * /
  5. While (gets (buffer) >0)
  6. Write (S,buf,strlen (buffer));
  7. Close (s);
  8. Exit (0);
  9. }
  10. else {/ * The parent, it receives answers * /
  11. while (N=read (S,buffer,sizeof (buffer)) {
  12. Do_something (N,buffer);
  13. /* Connection break from the server is assumed * /
  14. / * attention:deadlock here * /
  15. Wait (0); /* Wait for the child to exit * /
  16. Exit (0);
  17. }

For this code, what we expect is that the child process gets the data from the standard terminal, writes the socket close socket, and exits, the server end receives the data to detect EOF (indicating that the data has been sent out), also closes the connection, and exits. The parent process then reads the data from the server-side response and exits. However, the facts will be like this, not really! After the child process is close to the socket, the socket is still readable and writable for the parent process, although the parent process never writes the data. Therefore, the disconnection process for this socket does not occur, so the server side does not detect the EOF identity and waits for data from the client. At this point, the parent process does not detect the EOF identity sent from the server side. This way the server side and the client are stuck in a deadlock (deadlock). If you use shutdown instead of close, you will avoid deadlocks.

[CPP]View PlainCopyprint?
    1. if (fork ()) {/* the child * /
    2. While (gets (buffer)
    3. Write (S,buffer,strlen (buffer));
    4. Shutdown (s,1); /* Break the connection
    5. *for writing, the server would detect EOF now. Note:reading from
    6. *the socket is still allowed. The server may send some more data
    7. *after receiving EOF, why not? */
    8. Exit (0);
    9. }

Shutdown () and close () functions of Linux network programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.