#include "server.h"
#include "ui_server.h"
#include<QHostAddress>
#include<QHostInfo>
Server::Server(QWidget *parent) :
QWidget(parent),
ui(new Ui::Server)
{
ui->setupUi(this);
this->port=10086;
this->server=NULL;
ui->let_IP->setText(GetIpv4Address());
ui->let_Port->setText(QString::number(port));
ui->let_IP->setEnabled(false);
ui->let_Port->setEnabled(false);
ui->let_Name->setText("Server");
ui->btn_Start->setEnabled(false);
ui->btn_Stop->setEnabled(false);
ui->btn_Send->setEnabled(false);
connect(ui->btn_Listen,SIGNAL(clicked()),this,SLOT(slotListen()));
connect(ui->btn_Start,SIGNAL(clicked()),this,SLOT(slotStart()));
connect(ui->btn_Stop,SIGNAL(clicked()),this,SLOT(slotStop()));
}
Server::~Server()
{
emit ui->btn_Stop->click();
delete ui;
}
QString Server::GetIpv4Address()
{
QString localHostName = QHostInfo::localHostName();
QHostInfo info = QHostInfo::fromName(localHostName);
foreach(QHostAddress address,info.addresses())
if(address.protocol() == QAbstractSocket::IPv4Protocol)
return address.toString();
}
void Server::slotListen()
{
server=new Listener(port,this,ui->let_Name->text());
connect(this,SIGNAL(listening()),this,SLOT(slotListening()));
connect(server,SIGNAL(timeout(qint64)),this,SLOT(slotTimeout(qint64)));
emit listening();
}
void Server::slotConnected(QString name)
{
ui->lbl_Status->setText("You are speaking with "+name);
ui->btn_Start->setEnabled(true);
ui->btn_Stop->setEnabled(true);
ui->btn_Send->setEnabled(true);
ui->let_Name->setEnabled(false);
connect(ui->btn_Send,SIGNAL(clicked()),this,SLOT(slotSendWords()));
connect(this,SIGNAL(sendWords(QString)),server->client,SLOT(slotSendWords(QString)));
connect(this,SIGNAL(getWords(QString)),this,SLOT(slotGetWords(QString)));
}
void Server::slotListening()
{
ui->lbl_Status->setText("Listening...");
ui->btn_Start->setEnabled(false);
ui->btn_Stop->setEnabled(false);
ui->btn_Send->setEnabled(false);
}
void Server::slotStart()
{
int dev=1;
if (ui->rbn_Out->isChecked()==true)
dev=0;
server->Start(dev);
}
void Server::slotStop()
{
server->camThread->terminate();
server->camThread->wait();
server->camThread->stop();
}
void Server::slotDisconnected()
{
ui->lbl_Status->setText("No Connection");
ui->btn_Start->setEnabled(false);
ui->btn_Stop->setEnabled(false);
ui->btn_Send->setEnabled(false);
}
void Server::slotGetWords(QString words)
{
ui->lwt_Words->addItem(words);
}
void Server::slotSendWords()
{
emit sendWords(ui->let_Name->text()+":"+ui->let_Words->text());
emit getWords(ui->let_Name->text()+":"+ui->let_Words->text());
ui->let_Words->clear();
}
void Server::slotShowImage(QImage image)
{
ui->lbl_Image->setPixmap(QPixmap::fromImage(image));
}
void Server::slotTimeout(qint64 size)
{
ui->lbl_Speed->setText("Speed:"+QString::number((int)size/1024)+"kb/s");
size=0;
}
- 1
- 2
- 3
- 4
前往页