Beej's Guide Network to Programming series serialization 16

Source: Internet
Author: User

5.9. close () and shutdown () --- don't let me see your face again!
You have been sending (send () and receiving (recv () data all day. Now you are going to close your socket descriptor. This is simple. You can use the close () function of the general Unix file descriptor:

Int close (sockfd );

It will prevent reading and writing more data on the socket. Any enterprise image that reads or writes a socket on the other end will return an error message.

If you want to have more control over how to close the socket, you can use the function shutdown (). It allows you to disable communication in a certain direction or two-way communication (just like close (). You can use:

Int shutdown (intsockfd, int how );

Sockfd is the description of the socket file you want to close. The value of how is one of the following:

0-unacceptable

1-cannot be sent

2-sending and receiving are not allowed (the same as close)

If shutdown () succeeds, 0 is returned. If the shutdown fails,-1 is returned (errno is set at the same time .) If you use shutdown () in a connectionless datagram socket, it is only for sending () and recv () cannot be used (remember that you can use them after using connect in the datagram socket ).

 

From the column xiaobin_HLJ80

Related Article

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.