#include <QtGui>
#include <QtNetwork>
#include "server.h"
Server
::Server(QWidget *parent, Qt
::WFlags flags
){
ui.setupUi(this);
ui.statusLabel->setText(tr("Server is not running"));
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(doMagic()));
connect(ui.quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(ui.imageButton, SIGNAL(clicked()), this, SLOT(openImage()));
connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));
}
Server::~Server()
{
}
void Server::doMagic(void)
{
ui.textEdit->append("Blaaa");
}
void Server::startServer(void)
{
if (!tcpServer->isListening()) {
QMessageBox::critical(this, tr
("Server"), tr
("Unable to start server: %1.").
arg(tcpServer
->errorString
()));
close();
return;
}
ui.startstopButton->setText(tr("Stop Server"));
ui.statusLabel->setText(tr("The server is running on port %1.\n").arg(tcpServer->serverPort()));
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendTestString()));
disconnect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));
connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(stopServer()));
}
void Server::stopServer(void)
{
if(tcpServer->isListening()){
tcpServer->close();
}
ui.statusLabel->setText(tr("Server stopped!"));
disconnect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(stopServer()));
connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));
ui.startstopButton->setText(tr("Start Server"));
}
void Server::readClient(void)
{
ui.clientLabel->setText(tr("readClient()"));
}
void Server::sendTestString(void)
{
//ui.clientLabel->setText(tr("String sent"));
out << (quint16)0;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readClient()));
clientConnection->write(block);
clientConnection->disconnectFromHost();
}
void Server::openImage(void)
{
tr
("Open File"),
QDir::currentPath());
if (!fileName.isEmpty()) {
if (image.isNull()) {
tr("Cannot load %1.").arg(fileName));
return;
}
}
}
#include <QtGui>
#include <QtNetwork>
#include "server.h"
Server::Server(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
ui.statusLabel->setText(tr("Server is not running"));
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(doMagic()));
connect(ui.quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(ui.imageButton, SIGNAL(clicked()), this, SLOT(openImage()));
connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));
}
Server::~Server()
{
}
void Server::doMagic(void)
{
ui.textEdit->append("Blaaa");
}
void Server::startServer(void)
{
tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any,8001);
if (!tcpServer->isListening()) {
QMessageBox::critical(this, tr("Server"), tr("Unable to start server: %1.").arg(tcpServer->errorString()));
close();
return;
}
ui.startstopButton->setText(tr("Stop Server"));
ui.statusLabel->setText(tr("The server is running on port %1.\n").arg(tcpServer->serverPort()));
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendTestString()));
disconnect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));
connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(stopServer()));
}
void Server::stopServer(void)
{
if(tcpServer->isListening()){
tcpServer->close();
}
ui.statusLabel->setText(tr("Server stopped!"));
disconnect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(stopServer()));
connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));
ui.startstopButton->setText(tr("Start Server"));
}
void Server::readClient(void)
{
ui.clientLabel->setText(tr("readClient()"));
}
void Server::sendTestString(void)
{
//ui.clientLabel->setText(tr("String sent"));
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << QString("Test").toUtf8();
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readClient()));
clientConnection->write(block);
clientConnection->disconnectFromHost();
}
void Server::openImage(void)
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty()) {
QImage image(fileName);
if (image.isNull()) {
QMessageBox::information(this, tr("Image Viewer"),
tr("Cannot load %1.").arg(fileName));
return;
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks