1 void sock_Close(NLsocket socket)
2 {
3 nl_socket_t *sock = nlSockets[socket];
4 struct ip_mreq mreq;
5
6 if(sock->type == NL_UDP_MULTICAST)
7 {
8 /* leave the multicast group */
9 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)&sock->addressout)->sin_addr.s_addr;
10 mreq.imr_interface.s_addr = bindaddress;
11
12 (void)setsockopt((SOCKET)sock->realsocket, IPPROTO_IP, IP_DROP_MEMBERSHIP,
13 (char *)&mreq, (int)sizeof(mreq));
14 }
15 if(sock->type == NL_RELIABLE_PACKETS)
16 {
17 /* check for unsent data */
18 if(sock->sendlen > 0)
19 {
20 int tries = 200;
21
22 /* 200 * 50 ms = up to a 10 second delay to allow data to be sent */
23 while(tries-- > 0 && sock->sendlen > 0)
24 {
25 htThreadSleep(50);
26 }
27 }
28 sock_DeleteSocket(socket);
29 }
30 if((sock->type == NL_RELIABLE_PACKETS || sock->type == NL_RELIABLE) && sock->listen == NL_FALSE)
31 {
32 struct linger l = {1, 10};//延迟10s
33
34 (void)setsockopt((SOCKET)sock->realsocket, SOL_SOCKET, SO_LINGER, (const char *)&l, (int)sizeof(l));
35 (void)shutdown((SOCKET)sock->realsocket, SD_RECEIVE);
36 }
37 else
38 {
39 (void)shutdown((SOCKET)sock->realsocket, SD_BOTH);
40 }
41 (void)closesocket((SOCKET)sock->realsocket);
42 }
本文版权归作者 kanego 和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.