最近产品要做一个PC端与PAD端通信收发信息的功能。研究了一下Qt的实现。看到一个很好的帖子,简单易懂。仿照写了一个例子,记录下来备忘。
- 客户端
头文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void connectServer();
void slotReadMessage();
void slotSendMessage();
private:
Ui::MainWindow *ui;
QTcpSocket *m_tcpSocket;
};
#endif // MAINWINDOW_H
源文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_tcpSocket(0)
{
ui->setupUi(this);
ui->IpLineEdit->setText("192.168.1.179");
connect(ui->connectBtn, SIGNAL(clicked()), this, SLOT(connectServer()));
connect(ui->sendMsgBtn, SIGNAL(clicked()), this, SLOT(slotSendMessage()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connectServer()
{
m_tcpSocket = new QTcpSocket(this);
m_tcpSocket->abort();
QString ip = ui->IpLineEdit->text().trimmed();
qDebug()<<"ip="<<ip;
m_tcpSocket->connectToHost(ip, 19999);
// 当 QTcpSocket 上有新数据可读时,则会触发信号 readyRead(), 读取信息
connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(slotReadMessage()));
}
void MainWindow::slotReadMessage() // 读取信息
{
QByteArray contents = m_tcpSocket->readAll();
ui->receiveMsgTextEdit->clear();
ui->receiveMsgTextEdit->setText(QString(contents));
}
void MainWindow::slotSendMessage() // 发送信息
{
QString msg = ui->sendMsgLineEdit->text();
qint64 ret = m_tcpSocket->write(msg.toStdString().c_str(), strlen(msg.toStdString().c_str()));
if (ret < 0)
{
qDebug()<<"failed. error!";
return;
}
ui->sendMsgLineEdit->clear();
}
main函数:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
服务端
头文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void startTcpserver();
void slotCreateNewConnect();
void slotReadMessage();
void slotSendMessage();
private:
Ui::MainWindow *ui;
QTcpServer *m_tcpServer;
QTcpSocket *m_tcpSocket;
};
#endif // MAINWINDOW_H
源文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->startBtn, SIGNAL(clicked()), this, SLOT(startTcpserver()));
connect(ui->sendMsgBtn, SIGNAL(clicked()), this, SLOT(slotSendMessage()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::startTcpserver()
{
m_tcpServer = new QTcpServer(this);
// 监听任何连上19999端口的ip
m_tcpServer->listen(QHostAddress::Any, 19999);
// 新连接信号触发,调用 newConnect()槽函数,这个跟信号函数一样,其实你可以随便取。
connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(slotCreateNewConnect()));
}
void MainWindow::slotCreateNewConnect()
{
m_tcpSocket = m_tcpServer->nextPendingConnection(); // 得到每个连进来的socket
// 有可读的信息,触发读信息的槽函数
connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(slotReadMessage()));
}
void MainWindow::slotReadMessage() // 读取信息
{
QByteArray contents = m_tcpSocket->readAll(); // 读取
ui->textEdit_rec->clear();
ui->textEdit_rec->setText(QString(contents));
}
void MainWindow::slotSendMessage() // 发送信息
{
QString msg = ui->lineEdit_sendmessage->text();
m_tcpSocket->write(msg.toStdString().c_str(), strlen(msg.toStdString().c_str())); // 发送
}
main函数:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}