Qt环境:Qt4
先上代码
Client: mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLocalSocket>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButtoLinkServer_clicked();
void OnReadyRead();
void on_pushButtonSendMsg_clicked();
private:
Ui::MainWindow *ui;
QLocalSocket * m_pSocketReceive;
QString m_serverName;
};
#endif // MAINWINDOW_H
mainwindow.cpp :
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_pSocketReceive(NULL),
m_serverName("ServerName")
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButtoLinkServer_clicked()
{
if(NULL == m_pSocketReceive)
{
m_pSocketReceive = new QLocalSocket(this);
m_pSocketReceive->connectToServer(m_serverName);
// connect(m_pSocketReceive,SIGNAL(connected()),this,SLOT(OnClientConnectSuccess()));
connect(m_pSocketReceive,SIGNAL(readyRead()),this,SLOT(OnReadyRead()));
}
}
void MainWindow::OnReadyRead()
{
if(m_pSocketReceive)
{
QDataStream tempReadDataStream(m_pSocketReceive);
tempReadDataStream.setVersion(QDataStream::Qt_4_8);