工程文件

QT       += network

服务端

#ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <QTcpServer>
#include <QTcpSocket> namespace Ui {
class Widget;
} class Widget : public QWidget
{
Q_OBJECT public:
explicit Widget(QWidget *parent = 0);
~Widget(); private:
Ui::Widget *ui;
QTcpServer *server;
QTcpSocket *socket; void init(); private slots:
void new_client();
void read_client_data();
void client_dis();
void show_error(QAbstractSocket::SocketError);
}; #endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <qDebug> Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this); init();
} Widget::~Widget()
{
delete ui;
} void Widget::init()
{
//初始化服务器server对象
server = new QTcpServer(this); //关联客户端连接信号newConnection
connect(server, SIGNAL(newConnection()), this, SLOT(new_client())); //连接客户端
//启动服务器监听
server->listen(QHostAddress::Any, 8888);
} void Widget::new_client()
{
qDebug() << "new_client here"; socket = server->nextPendingConnection();//与客户端通信的套接字 //关联接收客户端数据信号readyRead信号(客户端有数据就会发readyRead信号)
connect(socket, SIGNAL(readyRead()), this, SLOT(read_client_data()));
//检测掉线信号
connect(socket, SIGNAL(disconnected()), this, SLOT(client_dis()));
/* socket出错 -> 出错处理 */
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(show_error(QAbstractSocket::SocketError)));
} void Widget::read_client_data()
{
qDebug() << "read_client_data here"; QTcpSocket *obj = (QTcpSocket*)sender(); //可实现多连接
QString msg = obj->readAll();
qDebug() << msg;
} void Widget::show_error(QAbstractSocket::SocketError)
{
qDebug() << "show_error here";
qDebug() << socket->errorString(); socket->close();
} void Widget::client_dis()
{
qDebug() << "client_dis here"; socket->close();
}

客户端

#ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <QAbstractSocket>
#include <QTcpSocket> namespace Ui {
class Widget;
} class Widget : public QWidget
{
Q_OBJECT public:
explicit Widget(QWidget *parent = 0);
~Widget(); private slots:
void start_transfer();
void continue_transfer(qint64);
void show_error(QAbstractSocket::SocketError);
void stop_transfer(); private:
Ui::Widget *ui;
QTcpSocket *socket; void init();
}; #endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QHostAddress> Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this); init();
} Widget::~Widget()
{
delete ui;
} void Widget::init()
{
socket = new QTcpSocket(this); socket->connectToHost(QHostAddress::LocalHost, 8888); /* 连接已建立 -> 开始发数据 */
connect(socket, SIGNAL(connected()),
this, SLOT(start_transfer()));
/* 数据已发出 -> 继续发 */
connect(socket, SIGNAL(bytesWritten(qint64)),
this, SLOT(continue_transfer(qint64)));
/* socket出错 -> 错误处理 */
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(show_error(QAbstractSocket::SocketError)));
/* 检测掉线信号 */
connect(socket, SIGNAL(disconnected()), this, SLOT(stop_transfer()));
} void Widget::start_transfer()
{
qDebug() << "start_transfer here"; QString msg = "hello furong";
socket->write(msg.toUtf8());
} void Widget::continue_transfer(qint64 sentSize)
{
qDebug() << "continue_transfer sentSize" << sentSize; QString msg = "I love Y";
socket->write(msg.toUtf8()); socket->close();
} void Widget::show_error(QAbstractSocket::SocketError)
{
qDebug() << "show_error here"; socket->close();
} void Widget::stop_transfer()
{
qDebug() << "stop_transfer here";
}

最新文章

  1. hdu4059 The Boss on Mars(差分+容斥原理)
  2. 【SQLServer】DBHelper即C#数据库底层封装
  3. 虚拟机安装Centos7 , 没有可用的网络设备【ifconfig 只有lo而没有eth0的解决办法】
  4. 用Qt Creator 对 leveldb 进行简单的读写
  5. paramiko模块,线程,进程
  6. 12款最佳Linux命令行终端工具, 20款优秀的 Linux 终端仿真器
  7. hdu 1526(最大匹配)
  8. 问题集-- SQL 约束名不能重复
  9. hihocoder 1049 后序遍历
  10. puppet重申证书
  11. Google IP
  12. SpringBoot使用ELK日志收集
  13. centos 下部署django nginx+uwsgi
  14. (转)linux 内存管理——内核的shmall 和shmmax 参数
  15. 2019 front-end web trending
  16. Beta阶段——2
  17. FreeBSD利用 ports 來安裝軟體
  18. 转:PCIe基础知识
  19. IAR调试时出现IAR one or more breakpoints could not be set and have been disabled的解决办法
  20. gin实现spring boot url拦截器

热门文章

  1. Oracle告Google输了
  2. Python前言之Markdown使用
  3. AI AND THE BOTTOM LINE: 15 EXAMPLES OF ARTIFICIAL INTELLIGENCE IN FINANCE
  4. zz模型剪枝
  5. 洛谷 SP9722 CODESPTB - Insertion Sort
  6. pointnet++的pytorch实现
  7. B1005 继续(3n+1)猜想 (25 分)
  8. [LeetCode] 926. Flip String to Monotone Increasing 翻转字符串到单调递增
  9. win10安装docker并结合Idea2018.1部署springboot项目
  10. kali linux2019.4 设置中文字体教程