#include "chatnetworkmanager.h"
ChatNetworkManager
::ChatNetworkManager(QObject *parent, quint16 port,
int maxPeer
) : QTcpServer(parent
){
setMaxPendingConnections(maxPeer);
if(isListening())
{
QObject::connect(this,
SIGNAL(newConnection
()),
this,
SLOT(ClientConnection
()));
m_serverStatus
= QString("The server is listening on port : ") + QString::number(serverPort
());
}
else
{
m_serverStatus = errorString();
}
}
QString ChatNetworkManager
::ServerStatus() {
return m_serverStatus;
}
void ChatNetworkManager::ClientConnection()
{
ChatNetworkServer *connection = new ChatNetworkServer(this);
connection = qobject_cast<ChatNetworkServer *>(nextPendingConnection());
m_list.append(connection);
QObject::connect(connection,
SIGNAL(disconnected
()),
this,
SLOT(DisConnectedFromServer
()));
}
void ChatNetworkManager::DisConnectedFromServer()
{
ChatNetworkServer *disconnection = qobject_cast<ChatNetworkServer *>(sender());
if(disconnection == 0)
{
return;
}
m_list.removeOne(disconnection);
disconnection->deleteLater();
}
void ChatNetworkManager
::SendToAll(const QString &forward
) {
for(int i = 0; i < m_list.size(); i++)
{
m_list[i]->SendData(forward);
}
}
#include "chatnetworkmanager.h"
ChatNetworkManager::ChatNetworkManager(QObject *parent, quint16 port, int maxPeer) : QTcpServer(parent)
{
setMaxPendingConnections(maxPeer);
listen(QHostAddress::Any, port);
if(isListening())
{
QObject::connect(this, SIGNAL(newConnection()), this, SLOT(ClientConnection()));
m_serverStatus = QString("The server is listening on port : ") + QString::number(serverPort());
}
else
{
m_serverStatus = errorString();
}
}
QString ChatNetworkManager::ServerStatus()
{
return m_serverStatus;
}
void ChatNetworkManager::ClientConnection()
{
ChatNetworkServer *connection = new ChatNetworkServer(this);
connection = qobject_cast<ChatNetworkServer *>(nextPendingConnection());
m_list.append(connection);
QObject::connect(connection, SIGNAL(disconnected()), this, SLOT(DisConnectedFromServer()));
QObject::connect(connection, SIGNAL(DataRecieved(QString)), this, SLOT(SendToAll(QString)));
}
void ChatNetworkManager::DisConnectedFromServer()
{
ChatNetworkServer *disconnection = qobject_cast<ChatNetworkServer *>(sender());
if(disconnection == 0)
{
return;
}
m_list.removeOne(disconnection);
disconnection->deleteLater();
}
void ChatNetworkManager::SendToAll(const QString &forward)
{
for(int i = 0; i < m_list.size(); i++)
{
m_list[i]->SendData(forward);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks