A brief
1. The client's Internet are often composed of a plurality of n components, data sharing and communication among the components are completed using the local socket
packet 2. The agreement between the components, and define a common service field field, each component parses the packet is completed communication
II renderings
three local socket communication sample Code
#ifndef LOCALCHARTROOM_H
#define LOCALCHARTROOM_H
#include <QWidget>
#include <QLocalServer>
#include <QLocalSocket>
#include <QDateTime>
#include <QEventLoop>
#include <QTimer>
namespace Ui {
class LocalChartRoom;
}
class LocalChartRoom : public QWidget
{
Q_OBJECT
public:
explicit LocalChartRoom(QWidget *parent = nullptr);
~LocalChartRoom();
void initClient();
void initServer();
private slots:
void on_pushButton_clicked();
void on_pushButton_4_clicked();
void on_ServerConnected();
void on_ServerReadyRead();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_ClientReadyRead();
void on_Clientdisconnected();
void on_ClientConnected();
void on_pushButton_6_clicked();
void on_pushButton_5_clicked();
private:
Ui::LocalChartRoom *ui;
QLocalServer mServer;
QLocalSocket mClientsocket;
QEventLoop mLoop;
QLocalSocket *m_pServerSocket;
};
#endif // LOCALCHARTROOM_H
#include "localchartroom.h"
#include "ui_localchartroom.h"
LocalChartRoom::LocalChartRoom(QWidget *parent) :
QWidget(parent),
ui(new Ui::LocalChartRoom)
{
ui->setupUi(this);
}
LocalChartRoom::~LocalChartRoom()
{
delete ui;
}
//************************************************client
void LocalChartRoom::initClient()
{
disconnect(&mClientsocket,SIGNAL(connected()),this,SLOT(on_ClientConnected()));
disconnect(&mClientsocket,SIGNAL(disconnected()),this,SLOT(on_Clientdisconnected()));
disconnect(&mClientsocket,SIGNAL(readyRead()),this,SLOT(on_ClientReadyRead()));
connect(&mClientsocket,SIGNAL(connected()),this,SLOT(on_ClientConnected()));
connect(&mClientsocket,SIGNAL(disconnected()),this,SLOT(on_Clientdisconnected()));
connect(&mClientsocket,SIGNAL(readyRead()),this,SLOT(on_ClientReadyRead()));
mClientsocket.connectToServer("ChartRoom");
}
void LocalChartRoom::on_ClientConnected()
{
qDebug()<<"on_ClientConnected";
}
void LocalChartRoom::on_Clientdisconnected()
{
qDebug()<<"on_Clientdisconnected";
}
void LocalChartRoom::on_ClientReadyRead()
{
ui->textBrowser->append(QDateTime::currentDateTime().toString("yyyy-mm-dd hh:mm:ss.ms") + "\n" + "server:" + mClientsocket.readAll());
}
//************************************************client
//************************************************server
void LocalChartRoom::initServer()
{
disconnect(&mServer,SIGNAL(newConnection()),this,SLOT(on_ServerConnected()));
mServer.removeServer("ChartRoom");
connect(&mServer,SIGNAL(newConnection()),this,SLOT(on_ServerConnected()));
mServer.listen("ChartRoom");
}
void LocalChartRoom::on_ServerConnected()
{
m_pServerSocket = mServer.nextPendingConnection();
connect(m_pServerSocket,SIGNAL(readyRead()),this,SLOT(on_ServerReadyRead()));
}
void LocalChartRoom::on_ServerReadyRead()
{
ui->textBrowser_2->append(QDateTime::currentDateTime().toString("yyyy-mm-dd hh:mm:ss.ms") + "\n" + "client:" + m_pServerSocket->readAll());
}
//************************************************server
void LocalChartRoom::on_pushButton_clicked()
{
ui->textBrowser->append(QString::fromLocal8Bit("正在连接服务端..."));
QTimer::singleShot(1000,this,[=](){
mLoop.quit();
});
mLoop.exec();
initClient();
ui->textBrowser->append(QString::fromLocal8Bit("服务端连接成功"));
}
void LocalChartRoom::on_pushButton_4_clicked()
{
ui->textBrowser_2->append(QString::fromLocal8Bit("正在启动服务端..."));
QTimer::singleShot(1000,this,[=](){
mLoop.quit();
});
mLoop.exec();
initServer();
ui->textBrowser_2->append(QString::fromLocal8Bit("服务端启动成功"));
}
void LocalChartRoom::on_pushButton_2_clicked()
{
//client send msg
mClientsocket.write(ui->lineEdit->text().toLatin1());
mClientsocket.flush();
ui->lineEdit->clear();
}
void LocalChartRoom::on_pushButton_3_clicked()
{
//server send msg
if(!mServer.isListening())
return;
m_pServerSocket->write(ui->lineEdit_2->text().toLatin1());
m_pServerSocket->flush();
ui->lineEdit_2->clear();
}
void LocalChartRoom::on_pushButton_6_clicked()
{
//client close
mClientsocket.disconnectFromServer();
ui->textBrowser->append(QString::fromLocal8Bit("已经断开服务器连接..."));
}
void LocalChartRoom::on_pushButton_5_clicked()
{
//server close
mServer.close();
mServer.removeServer("ChartRoom");
ui->textBrowser_2->append(QString::fromLocal8Bit("服务器停止提供服务..."));
}
IV Summary
1. The socket is essentially the input and output devices, read and write operations supported NATURAL
2. Note that read, write, connect, disconnect call blocking class methods