Qt Tcp 客户端 /服务端通信实例
客户端 QTcpSocket
Client.pro
#-------------------------------------------------
#
# Project created by QtCreator 2016-03-23T21:15:18
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Client
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
QT +=network
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButto_conn_clicked();
private:
QTcpSocket * socket;
private slots:
void connected();
void on_pushButton_discon_clicked();
void readyread();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->socket=new QTcpSocket(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButto_conn_clicked()
{
this->socket->connectToHost("127.0.0.1",80100,QTcpSocket::ReadWrite);
connect(this->socket,SIGNAL(connected()),this,SLOT(connected()));
}
void MainWindow::connected()
{
QMessageBox::about(this,"提示","连接成功");
connect(this->socket,SIGNAL(readyRead()),this,SLOT(readyread()));
}
void MainWindow::on_pushButton_discon_clicked()
{
this->socket->close();
}
void MainWindow::readyread()
{
QMessageBox::about(this,"提示","准备读取");
QByteArray arr=this->socket->readAll();
QDataStream * dst=new QDataStream(&arr,QIODevice::ReadOnly);/******重点******/
QString str1;
QString str2;
(*dst)>>str1>>str2;
this->ui->textBrowser->setText(str1+str2);
QMessageBox::about(this,"x",str1+str2);
}
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
服务端通信实例
QTcpServer
Server.pro
#-------------------------------------------------
#
# Project created by QtCreator 2016-03-23T20:48:06
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Server
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
QT +=network
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QTcpServer * server;
QTcpSocket * socket;
private slots:
void newConnection();
void ReceiveData();
void on_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->socket=0;
this->server=new QTcpServer(this);
this->server->listen(QHostAddress::Any,80100);
QObject::connect(this->server,SIGNAL(newConnection()),this,SLOT(newConnection()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::newConnection(){
this->socket=this->server->nextPendingConnection();
QMessageBox::about(this,"提示","有新的连接!");
connect(this->socket,SIGNAL(readyRead()),this,SLOT(ReceiveData()));
}
void MainWindow::ReceiveData(){
QByteArray arr=this->socket->readAll();
QDataStream dst(arr);
QString str1;
QString str2;
dst>>str1>>str2;
this->ui->browser->setText(str1+str2);
}
void MainWindow::on_pushButton_2_clicked()
{
QString str=this->ui->lineEdit->text();
QByteArray arr;
QDataStream dst(&arr,QIODevice::ReadWrite);/*QDataStream是继承于QIODevice的所以 必须要 设置读写权限*/
dst<socket->write(arr);
}
void MainWindow::on_pushButton_clicked()
{
this->socket->close();
}
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}