#include "tcpapp.h"
#include "ui_tcpapp.h"
TcpApp::TcpApp(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TcpApp)
{
ui->setupUi(this);
//设置窗口不可拉伸
this->setFixedSize(this->width(),this->height());
//设置图标
this->setWindowIcon(QIcon("./tool.jpg"));
//设置托盘图标
Icon.setIcon(QIcon("./tool.jpg"));
Icon.setToolTip("tcp小工具");
menu = new QMenu;
ac1 = new QAction("退出",this);
ac2 = new QAction("显示",this);
menu->addAction(ac1);
menu->addSeparator();
menu->addAction(ac2);
Icon.setContextMenu(menu);
Icon.show();
connect(ac1,SIGNAL(triggered(bool)),this,SLOT(exitWindow()));
connect(ac2,SIGNAL(triggered(bool)),this,SLOT(showWindow()));
//初始化定时器对象
mtimer = new QTimer;
//关联定时器信号
connect(mtimer,SIGNAL(timeout()),this,SLOT(on_send_clicked()));
online_num = 0;
recvsize = 0;
sendsize = 0;
isServer = false;
}
TcpApp::~TcpApp()
{
delete ui;
if(isServer)
delete mServer;
}
//退出窗口
void TcpApp::exitWindow()
{
this->close();
}
//显示窗口
void TcpApp::showWindow()
{
//this->show();
//this->showMaximized();
this->showNormal();
}
//服务器
void TcpApp::on_server_clicked(bool checked)
{
if(checked)
{
isServer = true;
ui->ipEdit->setEnabled(false); //设置ip选项无效
ui->start->setText("开启");
}
}
//客户端
void TcpApp::on_client_clicked(bool checked)
{
if(checked)
{
isServer = false;
ui->ipEdit->setEnabled(true); //设置ip选项有效
ui->start->setText("连接");
}
}
//开启服务器或连接服务器
void TcpApp::on_start_clicked()
{
if(isServer) //服务端
{
//创建QTcpServer对象,并初始化
mServer = new QTcpServer;
//设置监听listen
mServer->listen(QHostAddress::Any,ui->portEdit->text().toInt());
//等待连接,关联连接信号
connect(mServer,SIGNAL(newConnection()),this,SLOT(new_client()));
ui->start->setEnabled(false);
}
else //客户端
{
//创建QTcpSocket对象,并初始化
mSocket = new QTcpSocket;
//发起连接connectToHost
mSocket->connectToHost(ui->ipEdit->text(),ui->portEdit->text().toInt());
//关联连接成功信号
connect(mSocket,SIGNAL(connected()),this,SLOT(connect_suc()));
//关联读数据信号
connect(mSocket,SIGNAL(readyRead()),this,SLOT(read_data()));
ui->start->setEnabled(false);
}
}
//关闭服务端或客户端
void TcpApp::on_close_clicked()
{
if(isServer) //服务端
{
//关闭所有套接字
for(int i=0;i<clients.count();i++)
{
clients.at(i)->close();
}
//关闭连接
mServer->close();
ui->start->setEnabled(true);
//清零在线用户数
ui->online_num->setText("0");
//清空在线用户
ui->online_list->clear();
}
else //客户端
{
mSocket->close();//关闭套接字
ui->start->setEnabled(true);
}
}
//新客户端连接
void TcpApp::new_client()
{
mSocket = mServer->nextPendingConnection();
//向容器添加客户端套接字
clients.append(mSocket);
//读数据,关联读数据信号
connect(mSocket,SIGNAL(readyRead()),this,SLOT(read_data()));
//关联客户端下线信号
connect(mSocket,SIGNAL(disconnected()),this,SLOT(connect_dis()));
//显示在线用户数
ui->online_num->setText(QString::number(++online_num));
//获取客户端IP并显示
QString peerIP = mSocket->peerAddress().toString().remove("::ffff:");
ui->online_list->addItem(peerIP);
}
//客户端下线
void TcpApp::connect_dis()
{
//在线用户数减1
ui->online_num->setText(QString::number(--online_num));
QTcpSocket *obj = (QTcpSocket *)sender();
//找到下线用户在容器中的行号
int row = clients.indexOf(obj);
//从在线列表中删除下线用户的IP地址
ui->online_list->takeItem(row);
//删除容器中的套接字
clients.remove(row);
}
//读数据
void TcpApp::read_data()
{
//获取客户端IP
QTcpSocket *obj = (QTcpSocket *)sender();
QString msg = obj->readAll();
QString peerIP = obj->peerAddress().toString().remove("::ffff:");
ui->recvmsg->addItem(peerIP + ":" + msg);
//自动下滚
ui->recvmsg->setCurrentRow(ui->recvmsg->count() - 1);
//接收字节数
recvsize += msg.size();
ui->recv_size->setText(QString::number(recvsize));
}
//发送数据
void TcpApp::on_send_clicked()
{
QString msg = ui->sendmsg->toPlainText();
mSocket->write(msg.toUtf8());
//发送字节数
sendsize += msg.size();
ui->send_size->setText(QString::number(sendsize));
}
//连接成功
void TcpApp::connect_suc()
{
QMessageBox::information(this,"提示","连接成功");
}
//自动发送
void TcpApp::on_auto_send_clicked(bool checked)
{
QString auto_time = ui->auto_time->text();
if(checked) //选中自动发送
{
if(auto_time.isEmpty()) //间隔时间为空
{
QMessageBox::information(this,"提示","请输入间隔时间");
//设置为没有选中
ui->auto_send->setChecked(false);
return;
}
else //间隔时间不为空
{
mtimer->start(auto_time.toInt());//开启定时器
}
}
else //没选中自动发送
{
mtimer->stop(); //定时器关闭
}
}
//清空接收区
void TcpApp::on_clear_recv_clicked()
{
//清空字节数
recvsize = 0;
ui->recv_size->setText(QString::number(recvsize));
}
//清空发送区
void TcpApp::on_clear_send_clicked()
{
sendsize = 0;
ui->send_size->setText(QString::number(sendsize));
}
//单机鼠标,选择要发送的客户端套接字
void TcpApp::on_online_list_clicked(const QModelIndex &index)
{
mSocket = clients.at(index.row());
}