There are two methods for C to disable socket:
1. Shutdown
# Include <sys/socket. h>
Int Shutdown (INT sockfd, int how );
There are three methods for how:
Shut_rd (0): Disable the READ function on sockfd. This option does not allow sockfd to perform read operations.
Shut_wr (1): Disable the FD write function. This option does not allow FD to write.
Shut_rdwr (2): Disable the FD read/write function.
If it succeeds, 0 is returned. if the error is-1, the error code errno: ebadf indicates that sockfd is not a valid descriptor; enotconn indicates that sockfd is not connected; enotsock indicates that sockfd is a file descriptor rather than a socket descriptor.
Ii. Close
# Include <unistd. h>
Int close (int fd );
Disable read/write.
If the call succeeds, 0 is returned, and-1 is returned. The error code errno: ebadf indicates that FD is not a valid descriptor. eintr indicates that the close function is interrupted by signals. EIO indicates an IO error.
Note:
1. Shutdown can be disabled in one way, but close cannot.
2. When multiple threads share/call the same socket, close will only be reduced by 1, and the socket will not be closed until it is reduced to 0, while shutdown will ignore how many threads are in use, directly close the socket.