为了在原有的功能基础上添加注销账号功能,需要完成以下几个步骤:
1. **定义注销账号函数**:编写一个函数 `Sign_out` 来处理注销操作。
2. **更新主循环中的选项**:在 `handleclient` 函数中添加新的选项,允许用户选择注销账号。
3. **更新文件保存逻辑**:确保注销后的账号信息能够正确地保存到文件中。
以下是具体的实现代码:
### 1. 定义注销账号函数
```c
// 注销功能函数
void Sign_out(int fd) {
X* p1, *p;
int flag = 0; // 标识符,账号是否存在
char str[256];
memset(str, 0, 256);
strcpy(str, "请输入您想注销的账号");
send(fd, str, strlen(str), 0);
memset(str, 0, 256);
recv(fd, str, 256, 0);
p1 = head;
while (p1->next) {
if (strcmp(p1->next->id, str) == 0) {
flag = 1;
break;
}
p1 = p1->next;
}
if (flag == 0) {
memset(str, 0, 256);
strcpy(str, "账号不存在");
send(fd, str, strlen(str), 0);
return;
}
// 删除节点
p = p1->next;
p1->next = p->next;
free(p);
count--;
memset(str, 0, 256);
strcpy(str, "注销成功");
send(fd, str, strlen(str), 0);
Save_account(); // 保存一次账号信息在文件
}
```
### 2. 更新主循环中的选项
在 `handleclient` 函数中添加新的选项,允许用户选择注销账号:
```c
void* handleclient(void* newfd) {
int fd = *(int*)newfd;
char recvbuf[1024];
char recvbuf1[1024];
char sendbuf[1024];
memset(recvbuf, 0, 1024);
memset(recvbuf1, 0, 1024);
memset(sendbuf, 0, 1024);
if (recv(fd, recvbuf, 1024, 0) == -1) {
perror("recv"); // 错误提示
exit(1);
} else {
printf("cli say:%s", recvbuf);
}
p:
while (1) {
memset(sendbuf, 0, 1024);
// 登录后界面
strcpy(sendbuf, "请选择进行的操作:\t1.登录帐号\t2.注册帐号\t3.注销帐号\tend.退出");
send(fd, sendbuf, strlen(sendbuf), 0);
memset(recvbuf, 0, sizeof(recvbuf));
recv(fd, recvbuf, 1024, 0);
if (strcmp(recvbuf, "1") == 0) { // 接收到客户端输入为1,请输入登录账号
memset(sendbuf, 0, 1024);
strcpy(sendbuf, "请输入登录账号");
send(fd, sendbuf, strlen(sendbuf), 0);
memset(recvbuf, 0, sizeof(recvbuf));
recv(fd, recvbuf, 1024, 0);
memset(sendbuf, 0, sizeof(sendbuf));
strcpy(sendbuf, "请输入登录密码");
send(fd, sendbuf, strlen(sendbuf), 0);
memset(recvbuf1, 0, sizeof(recvbuf1));
recv(fd, recvbuf1, 1024, 0); // recvbuf存入帐号, recvbuf1存入密码,调验证函数验证帐号和密码是否正确
// 匹配账号密码是否都存在且正确
if (Verify_account(recvbuf) == 0 || Authentication_password(recvbuf, recvbuf1) == 0) {
memset(sendbuf, 0, 1024);
strcpy(sendbuf, "输入账号或密码不正确");
send(fd, sendbuf, strlen(sendbuf), 0);
} else {
strcpy(recvbuf1, recvbuf); // 把id存入recvbuf1, recvbuf用于接受客户端发来的信息
memset(sendbuf, 0, 1024);
strcpy(sendbuf, "登陆成功");
X* p;
p = head;
while (p->next) {
if (strcmp(p->next->id, recvbuf1) == 0) { // 登录成功后去查找获取其对应的套接字,便于通信
p->next->fd = fd;
break;
}
p = p->next;
}
send(fd, sendbuf, strlen(sendbuf), 0);
// 登录之后的操作
while (1) {
memset(sendbuf, 0, 1024);
strcpy(sendbuf, "请选择你要进行的操作:1.群聊 2.私聊 3.退出");
send(fd, sendbuf, strlen(sendbuf), 0);
memset(recvbuf, 0, sizeof(recvbuf));
if (recv(fd, recvbuf, 1024, 0) == -1) {
perror("recv");
exit(1);
}
if (strcmp(recvbuf, "1") == 0) { // 进入多人聊天室
Multiplayer_chat(fd, recvbuf1);
} else if (strcmp(recvbuf, "2") == 0) { // 双人聊天
Direct_Message(fd, recvbuf1);
} else if (strcmp(recvbuf, "3") == 0) { // 退出, 返回到登录界面
goto p;
}
}
}
} else if (strcmp(recvbuf, "2") == 0) {
Sign_in(fd);
goto p;
} else if (strcmp(recvbuf, "3") == 0) { // 注销账号
Sign_out(fd);
goto p;
} else if (strcmp(recvbuf, "end") == 0) {
memset(sendbuf, 0, 1024);
strcpy(sendbuf, "欢迎使用,再见");
send(fd, sendbuf, strlen(sendbuf), 0);
break;
} else {
continue; // 如果选择不存在的功能键,直接continue继续
}
}
close(fd);
}
```
### 3. 更新文件保存逻辑
已经在 `Sign_out` 函数中调用了 `Save_account()` 函数,因此文件保存逻辑已经更新。
通过以上修改,您的服务器程序将支持注销账号功能。用户可以在登录后选择注销自己的账号,并且注销后的账号信息将被正确地保存到文件中。