Qt TCP通信
工程文件
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";
}
Qt TCP通信的更多相关文章
- qt tcp 通信实例
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QHostAddress> ...
- QT实现TCP通信服务器端和客户端(支持多个客户端)精简版
上星期接了个私活,工期两星期,报酬3000,写一个小软件,采集定向网络上的数据,并进行双向通信,捣鼓了两天,终于把QT中tcp通信这块调通了,找过N多例子,绝大部分都是基本的一个服务端一个客户端通信的 ...
- 套接字、UDP通信、TCP通信、TCP/IP协议簇
一.套接字(socket) 1.英语单词socket:n.插座:穴:v.插入插座 2.套接字就是源IP地址和目的IP地址.源端口号和目的端口号的组合,是通过传输层进行通信的.IP指定电脑,端口指定某一 ...
- TCP通信
//网络套接字编程实例,服务器端,TCP通信. #include <WinSock2.h> #pragma comment(lib,"ws2_32.lib") #inc ...
- 清晰易懂TCP通信原理解析(附demo、简易TCP通信库源码、解决沾包问题等)C#版
目录 说明 TCP与UDP通信的特点 TCP中的沾包现象 自定义应用层协议 TCPLibrary通信库介绍 Demo演示 未完成功能 源码下载 说明 我前面博客中有多篇文章讲到了.NET中的网络编程, ...
- JAVASE02-Unit010: 多线程基础 、 TCP通信
多线程基础 . TCP通信 * 当一个方法被synchronized修饰后,那么 * 该方法称为同步方法,即:多个线程不能同时 * 进入到方法内部执行. package day10; /** * 当多 ...
- 艺萌文件上传下载及自动更新系统(基于networkComms开源TCP通信框架)
1.艺萌文件上传下载及自动更新系统,基于Winform技术,采用CS架构,开发工具为vs2010,.net2.0版本(可以很容易升级为3.5和4.0版本)开发语言c#. 本系统主要帮助客户学习基于TC ...
- NetworkComms V3 使用TCP通信传递IList<T>类型的数据
客户端从服务器获取一组IList<T>类型的数据非常常见(通常从数据库中获取) 我们用NeworkComms V3来演示一下(NetworkcommsV2.x版本也同样支持) [ 使用pr ...
- [C语言]一个很实用的服务端和客户端进行TCP通信的实例
本文给出一个很实用的服务端和客户端进行TCP通信的小例子.具体实现上非常简单,只是平时编写类似程序,具体步骤经常忘记,还要总是查,暂且将其记下来,方便以后参考. (1)客户端程序,编写一个文件clie ...
随机推荐
- 20180706模拟赛T3——神经衰弱
文件名: card 题目类型: 传统题 时间限制: 1秒 内存限制: 128MB 编译优化: 无 题目描述 天然少女小雪非常喜欢玩一个叫做神经衰弱的游戏. 游戏规则是,有若干种牌,每种牌有若干对,开始 ...
- python-下载网页链接
from urllib.request import urlretrieve url = 'http://images.jupiterimages.com/common/detail/27/68/22 ...
- 切换node版本
首先将原来的安装包删了,在控制面板中删除然后在https://nodejs.org/dist/找到想要的版本号 再找到msi文件
- 字符串s倒序输出
编程将字符串s倒序输出,要求利用函数递归实现. 输入格式要求:"%s" 提示信息:"input your string:\n" 输出格式要求:"%c& ...
- IntelliJ idea 创建Web项目后web文件夹下没有WEB-INF的解决方法
1.Ctrl+Shift+Alt+S快捷键进入Project structure(项目结构)管理的界面 2.选择左边菜单栏里的Facet,点击后能看到有Deployment Descriptors的输 ...
- Linux性能优化实战学习笔记:第三十一讲
一.上节回顾 上一节,我们一起回顾了常见的文件系统和磁盘 I/O 性能指标,梳理了核心的 I/O 性能观测工具,最后还总结了快速分析 I/O 性能问题的思路. 虽然 I/O 的性能指标很多,相应的性能 ...
- [LeetCode] 718. Maximum Length of Repeated Subarray 最长的重复子数组
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- oracle --工具 ODU
一,什么是ODU ODU全称为Oracle Data ba se Unloader ,是用于Oracle 数据库紧急恢复的软件,在各种原因 造成的数据库不能打开或数据删除后没有备份时,使用ODU抢救数 ...
- linux内核动态调试技术
动态调试功能就是你可以决定在程序运行过程中是否要 pr_debug(), dev_dbg(), print_hex_dump_debug(), print_hex_dump_bytes() 这些函数正 ...
- Vue官方文档笔记
1.如何创建一个Vue实例对象? var vm = new Vue({ el: "#app", //标签id 或 标签类名 data:{ //双向绑定的数据 message: &q ...