teodori.serge
6th November 2010, 19:58
Hello,
I need to pass a socket structure to an another object.
On the attached file you see a part of my program.
The problem is in the methode "void ChatNetworkManager::ClientConnection()",
ChatNetworkManager is a TCP Server (inherits : public QTcpServer)
ChatNetworkServer is a TCP Socket (inherits : public QTcpSocket)
nextPendingConnection() returns a QTcpSocket, but I have an object "ChatNetworkServer" that inherits QTcpSocket,
so I tried with "qobject_cast<>()", but the SIGNALs & SLOTs aren't working (QObject::connect : Cannot connect (null)::)
after that I tried various things like "setSocketDescriptor", or "setPeerAddress", etc...
those things work but sometimes I get errors (events lost, SEGMENTATION FAULTS, etc...)
is there a possibility like m_chatNetworkServer->copySocket(nextPendingConnection())
Thank you.
#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);
}
}
Added after 5 minutes:
I cannot use "QTcpSocket" instead of "ChatNetworkServer", because the "ChatNetworkServer" class is not only a tcp socket but also SQL connection and has lots of functions inside (would be to hard to handle)
The "ChatNetworkManager" class is much greater than that, this is just a part of the class.
:) any help would be nice.
I need to pass a socket structure to an another object.
On the attached file you see a part of my program.
The problem is in the methode "void ChatNetworkManager::ClientConnection()",
ChatNetworkManager is a TCP Server (inherits : public QTcpServer)
ChatNetworkServer is a TCP Socket (inherits : public QTcpSocket)
nextPendingConnection() returns a QTcpSocket, but I have an object "ChatNetworkServer" that inherits QTcpSocket,
so I tried with "qobject_cast<>()", but the SIGNALs & SLOTs aren't working (QObject::connect : Cannot connect (null)::)
after that I tried various things like "setSocketDescriptor", or "setPeerAddress", etc...
those things work but sometimes I get errors (events lost, SEGMENTATION FAULTS, etc...)
is there a possibility like m_chatNetworkServer->copySocket(nextPendingConnection())
Thank you.
#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);
}
}
Added after 5 minutes:
I cannot use "QTcpSocket" instead of "ChatNetworkServer", because the "ChatNetworkServer" class is not only a tcp socket but also SQL connection and has lots of functions inside (would be to hard to handle)
The "ChatNetworkManager" class is much greater than that, this is just a part of the class.
:) any help would be nice.