首页 > 代码库 > TCP客户端频繁连接退出,服务端避免内存的重复开辟
TCP客户端频繁连接退出,服务端避免内存的重复开辟
防止内存的重复开辟:Free 而不 Destory
参数说明:
lpFreeSocketList:服务端数据结构中保存的退出的客户对象链表头
lpSocketListCs:临界区指针
s:SOCKET对象
LPSOCKET_OBJ GetSocketObj(LPSOCKET_OBJ * lpFreeSocketList, CRITICAL_SECTION * lpSocketListCs, SOCKET s)
{
LPSOCKET_OBJ lpSockObj = NULL;
EnterCriticalSection(lpSocketListCs);
if(*lpFreeSocketList)
{
lpSockObj = *lpFreeSocketList;
*lpFreeSocketList = lpSockObj->lpNext;
lpSockObj->lpNext = NULL;
}
else
{
lpSockObj = (LPSOCKET_OBJ)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SOCKET_OBJ));
if(lpSockObj != NULL)
{
InitializeCriticalSection(&lpSockObj->SockCritSec);
}
}
LeaveCriticalSection(lpSocketListCs);
// Initialize the members
if(lpSockObj)
{
lpSockObj->s = s;
lpSockObj->lpUserData = http://www.mamicode.com/NULL;
lpSockObj->lClosingStatus = 0;
memset(lpSockObj->szPeerAddr, 0, sizeof(lpSockObj->szPeerAddr));
}
return lpSockObj;
}
TCP客户端频繁连接退出,服务端避免内存的重复开辟