I saw a piece of code like this, which was used to close a socket.
`//a TCP socket is created: sockfd
struct linger ling;
ling.l_onoff = 1;
ling.l_linger = 0;
setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
......
//when need to close the socket
if(sockfd>0)
{
shutdown(sockfd, SHUT_RDWR);
close(sockfd);
}
else
{
//some error message
}`
I have three questions:
- Why need call shutdown and close at the same time to close the socket? From Wireshark, It seems that a FIN would be sent, then a RST is sent.
- In this way, a TIME_WAIT state could be avoid?
- When shutdown sockfd, the file descriptor is still available? there is no error occurred when call close function.