typedef struct SocketInfo
{
char IPAddress[20]; //IP地址
SOCKET hSocket; //socket句柄
char cpName[128]; //计算机名
char wVersion[64]; //操作系统版本
SocketInfo * next; //下一个节点指针
private:
SocketInfo * head;
}SocketInfo;
class SocketData //连接链表
{
public:
SocketData();
~SocketData();
int GetSocketNum();//获取链表数量
BOOL DeleteSocket(char * IPAddress , char * cpName);//删除指定节点
BOOL AppendSocket(char * IPAddress , SOCKET hSocket , char * cpName , char * wVersion );//尾部添加节点
SocketInfo * GetSocketInformation(char * IPAddress , char * cpName);//获取指定节点信息
private:
SocketInfo * head;
};
SocketData::SocketData()
{
head = NULL;
}
SocketData::~SocketData()
{
SocketInfo * p = head;
SocketInfo * p1 = head;
if(head != NULL)
{
do
{
p1 = p;
p = p->next;
free(p1);
}while(p != NULL);
head = NULL;
}
}
int SocketData::GetSocketNum()
{
int num = 0;
SocketInfo * p = head;
if(head != NULL)
{
do
{
p = p->next;
num++;
}while(p != NULL);
}
return num;
}
BOOL SocketData::AppendSocket(char * IPAddress , SOCKET hSocket , char * cpName , char * wVersion )
{
SocketInfo * p = head;
if(head != NULL) //如果是非空表
{
while(p->next != NULL)
{
p=p->next;
}
SocketInfo * newnode = (SocketInfo * )malloc(LEN);
strcpy(newnode->IPAddress , IPAddress);
newnode->hSocket = hSocket;
strcpy(newnode->cpName , cpName);
strcpy(newnode->wVersion , wVersion);
newnode->next = NULL;
p->next = newnode;
}
else //如果是空表
{
SocketInfo * newnode = (SocketInfo * )malloc(LEN);
strcpy(newnode->IPAddress , IPAddress);
newnode->hSocket = hSocket;
strcpy(newnode->cpName , cpName);
strcpy(newnode->wVersion , wVersion);
newnode->next = NULL;
head = newnode;
}
return TRUE;
}
BOOL SocketData::DeleteSocket(char * IPAddress , char * cpName)
{
SocketInfo * p = head;
SocketInfo * p1 = head;
if(head != NULL)//非空表删除节点
{
do
{
if(strcmp(p->IPAddress , IPAddress)==0 && strcmp(p->cpName , cpName)==0)
break;
p1 = p;
p = p->next;
}while(p != NULL);
if(p == NULL) //搜索至表尾部没有发现搜索项目
return FALSE;
if(p1 == head && p ==head) //首节点
{
if(p->next!=NULL) //不止一个节点
{
head = p->next;
free(p);
}
else //只有一个节点
{
head = NULL;
free(p);
}
}
else if (p->next == NULL) //尾节点
{
p1->next = NULL;
free(p);
}
else //中间节点
{
p1->next = p->next;
free(p);
}
return TRUE;
}
return FALSE; //空表不能删除节点
}
SocketInfo * SocketData::GetSocketInformation(char * IPAddress , char * cpName)
{
SocketInfo * p = head;
if(head != NULL)//非空表获取信息
{
do
{
if(strcmp(p->IPAddress , IPAddress)==0 && strcmp(p->cpName , cpName)==0)
break;
p = p->next;
}while(p != NULL);
if(p == NULL) //搜索至表尾部没有发现搜索项目
return NULL;
return p;
}
return NULL; //空表不能获取信息
}