运行截图如下:
代码如下:
客户端:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QAbstractSocket>
class QTcpSocket;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QString m_message;
QTcpSocket *m_tcpSocket;
quint16 m_blockSize; //存放数据大小
private slots:
void newConnect();
void readMessage();
void displayError(QAbstractSocket::SocketError);
void connectBtnClicked();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QtNetwork>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_tcpSocket=new QTcpSocket(this);
connect(m_tcpSocket,&QTcpSocket::readyRead,this,&Widget::readMessage);
connect(m_tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
connect(ui->connectPushButton,&QPushButton::clicked,this,Widget::connectBtnClicked);
}
void Widget::newConnect(){
m_blockSize=0;
m_tcpSocket->abort();
m_tcpSocket->connectToHost(ui->hostLineEdit->text(),ui->portLineEdit->text().toInt());
}
void Widget::readMessage(){
QDataStream in(m_tcpSocket);
in.setVersion(QDataStream::Qt_5_7);
if(m_blockSize==0){
if(m_tcpSocket->bytesAvailable()<(int)sizeof(quint16))
return;
in>>m_blockSize;
}
if(m_tcpSocket->bytesAvailable()<m_blockSize) return;
in>>m_message;
qDebug()<<"message:"<<m_message;
ui->listWidget->insertItem(0,m_message);
}
void Widget::displayError(QAbstractSocket::SocketError){
qDebug()<<m_tcpSocket->errorString();
}
void Widget::connectBtnClicked(){
newConnect();
}
Widget::~Widget()
{
delete ui;
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
服务端:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QTcpServer;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QTcpServer *m_tcpServer;
private slots:
void sendMessage();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QtNetwork>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_tcpServer=new QTcpServer(this);
if(!m_tcpServer->listen(QHostAddress::LocalHost,10086)){
qDebug()<<m_tcpServer->errorString();
close();
}
connect(m_tcpServer,&QTcpServer::newConnection,this,&Widget::sendMessage);
}
Widget::~Widget()
{
delete ui;
}
void Widget::sendMessage()
{
QByteArray block;
QDataStream out(&block,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_7);
out<<(quint16)0;
out<<QString::fromUtf8("你妹");
out.device()->seek(0);
out<<(quint16)(block.size()-sizeof(quint16));
QTcpSocket *clientConnection=m_tcpServer->nextPendingConnection();
connect(clientConnection,&QTcpSocket::disconnected,clientConnection, &QTcpSocket::deleteLater);
clientConnection->write(block);
clientConnection->disconnectFromHost();
ui->label->setText("发送数据成功");
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}