我有一台 linux 机器和一台 Windows 机器我有一个 C++ 程序,它充当 linux 机器上的服务器,而 Qt 应用程序充当 Windows 机器上的客户端。我使用套接字作为加密密钥(基本异或加密)
问题是当我多次点击连接时,我得到了假字符串。我认为问题出在 qt 应用程序中,这是我的代码。我试图让一个全局标志忙,以防止用户多次点击连接按钮,但这个标志永远不会被触发。
为什么?我应该使用互斥锁吗?如果是怎么办?
void MainWindow::performRead()
{
QTcpSocket * sock = static_cast<QTcpSocket*>(this->sender());
if (key == 0)
{
busy = true;
QString recv(sock->readLine());
key = recv.toInt();
qDebug() << "Cheia este " << key;
char * response = enc_dec("#AUTH|admin|admin",strlen("#AUTH|admin|admin"),key);
sock->write(response);
}
else
{
busy = true;
while (sock->bytesAvailable() > 0)
{
unsigned short word;
sock->read((char*)(&word),2);
qDebug()<<word;
QByteArray bts = sock->read(word);
char * decodat = enc_dec((char*)bts.data(),bts.length() - 2,key);
char testx[2];
sock->peek(&testx[0],2);
qDebug() << decodat;
}
}
busy = false;
}
void MainWindow::on_pushButton_clicked()
{
if (!busy)
{
key = 0;
QTcpSocket * sock = new QTcpSocket();
connect(sock,SIGNAL(readyRead()),this,SLOT(performRead()));
sock->connectToHost("194.110.212.46",6550);
}
else qDebug()<<"Can't you see I am busy??" << endl;
}
char * enc_dec(char * str,int len, int key)
{
unsigned char keys[2];
keys[0] = (unsigned char)key;
keys[1] = keys[0] ^ 255;
char * nou = new char[len + 1];
nou[len] = '\0';
for (int i = 0; i < len; i++)
{
char tmpy = str[i] ^ keys[i % 2];
nou[i] = tmpy;
}
return nou;
}