TCP客戶端頻繁鏈接退出,服務端避免內存的重複開闢

防止內存的重複開闢:Free 而不 Destory數據結構

參數說明:指針

lpFreeSocketList:服務端數據結構中保存的退出的客戶對象鏈表頭對象

lpSocketListCs:臨界區指針內存

s:SOCKET對象it

LPSOCKET_OBJ GetSocketObj(LPSOCKET_OBJ * lpFreeSocketList, CRITICAL_SECTION * lpSocketListCs, SOCKET s)
{
LPSOCKET_OBJ lpSockObj = NULL;io

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 = NULL;
lpSockObj->lClosingStatus = 0;
memset(lpSockObj->szPeerAddr, 0, sizeof(lpSockObj->szPeerAddr));
}
return lpSockObj;
}List

相關文章
相關標籤/搜索