#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowIcon(QIcon(":/image/14g.png")); //设置图标
SetStyle(":/qss/blue.css"); //设置样式表
setWindowTitle("TCP网络调试助手");
comboBox_config();
timer = new QTimer(this); //创建定时器
connect(timer, SIGNAL(timeout()), this, SLOT(update())); //关联槽函数
/*控件名字设置*/
ui->checkBox_ShowTime->setText("接收时间显示");
ui->checkBox_TxBit->setText("自动发送附加位\\r\\n");
ui->checkBox_LoopTx->setText("循环发送数据");
ui->label_4->setText("设置循环间隔ms时间:");
ui->pushButton_connect->setText("连接");
ui->pushButton_SendData->setText("发送");
ui->About->setText("帮助");
ui->label->setText("1.协议类型");
ui->label_ip->setText("2.本地IP地址");
ui->label_prot->setText("3.本地端口");
ui->label_SendCnt->setText("TX");
ui->label_RxCnt->setText("RX");
ui->pushButton_clear->setText("复位");
ui->plainTextEdit_ShowData->setEnabled(false); //不可编辑
ui->pushButton_cleanShow->setText("清除显示");
}
//设置指定样式
void Widget::SetStyle(const QString &qssFile) {
QFile file(qssFile);
if (file.open(QFile::ReadOnly)) {
QString qss = QLatin1String(file.readAll());
qApp->setStyleSheet(qss);
QString PaletteColor = qss.mid(20, 7);
qApp->setPalette(QPalette(QColor(PaletteColor)));
file.close();
}
}
//基本配置
void Widget::comboBox_config()
{
/*第一部分:网络设置*/
//1.1 配置协议
ui->comboBox_protocol->addItem("TCP Server");
ui->comboBox_protocol->addItem("TCP Client");
//1.2. 获取并配置本地IP地址
QList<QHostAddress> list = QNetworkInterface::allAddresses();
for(int i=0;i<list.count();i++)
{
QHostAddress addr=list.at(i);
if(addr.protocol() == QAbstractSocket::IPv4Protocol)
{
ui->comboBox_ipaddress->addItem(addr.toString());
}
}
//1.3 设置可编辑性
ui->comboBox_ipaddress->setEditable(true);
//1.4. 设置默认端口号
ui->lineEdit_prot->setText("8080");
/*第二部分:发送设置*/
//2.1设置循环发送时间
ui->lineEdi_LoopTime->setText("1000");
//2.2 设置默认发送的内容
ui->lineEdit_InputTxDtat->setText("www.wanbangee.com");
//接收数量置0
TxCount=0;
RxCount=0;
/*第三部分:选择客户端时用于显示本地端口和IP*/
ClientShowLocalProtLabel =new QLabel("本地端口:");
ClientShowLocalIptLabel =new QLabel("本地IP地址:");
ClientEditProt= new QLineEdit;
ClientEditIP= new QLineEdit;
ClientLayout = new QHBoxLayout;
ClientLayout->addWidget(ClientShowLocalIptLabel);
ClientLayout->addWidget(ClientEditIP);
ClientLayout->addWidget(ClientShowLocalProtLabel);
ClientLayout->addWidget(ClientEditProt);
/*第四部分:选服务器时用于显示已经连接的客户端*/
SaverShowLocalProtLabel =new QLabel("已连接的客户端列表:");
ClientComboBoxList=new QComboBox;
ClientComboBoxList->setMinimumWidth(180);
ServerLayout = new QHBoxLayout;
ServerLayout->addWidget(SaverShowLocalProtLabel);
ServerLayout->addWidget(ClientComboBoxList);
ServerLayout->addStretch();
}
Widget::~Widget()
{
timer->stop(); //停止定时器
delete timer;
delete ui;
}
//创建服务器或者连接到服务器
void Widget::on_pushButton_connect_clicked()
{
if(ui->pushButton_connect->text()=="连接")
{
switch(ui->comboBox_protocol->currentIndex())
{
case 0: //服务器模式
NewServer();
break;
case 1: //客户端模式
NewClinet();
break;
}
}else
{
switch(ui->comboBox_protocol->currentIndex())
{
case 0: //服务器模式
ui->pushButton_connect->setText("连接");
for(int i=0;i<TcpFarClientList.count();i++)
{
TcpFarClientList.at(i)->close();
TcpFarClientList.removeAt(i);
}
LocalTcpServer->close();//关闭服务器
//取消客户端列表显示
ui->verticalLayout_9->removeItem(ServerLayout);
ClientComboBoxList->close();
SaverShowLocalProtLabel->close();
//断开服务器之后设置控件可用性
//创建服务器之后设置控件可用
ui->comboBox_ipaddress->setEnabled(true);
ui->comboBox_protocol->setEnabled(true);
ui->lineEdit_prot->setEnabled(true);
break;
case 1: //客户端模式
LocalTcpClientSocket->close();
break;
}
}
}
//服务器模式:创建服务器
void Widget::NewServer()
{
/*1. 实例化服务器*/
LocalTcpServer= new QTcpServer;
/*2. 设置监听的端口和IP地址*/
quint16 port=QString(ui->lineEdit_prot->text()).toInt();
if(ui->comboBox_ipaddress->currentText()=="QHostAddress::Any")
{
LocalTcpServer->listen(QHostAddress::Any,port);
}else
{
QHostAddress addr(ui->comboBox_ipaddress->currentText());
LocalTcpServer->listen(addr,port);
}
/*3. 关联连接信号,检测是否有新的客户端连接*/
connect(LocalTcpServer,SIGNAL(newConnection()),this,SLOT(NewTcpConnection()));
ui->pushButton_connect->setText("断开连接");
//添加布局,显示已经连接的客户端列表
ui->verticalLayout_9->insertLayout(1,ServerLayout);
ClientComboBoxList->show();
SaverShowLocalProtLabel->show();
//创建服务器之后设置控件可用
ui->comboBox_ipaddress->setEnabled(false);
ui->comboBox_protocol->setEnabled(false);
ui->lineEdit_prot->setEnabled(false);
}
//客户端模式:创建客户端
void Widget::NewClinet()
{
/*1. 创建本地客户端TCP套接字*/
LocalTcpClientSocket = new QTcpSocket;
/*2. 设置服务器IP地址*/
QString Ipaddr=ui->comboBox_ipaddress->currentText();
QHostAddress FarServerAddr(Ipaddr);
/*3. 连接客户端的信号槽*/
connect(LocalTcpClientSocket,SIGNAL(connected()),this,SLOT(LocalTcpClientConnectedSlot()));
connect(LocalTcpClientSocket,SIGNAL(disconnected()),this,SLOT(LocalTcpClientDisconnectedSlot()));
connect(LocalTcpClientSocket,SIGNAL(readyRead()),this,SLOT(LocalTcpClientReadDtatSlot()));
/*4. 尝试连接服务器主机*/
int prot=ui->lineEdit_prot->text().toInt();
LocalTcpClientSocket->connectToHost(FarServerAddr,prot);
}
//客户端模式:响应连接上服务器之后的操作
void Widget::LocalTcpClientConnectedSlot()
{
//显示本地端口和IP
ClientEditProt->setText(QString::number(LocalTcpClientSocket->localPort()));
ClientEditIP->setText(LocalTcpClientSocket->localAddress().toString());
ui->verticalLayout_9->insertLayout(1,ClientLayout);
ClientEditProt->show();
ClientEditIP->show();
ClientShowLocalProtLabel->show();
ClientShowLocalIptLabel->show();
//当连接上服务器之后设置控件不可用
ui->comboBox_ipaddress->setEnabled(false);
ui->comboBox_protocol->setEnabled(false);
ui->lineEdit_prot->setEnabled(false);
ui->pushButton_connect->setText("断开连接");
}
//客户端模式:断开服务器
void Widget::LocalTcpClientDisconnectedSlot()
{
ui->verticalLayout_9->removeWidget(ClientEditProt);
ui->verticalLayout_9->removeWidget(ClientEditIP);
ui->verticalLayout_9-
没有合适的资源?快使用搜索试试~ 我知道了~
TCP调试助手源码(QT编写的).zip
共38个文件
png:18个
css:8个
qrc:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 5 下载量 11 浏览量
2021-05-25
09:39:20
上传
评论 3
收藏 10.07MB ZIP 举报
温馨提示
使用QT编写的Android手机APP。本软件支持TCP协议网络调试,可以创建TCP客户端与TCP服务器。适合电子工程师、嵌入式工程作为网络调试的工具。服务器支持多连接,支持显示已经连接的客户端信息,支持循环发送。应用程序固定为横屏显示。
资源推荐
资源详情
资源评论
收起资源包目录
TCP调试助手_CSDN.zip (38个子文件)
TCP调试助手_CSDN
widget.ui 7KB
images
qss
darkgray.css 9KB
silvery.css 9KB
brown.css 9KB
lightgray.css 9KB
black.css 9KB
gray.css 9KB
dev.css 9KB
blue.css 9KB
images.qrc 1KB
image
add_bottom.png 3KB
radio_selected.png 1024B
msg_error.png 2KB
Font Awesome Cheatsheet.png 581KB
qt.ico 143KB
checkbox_checked.png 691B
checkbox_unchecked.png 581B
btn_close.png 6KB
msg_question.png 4KB
14g.png 22KB
msg_info.png 3KB
add_left.png 148B
add_top.png 3KB
add_right.png 151B
qt_zh_CN.qm 115KB
radio_normal.png 888B
btn_ok.png 10KB
fontawesome-webfont.ttf 138KB
android
image.qrc 1KB
AndroidManifest.xml 6KB
res
drawable-ldpi
icon.png 247KB
drawable-hdpi
icon.png 248KB
drawable-mdpi
icon.png 247KB
main.cpp 491B
socket.pro 557B
网络调试助手_1.2.apk 8.59MB
widget.cpp 16KB
widget.h 2KB
共 38 条
- 1
资源评论
- youyou06082023-01-24资源很好用,有较大的参考价值,资源不错,支持一下。
- qq_26086942832021-09-29用户下载后在一定时间内未进行评价,系统默认好评。
- nyykji2022-01-31用户下载后在一定时间内未进行评价,系统默认好评。
- lxh4134587272023-08-03这个资源值得下载,资源内容详细全面,与描述一致,受益匪浅。
- fanfanll19842021-12-01用户下载后在一定时间内未进行评价,系统默认好评。
DS小龙哥
- 粉丝: 4w+
- 资源: 521
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功