Qt5 TcpSocket 客户端/服务器 通讯实例
2017-02-21 22:21
369 查看
Qt Tcp 客户端 /服务端通信实例
客户端 QTcpSocket
[html] view
plain copy
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
[html] view
plain copy
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QTcpSocket>
#include<QString>
#include<QDataStream>
#include<QByteArray>
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
[html] view
plain copy
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QHostAddress>
#include<QMessageBox>
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
[html] view
plain copy
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
服务端通信实例
QTcpServer
Server.pro
[html] view
plain copy
#-------------------------------------------------
#
# 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
[html] view
plain copy
#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();
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
[html] view
plain copy
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QTcpServer>
#include<QTcpSocket>
#include<QMessageBox>
#include<QDataStream>
#include<QString>
#include<QByteArray>
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<<QString("信息")<<str;/*这里一定要设置好数据类型*/
this->socket->write(arr);
}
void MainWindow::on_pushButton_clicked()
{
this->socket->close();
}
main.cpp
[html] view
plain copy
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
客户端 QTcpSocket
[html] view
plain copy
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
[html] view
plain copy
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QTcpSocket>
#include<QString>
#include<QDataStream>
#include<QByteArray>
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
[html] view
plain copy
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QHostAddress>
#include<QMessageBox>
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
[html] view
plain copy
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
服务端通信实例
QTcpServer
Server.pro
[html] view
plain copy
#-------------------------------------------------
#
# 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
[html] view
plain copy
#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();
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
[html] view
plain copy
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QTcpServer>
#include<QTcpSocket>
#include<QMessageBox>
#include<QDataStream>
#include<QString>
#include<QByteArray>
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<<QString("信息")<<str;/*这里一定要设置好数据类型*/
this->socket->write(arr);
}
void MainWindow::on_pushButton_clicked()
{
this->socket->close();
}
main.cpp
[html] view
plain copy
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
相关文章推荐
- Qt5 TcpSocket 客户端/服务器 通讯实例
- Qt网络程序:基于TCP的服务器、客户端实例
- Linux Socket编程:基于TCP/IP的客户端与服务端通讯实例
- QTcpSocket 通讯 ( 服务器、客户端、封包、解包 )
- 基于Qt-socketio客户端与Nodejs服务器通讯
- tcp通讯的方式并使用socket建立客户端与服务器的通信的过程
- 简单的TCP 流式socket 服务器和客户端代码示例
- QTcpSocket类 客户端/服务器模式
- java 通过 socket 实现 服务器和客户端的通信 TCP
- 详解 QT 多线程 TCP 文件接收服务器实例(2)
- Android客户端与PC服务器通过socket进行交互实例
- 客户端服务器基于Socket的UDP和TCP编程介绍
- Winsocket入门教程二:非阻塞式服务器和客户端程序(TCP)
- C# 异步TCP Socket聊天室(1服务器,N客户端)
- 详解 QT 多线程 TCP 文件接收服务器实例(1)
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- socket 服务器浏览器与服务器客户端实例
- 某实例+修改 Winsock实现基于TCP的客户端/服务器通信
- C# 异步TCP Socket聊天室(1服务器,N客户端)
- Android基于客户端和服务器的Socket编程例子之Socket基础通讯--socket相关知识