一、服务器端编程
首先,建立服务器端工程“TimeServer.pro”。文件代码如下。
(1)在头文件“dialog.h”中,定义服务器端界面类Dialog继承自QDialog类,其具体代码如下:
#include <QDialog>
#include <QLabel>
#include <QPushButton>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
QLabel *Label1; //此标签用于显示监听端口
QLabel *Label2; //此标签用于显示请求次数
QPushButton *quitBtn; //退出按钮
};
(2)在源文件“dialog.cpp”中,Dialog类的构造函数完成了初始化界面,其具体代码如下:
#include "dialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
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()));
}
(3)此时运行服务器端工程“TimeServer.pro”,界面显示如下图所示:
(4)在服务器端工程“TimeServer.pro”中,添加C++ Class文件“timethread.h”及“timethread.cpp”。
在头文件“timethread.h”中,工作线程TimeThread类继承自QThread类,实现TCP套接字,其具体代码如下:
#include <QThread>
#include <QtNetwork>
#include <QTcpSocket>
class TimeThread : public QThread
{
Q_OBJECT
public:
TimeThread(qintptr socketDescriptor,QObject *parent=0);
void run(); //重写此虚函数
signals:
void error(QTcpSocket::SocketError socketError); //出错信号
private:
qintptr socketDescriptor; //套接字描述符
};
(5)在源文件“timethread.cpp”中,TimeThread类的构造函数只是初始化了套接字描述符,其具体代码如下:
#include "timethread.h"
#include <QDateTime>
#include <QByteArray>
#include <QDataStream>
TimeThread::TimeThread(qintptr socketDescriptor,QObject *parent)
:QThread(parent),socketDescriptor(socketDescriptor)
{
}
TimeThread::run()函数是工作线程(TimeThread)的实质所在,当在TimeServer::incomingConnection()函数中调用了thread->start()函数后,此虚函数开始执行,其具体代码如下:
void TimeThread::run()
{
QTcpSocket tcpSocket; //创建一个QTcpSocket类
if(!tcpSocket.setSocketDescriptor(socketDescriptor)) //将以上创建的QTcpSocket类置以从构造函数中传入的套接字描述符,用于向客户端传回服务器端的当前时间。
{
emit error(tcpSocket.error()); //如果出错,则发出error(tcpSocket.error())信号报告错误。
return;
}
QByteArray block;
QDataStream out(&block,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_11);
uint time2u = QDateTime::currentDateTime().toTime_t();//(c)
out<<time2u;
tcpSocket.write(block); //将获得的当前时间传回客户端
tcpSocket.disconnectFromHost(); //断开连接
tcpSocket.waitForDisconnected(); //如果不出错,则开始获取当前时间。
}
(6)在服务器端工程“TimeServer.pro”中添加C++ Class文件“timeserver.h”及“timeserver.cpp”。在头件“timeserver.h”中,实现了一个TCP服务器端,TimeServer类继承自QTcpServer类,其具体代码如下:
#include <QTcpServer>
class Dialog; //服务器端的声明
class TimeServer : public QTcpServer
{
Q_OBJECT
public:
TimeServer(QObject *parent=0);
protected:
void incomingConnection(qintptr socketDescriptor); //重写此虚函数。这个函数在TCP服务器端有新的连接时被调用,其参数为所接收新连接的套接字描述符。
private:
Dialog *dlg; //用于记录创建这个TCP服务器端对象的父类,这里是界面指针,通过这个指针将线程发出的消息关联到界面的槽函数中。
};
(7)在源文件“timeserver.cpp”中,构造函数只是用传入的父类指针parent初始化私有变量dlg,其具体代码如下:
#include "timeserver.h"
#include "timethread.h"
#include "dialog.h"
TimeServer::TimeServer(QObject *parent):QTcpServer(parent)
{
dlg =(Dialog *)parent;
}
重写的虚函数incomingConnection()的具体代码如下:
void TimeServer::incomingConnection(qintptr socketDescriptor)
{
TimeThread *thread = new TimeThread(socketDescriptor,0); //以返回的套接字描述符socketDescriptor创建一个工作线程TimeThread。
connect(thread,SIGNAL(finished()),dlg,SLOT(slotShow()));//将上述创建的线程结束消息函数finished()关联到槽函数slotShow()用于显示请求计数。此操作中,因为信号是跨线程的,所以使用了排队连接方式。
connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()),
Qt::DirectConnection); //将上述创建的线程结束消息函数finished()关联到线程自身的槽函数 deleteLater()用于结束线程。
thread->start(); //启动上述创建的线程。执行此语句后,工作线程(TimeThread)的虚函数run()开始执行。
}
(8)在服务器端界面的头文件“dialog.h”中添加的具体代码如下:
class TimeServer;
public slots:
void slotShow(); //此槽函数用于界面上显示的请求次数
private:
TimeServer *timeServer; //TCP服务器端timeServer
int count; //请求次数计数器count
(9)在源文件“dialog.cpp”中,添加的头文件如下:
#include <QMessageBox>
#include "timeserver.h"
其中,在Dialog类的构造函数中添加的内容,用于启动服务器端的网络监听,其具体实现如下:
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.cpp”中,槽函数slotShow()的具体内容如下:
void Dialog::slotShow()
{
Label2->setText(tr("第%1次请求完毕。").arg(++count)); //在标签Label2上显示当前的请求次数,并将请求数计数count加1。注意,槽函数slotShow()虽然被多个线程激活,但调用入口只有主线程的事件循环这一个。多个线程的激活信号最终会在主线程的事件循环中排队调用此槽函数,从而保证了count变量的互斥访问。因此,槽函数slotShow()是一个天然的临界区。
}
(10)在服务器端工程文件“TimeServer.pro”中添加如下代码:
QT += network
(11)最后运行服务器端工程“TimeServer.pro”,结果如下图所示。
二、客户端编程
操作步骤如下:
(1)建立客户端工程“TimeClient.pro”。在头文件“timeclient.h”中,定义了客户端界面类 TimeClient继承自QDialog类,其具体代码如下:
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QDateTimeEdit>
#include <QTcpSocket>
#include <QAbstractSocket>
class TimeClient : public QDialog
{
Q_OBJECT
public:
TimeClient(QWidget *parent = 0);
~TimeClient();
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;
};
(2)在源文件“timeclient.cpp”中,TimeClient类的构造函数完成了初始化界面,其具体代码如下:
#include "timeclient.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QDataStream>
#include <QMessageBox>
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();
}
在源文件“timeclient.cpp”中,enableGetBtn()函数的具体代码如下:
void TimeClient::enableGetBtn()
{
getBtn->setEnabled(!serverNameLineEdit->text().isEmpty()&& !portLineEdit->text().isEmpty());
}
在源文件“timeclient.cpp”中,getTime()函数的具体代码如下:
void TimeClient::enableGetBtn()
{
getBtn->setEnabled(!serverNameLineEdit->text().isEmpty()&&
!portLineEdit->text().isEmpty());
}
在源文件“timeclient.cpp”中,readTime ()函数的具体代码如下:
void TimeClient::readTime()
{
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_5_8);
if(time2u==0)
{
if(tcpSocket->bytesAvailable()<(int)sizeof(uint))
return;
in>>time2u;
}
dateTimeEdit->setDateTime(QDateTime::fromTime_t(time2u));
getBtn->setEnabled(true);
}
在源文件“timeclient.cpp”中,showError()函数的具体代码如下:
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);
}
(3)在客户端工程文件“TimeClient.pro”中,添加如下代码:
QT += network
(4)运行客户端工程“TimeClient.pro”,显示界面如下图所示:
最后,同时运行服务器和客户端程序,单击客户端“获取时间”按钮,从服务器上获得当前的系统时间,
如下图所示: