#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QTcpSocket"
#include "QTcpServer"
#include "myserver.h"
#include "QNetworkInterface"
#include "QHostInfo"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);
setFixedSize(214,155);
ui->label->setText("Assistant is closed");
udpSocket=new QUdpSocket(this);
connect(udpSocket, SIGNAL(readyRead()),this,SLOT(on_upd_received()));
udpSocket->bind(5678);
}
MainWindow::~MainWindow()
{
server.close();
udpSocket->close();
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
if(started==0){
server.StartServer();
started=1;
ui->pushButton->setText("CLOSE");
ui->label->setText("Assistant is running");
}else{
server.close();
started=0;
ui->pushButton->setText("OPEN");
ui->label->setText("Assistant is closed");
}
}
void MainWindow::on_upd_received()
{
while(udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(),datagram.size());
QString msg=datagram.data();
qDebug()<1){
if(!(request.at(0).compare("requestip"))&&started==1){
QString requstIp=request.at(1);
qDebug()<<"ip: "<connectToHost(requstIp,1234);
answerSocket->write(myName.append(",").append(myIp).toUtf8());
answerSocket->close();
}
}
}
}
QString MainWindow::getHostIp(){
QString str="";
QList netList = QNetworkInterface::allAddresses();
for(int i=0;i