創建一個鏈式結構便於存儲用戶信息,以便從此處理登陸和未登陸的信息。首先咱們創建一個用戶信息的結構:緩存
/// <summary>客戶端信息結構</summary> typedef struct _client *lp_client; /// <summary>客戶端信息指針結構</summary> typedef struct _client { char userName[20]; //用戶名稱 char userPassword[20]; //用戶口令 char appCode[20]; //應用編碼 char machineCode[32]; //機器碼 char runtimeCode[32]; //運行碼 struct in_addr addr; //網址 u_short port; //通信端口 lp_client next; //鏈表下一個 };
如圖所示,所謂鏈式結構就是左側圖示的方式,從上面的結構上看,最後那個next就是用於連接的,爲了更好的操做這個連接,咱們須要創建兩個變量來便於操做和處理,這兩個變量是根節點和末節點變量。app
/// <summary>客戶信息列表根節點</summary> lp_client rootItem = NULL; /// <summary>客戶信息列表末節點</summary> lp_client lastItem = NULL;
下面提供一下整個代碼,包含查詢、增長、刪除等操做。函數
UDPList.hui
#pragma once #include "targetver.h" /// <summary>信息結構的指針型</summary> typedef struct _message *lp_message; /// <summary>信息結構</summary> typedef struct _message { union { struct { char buff[1030]; }; struct { char cmd[6]; char rt_code[32]; char data[992]; }; }; SOCKADDR_IN addr; }; /// <summary>客戶端信息結構</summary> typedef struct _client *lp_client; /// <summary>客戶端信息指針結構</summary> typedef struct _client { char userName[20]; //用戶名稱 char userPassword[20]; //用戶口令 char appCode[20]; //應用編碼 char machineCode[32]; //機器碼 char runtimeCode[32]; //運行碼 struct in_addr addr; //網址 u_short port; //通信端口 lp_client next; //鏈表下一個 }; /// <summary>根據用戶名稱獲得用戶信息節點</summary> lp_client IndexOf(char* appCode, char * userName); /// <summary>根據用戶名稱獲得用戶信息節點</summary> lp_client IndexOf(char* appCode, char * userName, char * machineCode); /// <summary>根據運行編碼獲得用戶信息節點</summary> lp_client IndexOf(char * runtimeCode); /// <summary>添加一個客戶端信息</summary> lp_client addClient(char* userName, char* userPassword, char* appCode, char* machineCode); /// <summary>添加一個客戶端信息</summary> void addClient(lp_client client); /// <summary>一個客戶端登陸系統</summary> lp_client regClient(char * userName, char * userPassword, char * appCode, struct in_addr addr, u_short port); /// <summary>一個客戶端登陸系統</summary> lp_client regClient(char * userName, char * userPassword, char * appCode, char * machineCode, struct in_addr addr, u_short port); /// <summary>根據用戶名稱刪除用戶信息節點</summary> void removeClient(char* appCode, char * userName); /// <summary>從文件緩存中提取客戶端列表</summary> void loadFromFile(char* filename); /// <summary>將客戶端列表寫入緩存文件</summary> void saveToFile(char* filename);
下面是UDPList.cpp編碼
#include "stdafx.h" #include "UDPList.h" /// <summary>客戶信息列表根節點</summary> lp_client rootItem = NULL; /// <summary>客戶信息列表末節點</summary> lp_client lastItem = NULL; void setRuntimeCode(lp_client client) { GUID gid; CoCreateGuid(&gid); sprintf(client->runtimeCode, "%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x", gid.Data1, gid.Data2, gid.Data3, gid.Data4[0], gid.Data4[1], gid.Data4[2], gid.Data4[3], gid.Data4[4], gid.Data4[5], gid.Data4[6], gid.Data4[7]); } /// <summary>根據用戶名稱獲得用戶信息節點</summary> lp_client IndexOf(char* appCode, char * userName) { if (rootItem == NULL)return NULL; lp_client tmp = rootItem; while (tmp) { if (strcmp(tmp->appCode, appCode) == 0 && strcmp(tmp->userName, userName) == 0) return tmp; tmp = tmp->next; } return NULL; } /// <summary>根據用戶名稱獲得用戶信息節點</summary> lp_client IndexOf(char* appCode, char * userName, char * machineCode) { if (rootItem == NULL)return NULL; lp_client tmp = rootItem; while (tmp) { if (strncmp(tmp->appCode, appCode, 20) == 0 && strncmp(tmp->userName, userName, 20) == 0 && strncmp(tmp->machineCode, machineCode, 32) == 0) return tmp; tmp = tmp->next; } return NULL; } /// <summary>根據運行編碼獲得用戶信息節點</summary> lp_client IndexOf(char * runtimeCode) { if (rootItem == NULL)return NULL; lp_client tmp = rootItem; while (tmp) { if (strcmp(tmp->runtimeCode, runtimeCode) == 0) return tmp; tmp = tmp->next; } return NULL; } /// <summary>一個客戶端登陸系統</summary> lp_client regClient(char * userName, char * userPassword, char * appCode, struct in_addr addr, u_short port) { lp_client result = IndexOf(userName, userPassword); if (result == NULL)return NULL; if (strcmp(result->userPassword, userPassword) != 0)return NULL; result->addr = addr; result->port = port; setRuntimeCode(result); return result; } /// <summary>一個客戶端登陸系統</summary> lp_client regClient(char * userName, char * userPassword, char * appCode, char * machineCode, struct in_addr addr, u_short port) { lp_client result = IndexOf(appCode, userName, machineCode); if (result == NULL)return NULL; if (strcmp(result->userPassword, userPassword) != 0)return NULL; result->addr = addr; result->port = port; setRuntimeCode(result); return result; } /// <summary>根據用戶名稱刪除用戶信息節點</summary> void removeClient(char * appCode, char * userName) { if (rootItem == NULL)return ; lp_client tmp = rootItem; lp_client parent = NULL; while (tmp) { if (strcmp(tmp->appCode, appCode) == 0 && strcmp(tmp->userName, userName) == 0) { if (parent == NULL)rootItem = tmp->next; else parent->next = tmp->next; if (tmp->next == NULL)lastItem = parent; free(tmp); return; } parent = tmp; tmp = tmp->next; } } /// <summary>從文件緩存中提取客戶端列表</summary> void loadFromFile(char * filename) { FILE *fp; if ((fp = fopen(filename, "rb")) == NULL) { printf("cant open the file"); exit(0); } lp_client client = (lp_client)malloc(sizeof(_client)); memset(client, 0, sizeof(_client)); while(fread(client, 20 + 20 + 20 + 32, 1, fp) > 0) { if (rootItem == NULL)rootItem = client; else lastItem->next = client; lastItem = client; client = (lp_client)malloc(sizeof(_client)); memset(client, 0, sizeof(_client)); } free(client); fclose(fp); } /// <summary>將客戶端列表寫入緩存文件</summary> void saveToFile(char * filename) { FILE * outfile = fopen(filename, "wb"); if (outfile == NULL) { printf("cant open the file"); exit(0); } lp_client client = rootItem; while (client) { fwrite(client, 20 + 20 + 20 + 32, 1, outfile); client = client->next; } fclose(outfile); } /// <summary>添加一個客戶端信息</summary> lp_client addClient(char* userName, char* userPassword, char* appCode, char* machineCode) { lp_client result = IndexOf(userName, userPassword); if (result != NULL)return result; result= (lp_client)malloc(sizeof(_client)); memset(result, 0, sizeof(_client)); strcpy(result->userName, userName); strcpy(result->userPassword, userPassword); strcpy(result->appCode, appCode); strcpy(result->machineCode, machineCode); addClient(result); return result; } /// <summary>添加一個客戶端信息</summary> void addClient(lp_client client) { if (rootItem == NULL)rootItem = client; else lastItem->next = client; lastItem = client; }
同時函數還實現了文件緩存功能,經過SaveToFile和LoadFromFile來實現。指針