一、服务器端实现
1.tcpserver.h
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QMainWindow>
#include <QtNetWork>
namespace Ui {
class TcpServer;
}
class TcpServer : public QMainWindow
{
Q_OBJECT
public:
explicit TcpServer(QWidget *parent = 0);
~TcpServer();
private:
Ui::TcpServer *ui;
QTcpServer *tcpServer;
private slots:
void sendMessage();
};
#endif // TCPSERVER_H
2.tcpserver.cpp
#include "tcpserver.h"
#include "ui_tcpserver.h"
TcpServer::TcpServer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TcpServer)
{
ui->setupUi(this);
tcpServer = new QTcpServer(this);
if(!tcpServer->listen(QHostAddress::LocalHost,6666))
{
qDebug()<<tcpServer->errorString();
close();
}
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(sendMessage())); //连接信号和相应槽函数
//有新的连接进来就执行sendMessage函数
}
TcpServer::~TcpServer()
{
delete ui;
delete tcpServer;
}
void TcpServer::sendMessage()
{
QByteArray block; //用于暂存要发的数据
QDataStream out(&block,QIODevice::WriteOnly); //使用数据流写入数据
out.setVersion(QDataStream::Qt_5_6); //设置数据流的版本,客户端和服务器端使用版本要相同
out<<(quint16) 0;
out<<tr("Hello Tcp!!!");
out.device()->seek(0);
out<<(quint16) (block.size()-sizeof(quint16));
QTcpSocket *clientConnection=tcpServer->nextPendingConnection(); //获取已经建立的连接的子套接字
connect(clientConnection,SIGNAL(disconnected()),clientConnection,SLOT(deleteLater()));
clientConnection->write(block);
clientConnection->disconnectFromHost();
ui->statusLabel->setText("Send message successful!!!");
}
二、客户端实现
1.tcpclient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QMainWindow>
#include <QtNetwork>
namespace Ui {
class TcpClient;
}
class TcpClient : public QMainWindow
{
Q_OBJECT
public:
explicit TcpClient(QWidget *parent = 0);
~TcpClient();
private:
Ui::TcpClient *ui;
QTcpSocket *tcpClient;
QString message; //存放从服务器接收到的字符串
quint16 blockSize; //存放文件的大小信息
private slots:
void newConnect(); //连接服务器
void readMessage(); //接收数据
void displayError(QAbstractSocket::SocketError); //显示错误
void on_pushButton_clicked();
};
#endif // TCPCLIENT_H
2.tcpclient.cpp
#include "tcpclient.h"
#include "ui_tcpclient.h"
TcpClient::TcpClient(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TcpClient)
{
ui->setupUi(this);
tcpClient=new QTcpSocket(this);
connect(tcpClient,SIGNAL(readyRead()),this,SLOT(readMessage())); //有数据来时执行readMessage函数
connect(tcpClient,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(displayError(QAbstractSocket::SocketError))); //发生错误时执行displayError函数
}
TcpClient::~TcpClient()
{
delete ui;
delete tcpClient;
}
void TcpClient::newConnect()
{
blockSize=0;
tcpClient->abort(); //取消已经有的连接
tcpClient->connectToHost(ui->hostLineEdit->text(),
ui->portLineEdit->text().toInt());
}
void TcpClient::readMessage()
{
QDataStream in(tcpClient);
in.setVersion(QDataStream::Qt_5_6); //设置数据流版本,这里要和服务器端相同
if(blockSize==0)
{
if(tcpClient->bytesAvailable()<(int)sizeof(quint16)) return;
in>>blockSize;
}
if(tcpClient->bytesAvailable()<blockSize) return;
in>>message;
ui->messageLabel->setText(message);
}
void TcpClient::displayError(QAbstractSocket::SocketError)
{
qDebug()<<tcpClient->errorString(); //输出出错信息
}
void TcpClient::on_pushButton_clicked()
{
newConnect(); //请求连接
}
三、运行结果