#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
#include<QMessageBox>
#include<windows.h>
#include<QTime>
#include<QTimer>
#include<QHostAddress>
#include <QNetworkInterface>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QHostAddress ip = get_local_host_ip();//获取本机的ip地址
QString port = "8999";//端口设置为8888
//全局时间
QTimer *time;
time=new QTimer(this);
time->start(1000);
connect(time,SIGNAL(timeout()),this,SLOT(currentTime()));
ui->server_ip->setText(ip.toString());
ui->server_port->setText(port);
ui->state->appendPlainText("["+currenttime+"]"+":等待客户端连接...");
//
tcp_socket=new Tcp_Socket(ip,port);
connect(tcp_socket,&Tcp_Socket::signal_msg,this,&MainWindow::print_msg);
connect(this, &MainWindow::destroyed, this, &MainWindow::close_socket); //线程跟随窗口退出
tcp_socket->start();
tcp_socket->stop();
ui->btn_send->setEnabled(false);
ui->btn_clear->setEnabled(false);
ui->text_send->setEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::currentTime()
{
currenttime=QTime::currentTime().toString();
}
QHostAddress MainWindow::get_local_host_ip()
{
QList<QHostAddress> AddressList = QNetworkInterface::allAddresses();
QHostAddress result;
foreach(QHostAddress address, AddressList)
{
if(address.protocol() == QAbstractSocket::IPv4Protocol &&
address != QHostAddress::Null &&
address != QHostAddress::LocalHost)
{
if (address.toString().contains("127.0.")){
continue;
}
if(address.toString().contains("192.168.")){
result = address;
break;
}
}
}
return result;
}
//显示通信数据
void MainWindow::print_msg()
{
if(!tcp_socket->msg_queue.isEmpty())
{
ui->plainTextEdit->appendPlainText("["+currenttime+"]"+"client:"+tcp_socket->msg_queue.dequeue());
if(!tcp_socket->connect_flag)
{
on_btn_listen_clicked();
ui->state->appendPlainText("["+currenttime+"]"+"server:等待客户端连接...");
}
}
}
//关闭线程
void MainWindow::close_socket()
{
tcp_socket->go_on();
tcp_socket->start_flag = false; //停止线程
tcp_socket->quit();
tcp_socket->wait(); //等待线程处理完手头工作
if(tcp_socket->connect_flag)
{
tcp_socket->socket->abort();
tcp_socket->server->close();
}
}
//开启监听
void MainWindow::on_btn_listen_clicked()
{
if(ui->btn_listen->text().toUtf8() == "监听")
{
if(tcp_socket->connect_flag)
{
ui->btn_listen->setText("关闭");
tcp_socket->go_on();
tcp_socket->recv_flag = true;
model=true;
ui->btn_send->setEnabled(true);
ui->btn_clear->setEnabled(true);
ui->text_send->setEnabled(true);
}
else
{
ui->plainTextEdit->appendPlainText("["+currenttime+"]"+"server:等待客户端连接........");
}
}
else if(ui->btn_listen->text().toUtf8() == "关闭")
{
ui->btn_listen->setText("监听");
tcp_socket->recv_flag = false;
tcp_socket->stop();
ui->plainTextEdit->clear();
}
}
//发送
void MainWindow::on_btn_send_clicked()
{
QString msg = ui->text_send->text();
tcp_socket->socket->write(msg.toUtf8());
tcp_socket->socket->flush();
ui->plainTextEdit->appendPlainText("["+currenttime+"]"+"server:"+msg);
}
//清空记录
void MainWindow::on_btn_clear_clicked()
{
ui->plainTextEdit->clear();
ui->text_send->clear();
}
评论1