Implementation content:
1.C++ connects to mysql database
2. Implement login, registration, and logout functions
The server encapsulates the ClientManager class and uses singleton mode to manage the users database.
#pragma once
#include<mysql.h>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
typedef struct Client
{
int id;
string name;
string password;
bool operator==(const struct Client& W)
{
return W.id == this->id
&& W.name == this->name
&& W.password == this->password;
}
}Client;
class ClientManager
{
ClientManager();
~ClientManager();
public :
static ClientManager* GetInstance()
{
static ClientManager ClientManager_Instance;
return &ClientManager_Instance;
}
public:
int insert_client(Client& t);
bool update_client(Client& t);
bool delete_client(int client_id,std::string password);
vector<Client> get_clients(string condition ="");
private:
MYSQL* con;
const char* host = "localhost";
const char* user = "root";
const char* pw = "123456";
const char* database_name = "database_client";
};
Client core code
while (true)
{
printf("请输入数字进行您的操作:1 登陆 2 注册 3 注销\n");
int flag;
scanf("%d", &flag);
std::string s = "";
std::string tmp;
if (flag == 1)
{
printf("请输入您的账号ID\n");
std::string tmp;
std::cin >> tmp;
s += "1 " + tmp + " ";
printf("请输入您的密码\n");
std::cin >> tmp;
s += tmp;
send(cSock, s.c_str(),s.size(), 0);
int len = recv(cSock, recvmsg, sizeof(recvmsg) - 1, 0);
if (len == -1)
{
printf("服务器接受消息失败\n");
return 0;
}
recvmsg[len] = '\0';
if (strcmp(recvmsg, "yes")==0)
{
printf("登录成功!%s\n", recvmsg);
break;
}
else
{
printf("登录失败:账号不存在或者密码错误!%s\n", recvmsg);
continue;
}
}
else if (flag == 2)
{
printf("请输入您要注册的账号昵称\n");
std::string tmp;
std::cin >> tmp;
s += "2 " + tmp + " ";
printf("请输入您要注册的密码\n");
std::cin >> tmp;
s += tmp;
send(cSock, s.c_str(), s.size(), 0);
int len = recv(cSock, recvmsg, sizeof(recvmsg) - 1, 0);
if (len == -1)
{
printf("服务器接受消息失败\n");
return 0;
}
recvmsg[len] = '\0';
printf("注册成功!\n");
printf("您的账号ID是 %s \n您已进入聊天室\n", recvmsg);
break;
}
else if (flag == 3)
{
printf("请输入您要注销的账号ID\n");
std::string tmp;
std::cin >> tmp;
s += "3 " + tmp + " ";
printf("请输入您要注销的账号密码\n");
std::cin >> tmp;
s += tmp;
send(cSock, s.c_str(), s.size(), 0);
int len = recv(cSock, recvmsg, sizeof(recvmsg) - 1, 0);
if (len == -1)
{
printf("服务器接受消息失败\n");
return 0;
}
recvmsg[len] = '\0';
if(strcmp(recvmsg, "yes") == 0)
{
printf("注销成功!\n");
}
else
{
printf("注销失败!没有此账号或密码错误!\n");
}
continue;
}
else
{
printf("未知符号!重新输入!");
continue;
}
}
Server core code (login, registration, logout)
void client_login(int fd,std::string s, ClientManager* CM)
{
std::vector<std::string> tokens;
std::istringstream iss(s);
std::string token;
while (iss >> token) {
tokens.push_back(token);
}
std::string id = tokens[1], password = tokens[2];
vector<Client> ret = CM->get_clients("WHERE id=" + id + " AND password='" + password + "'");
if (ret.size() > 0)
{
client_info[fd] = ret[0];
client_online[fd] = true;
printf("%d login success ! id = %d", fd,ret[0].id);
std::cout << " name = " << ret[0].name << '\n';
write(fd, "yes", 3);
}
else
{
write(fd, "no", 2);
}
}
void client_register(int fd,std::string s, ClientManager* CM)
{
std::vector<std::string> tokens;
std::istringstream iss(s);
std::string token;
while (iss >> token) {
tokens.push_back(token);
}
std::string name = tokens[1], password = tokens[2];
Client tmp;
tmp.name = name;
tmp.password = password;
int tmp_id = CM->insert_client(tmp);
if (tmp_id < 0)
{
printf("%d register failed !", fd);
}
else
{
printf("%d register success ! id = %d\n", fd, tmp_id);
tmp.id = tmp_id;
client_online[fd] = true;
client_info[fd] = tmp;
std::string tmp_id_s = std::to_string(tmp_id);
write(fd, tmp_id_s.c_str(), tmp_id_s.size());
}
}
void client_delete(int fd, std::string s, ClientManager* CM)
{
std::vector<std::string> tokens;
std::istringstream iss(s);
std::string token;
while (iss >> token) {
tokens.push_back(token);
}
int id = atoi(tokens[1].c_str());
std::string password = tokens[2];
if (CM->delete_client(id, password))
{
write(fd, "yes", 3);
}
else
{
write(fd, "no", 2);
}
}