Home |
Function closesocket |
Function closesocketClose a socket and shut down any connections associated with this socket. int closesocket(SOCKET socket); ParameterssocketSocket returned by socket or accept. return valueReturns 0 if successful (TCP close initiated but not necessarily the close handshake, UDP close done or RAW close done), otherwise SOCKET_ERROR. If an error occurred, call xn_getlasterror and xn_geterror_string to return the error value. Error Codes further describes each error. Possible values for this function are:
For TCP, this routine starts a shut down of a connection. Once handshaking is complete and all the input data queued on the socket has been read, all resources associated with the connection are freed. For UDP and RAW, the connection is dropped immediately. For TCP, if the connection is still established, a close connection handshake is initiated. If the connection was never established, the resources are freed immediately. If the connection was established, the resources will be freed when the close handshake completes and the input window is empty. The socket is no longer valid for sending data once closesocket has been called but it is valid for reading data. There are three methods used to close:
If a graceful close without timeout is done, assume the other side received all the data before the socket resources were returned. If a hard close or graceful close with timeout is done, there is no guarantee the other side received all the data sent. Since the sequence number for the FIN packets assumes the remote host received the data, it is recommended to choose the timeout value greater than CFG_MAXRTO to guarantee the data will be retransmitted at least once (in case the original data packet is lost). The method closesocket uses to close the socket depends upon the option SO_LINGER and the linger value associated with it, set by calling setsockopt. If the SO_LINGER option is set and the linger value is zero, closesocket does a hard close. If the SO_LINGER option is set with a non-zero linger value, closesocket will block on the write signal until the data in the output window has been acknowledged or a timeout occurs. If a timeout occurs, closesocket does a hard close. If a timeout does not occur, i.e. all the data has been acknowledged, closesocket will do a graceful close. If the SO_LINGER option is not set, closesocket will do a graceful close. The default is a graceful close without a linger value. Failure to call closesocket or xn_abort will eventually exhaust resources. TCP CLOSE SOCKET SUMMARY
Close Socket summary This function does not support sockets in secure mode. For secure sockets, use function net_closesocket instead.
|