#ifndef SERVER_H
#define SERVER_H
#include <QDialog>
{
Q_OBJECT
public:
private slots:
void sendWelcomeMessage();
private:
};
#endif
#ifndef SERVER_H
#define SERVER_H
#include <QDialog>
class QTcpServer;
class QLabel;
class QPushButton;
class QTextEdit;
class Server : public QDialog
{
Q_OBJECT
public:
Server(QWidget* parent = 0);
private slots:
void sendWelcomeMessage();
private:
QLabel* status;
QPushButton* quit;
QTcpServer* server;
QTextEdit* read;
};
#endif
To copy to clipboard, switch view to plain text mode
server.cpp
#include <QtGui>
#include <QtNetwork>
#include "server.h"
{
connect(quit, SIGNAL(clicked()), this, SLOT(close()));
read->setReadOnly(true);
if(!server->listen(addr, 12345)) {
QMessageBox::information(this, tr
("Error!!"), tr
("The following errors has occured: %1").
arg(server
->errorString
()));
return;
}
status->setText(tr("The server is running on %1 port.").arg(server->serverPort()));
connect(server, SIGNAL(newConnection()), this, SLOT(sendWelcomeMessage()));
layout->addWidget(status);
layout->addWidget(quit);
layout->addWidget(read);
setLayout(layout);
}
void Server::sendWelcomeMessage()
{
typedef QVector<QTcpSocket*> Connections;
Connections connections;
QTcpSocket *client
= server
->nextPendingConnection
();
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
client->write("Welcome!");
char buffer[100];
sprintf(buffer, "Incoming connection from IP: %s", client->peerAddress().toString().toStdString().c_str());
read->append(buffer);
connections.push_back(client);
for(Connections::Iterator it = connections.begin(); it != connections.end(); ++it)
(*it)->write("Hello!!!");
}
#include <QtGui>
#include <QtNetwork>
#include "server.h"
Server::Server(QWidget* parent)
: QDialog(parent)
{
status = new QLabel;
quit = new QPushButton(tr("&Close"));
connect(quit, SIGNAL(clicked()), this, SLOT(close()));
server = new QTcpServer(this);
read = new QTextEdit(tr("Status:\n"));
read->setReadOnly(true);
QHostAddress addr("127.0.0.1");
if(!server->listen(addr, 12345)) {
QMessageBox::information(this, tr("Error!!"), tr("The following errors has occured: %1").arg(server->errorString()));
return;
}
status->setText(tr("The server is running on %1 port.").arg(server->serverPort()));
connect(server, SIGNAL(newConnection()), this, SLOT(sendWelcomeMessage()));
QHBoxLayout* layout = new QHBoxLayout;
layout->addWidget(status);
layout->addWidget(quit);
layout->addWidget(read);
setLayout(layout);
}
void Server::sendWelcomeMessage()
{
typedef QVector<QTcpSocket*> Connections;
Connections connections;
QTcpSocket *client = server->nextPendingConnection();
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
client->write("Welcome!");
char buffer[100];
sprintf(buffer, "Incoming connection from IP: %s", client->peerAddress().toString().toStdString().c_str());
read->append(buffer);
connections.push_back(client);
for(Connections::Iterator it = connections.begin(); it != connections.end(); ++it)
(*it)->write("Hello!!!");
}
To copy to clipboard, switch view to plain text mode
guess no need for main.cpp.
Bookmarks