当有客户请求到达时,服务器将启动一个新的进程为它返回当前时间,服务完毕后这个线程将自动退出。
运行截图如下:
服务端:
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
#include
class TimeServer;
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
public slots:
void slotShow();
private:
QLabel *Label1;
QLabel *Label2;
QPushButton *quitBtn;
TimeServer *timeServer;
int count;
};
#endif // DIALOG_H
timeserver.h
#ifndef TIMESERVER_H
#define TIMESERVER_H
#include
#include
class Dialog;
class TimeServer : public QTcpServer
{
Q_OBJECT
public:
TimeServer(QObject *parent=0);
protected:
void incomingConnection(int socketDescriptor);
private:
Dialog *dlg;
};
#endif // TIMESERVER_H
timethread.h
#ifndef TIMETHREAD_H
#define TIMETHREAD_H
#include
#include
#include
class TimeThread : public QThread
{
Q_OBJECT
public:
TimeThread(int socketDescriptor,QObject *parent=0);
void run();
signals:
void error(QTcpSocket::SocketError socketError);
private:
int socketDescriptor;
};
#endif // TIMETHREAD_H
dialog.cpp
#include "dialog.h"
#include
#include
#include
#include "timeserver.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("多线程时间服务器"));
Label1 =new QLabel(tr("服务器端口:"));
Label2 = new QLabel;
quitBtn = new QPushButton(tr("退出"));
QHBoxLayout *BtnLayout = new QHBoxLayout;
BtnLayout->addStretch(1);
BtnLayout->addWidget(quitBtn);
BtnLayout->addStretch(1);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(Label1);
mainLayout->addWidget(Label2);
mainLayout->addLayout(BtnLayout);
connect(quitBtn,SIGNAL(clicked()),this,SLOT(close()));
count=0;
timeServer = new TimeServer(this);
if(!timeServer->listen())
{
QMessageBox::critical(this,tr("多线程时间服务器"),
tr("无法启动服务器:%1.").arg(timeServer->errorString()));
close();
return;
}
Label1->setText(tr("服务器端口:%1.").arg(timeServer->serverPort()));
}
Dialog::~Dialog()
{
}
void Dialog::slotShow()
{
Label2->setText(tr("第%1次请求完毕。").arg(++count));
}
main.cpp
#include "dialog.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
timeserver.cpp
#include "timeserver.h"
#include "timethread.h"
#include "dialog.h"
TimeServer::TimeServer(QObject *parent)
:QTcpServer(parent)
{
dlg =(Dialog *)parent;
}
void TimeServer::incomingConnection(int socketDescriptor)
{
TimeThread *thread = new TimeThread(socketDescriptor,0);
connect(thread,SIGNAL(finished()),dlg,SLOT(slotShow()));
connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()),Qt::DirectConnection);
thread->start();
}
timethread.cpp
#include "timethread.h"
#include
#include
#include
TimeThread::TimeThread(int socketDescriptor,QObject *parent)
:QThread(parent),socketDescriptor(socketDescriptor)
{
}
void TimeThread::run()
{
QTcpSocket tcpSocket;
if(!tcpSocket.setSocketDescriptor(socketDescriptor))
{
emit error(tcpSocket.error());
return;
}
QByteArray block;
QDataStream out(&block,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_3);
uint time2u = QDateTime::currentDateTime().toTime_t();
out<
客户端:
timeclient.h
#ifndef TIMECLIENT_H
#define TIMECLIENT_H
#include
#include
#include
#include
#include
#include
#include
#include
class TimeClient : public QDialog
{
Q_OBJECT
public:
TimeClient(QWidget *parent = 0);
~TimeClient();
virtual void timerEvent(QTimerEvent *event);
public slots:
void enableGetBtn();
void getTime();
void readTime();
void showError(QAbstractSocket::SocketError socketError);
private:
QLabel *serverNameLabel;
QLineEdit *serverNameLineEdit;
QLabel *portLabel;
QLineEdit *portLineEdit;
QDateTimeEdit *dateTimeEdit;
QLabel *stateLabel;
QPushButton *getBtn;
QPushButton *quitBtn;
uint time2u;
QTcpSocket *tcpSocket;
int m_timerId;
bool startRun;
};
#endif // TIMECLIENT_H
main.cpp
#include "timeclient.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TimeClient w;
w.show();
return a.exec();
}
timeclient.cpp
#include "timeclient.h"
#include
#include
#include
#include
#include
TimeClient::TimeClient(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("多线程时间服务客户端"));
serverNameLabel =new QLabel(tr("服务器名:"));
serverNameLineEdit = new QLineEdit("Localhost");
portLabel =new QLabel(tr("端口:"));
portLineEdit = new QLineEdit;
QGridLayout *layout = new QGridLayout;
layout->addWidget(serverNameLabel,0,0);
layout->addWidget(serverNameLineEdit,0,1);
layout->addWidget(portLabel,1,0);
layout->addWidget(portLineEdit,1,1);
dateTimeEdit = new QDateTimeEdit(this);
QHBoxLayout *layout1 = new QHBoxLayout;
layout1->addWidget(dateTimeEdit);
stateLabel =new QLabel(tr("请首先运行时间服务器!"));
QHBoxLayout *layout2 = new QHBoxLayout;
layout2->addWidget(stateLabel);
getBtn = new QPushButton(tr("获取时间"));
getBtn->setDefault(true);
getBtn->setEnabled(false);
quitBtn = new QPushButton(tr("退出"));
QHBoxLayout *layout3 = new QHBoxLayout;
layout3->addStretch();
layout3->addWidget(getBtn);
layout3->addWidget(quitBtn);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(layout);
mainLayout->addLayout(layout1);
mainLayout->addLayout(layout2);
mainLayout->addLayout(layout3);
connect(serverNameLineEdit,SIGNAL(textChanged(QString)),this,SLOT(enableGetBtn()));
connect(portLineEdit,SIGNAL(textChanged(QString)),this,SLOT(enableGetBtn()));
connect(getBtn,SIGNAL(clicked()),this,SLOT(getTime()));
connect(quitBtn,SIGNAL(clicked()),this,SLOT(close()));
tcpSocket = new QTcpSocket(this);
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readTime()));
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(showError(QAbstractSocket::SocketError)));
portLineEdit->setFocus();
m_timerId=startTimer(1000);
startRun=false;
}
void TimeClient::timerEvent(QTimerEvent *event){
if(event->timerId()==m_timerId && startRun){
getTime();
}
}
TimeClient::~TimeClient()
{
}
void TimeClient::enableGetBtn()
{
getBtn->setEnabled(!serverNameLineEdit->text().isEmpty()&&!portLineEdit->text().isEmpty());
}
void TimeClient::getTime()
{
getBtn->setEnabled(false);
startRun=true;
time2u =0;
tcpSocket->abort();
tcpSocket->connectToHost(serverNameLineEdit->text(),portLineEdit->text().toInt());
}
void TimeClient::readTime()
{
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_3);
if(time2u==0)
{
if(tcpSocket->bytesAvailable()<(int)sizeof(uint))
return;
in>>time2u;
}
dateTimeEdit->setDateTime(QDateTime::fromTime_t(time2u));
getBtn->setEnabled(true);
}
void TimeClient::showError(QAbstractSocket::SocketError socketError)
{
switch (socketError)
{
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("时间服务客户端"),tr("主机不可达!"));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("时间服务客户端"),tr("连接被拒绝!"));
break;
default:
QMessageBox::information(this, tr("时间服务客户端"),tr("产生如下错误: %1.").arg(tcpSocket->errorString()));
}
getBtn->setEnabled(true);
}