lanpaper/Client.cpp

63 lines
1.6 KiB
C++

#include "Client.h"
Client::Client(QObject *parent) :
QObject(parent)
{
tcpSocket = new QTcpSocket(this);
connect(tcpSocket,SIGNAL(connected()),this,SLOT(connected()) );
}
void Client::sendMessage(const QString& message)
{
Secret* secret=new Secret;
QString encodedText=secret->getEncodeString(message);
this->message=encodedText;
//qDebug()<<Config::getDesIP()<<"\n";
//qDebug()<<QString(Config::getDesPort()).toInt()<<"\n";
this->tcpSocket->close();
this->startConnection();
this->socketState=QTcpSocket::ConnectingState;
this->trialCount=1;
this->timer=this->startTimer(1000);
//qDebug()<<"start timer"<<"\n";
}
void Client::connected()
{
tcpSocket->write(message.toUtf8());
tcpSocket->waitForBytesWritten(10*1000);
//tcpSocket->write("\n\n\n\n\n");
tcpSocket->waitForBytesWritten(10*1000);
this->socketState=QTcpSocket::ConnectedState;
emit this->connectionSuccess(true);
this->killTimer(this->timer);
this->tcpSocket->close();
}
void Client::startConnection()
{
//qDebug()<<Config::getDesIP()<<"\n";
this->tcpSocket->connectToHost(Config::getDesIP(),QString(Config::getDesPort()).toInt() );
}
void Client::timerEvent(QTimerEvent *e)
{
//qDebug()<<"try"<<this->trialCount<<"\n";
if(this->trialCount==6)
{
this->tcpSocket->close();
this->killTimer(this->timer);
emit this->connectionSuccess(false);
}
if(this->socketState==QTcpSocket::ConnectingState)
{
//this->tcpSocket->close();
//this->startConnection();
this->trialCount+=1;
}
}