Results 1 to 2 of 2

Thread: QTcpServer nextPendingConnection() problem

  1. #1
    Join Date
    Nov 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTcpServer nextPendingConnection() problem

    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.

    Qt Code:
    1. #include "chatnetworkmanager.h"
    2.  
    3. ChatNetworkManager::ChatNetworkManager(QObject *parent, quint16 port, int maxPeer) : QTcpServer(parent)
    4. {
    5. setMaxPendingConnections(maxPeer);
    6. listen(QHostAddress::Any, port);
    7.  
    8. if(isListening())
    9. {
    10. QObject::connect(this, SIGNAL(newConnection()), this, SLOT(ClientConnection()));
    11. m_serverStatus = QString("The server is listening on port : ") + QString::number(serverPort());
    12. }
    13. else
    14. {
    15. m_serverStatus = errorString();
    16. }
    17. }
    18.  
    19. QString ChatNetworkManager::ServerStatus()
    20. {
    21. return m_serverStatus;
    22. }
    23.  
    24. void ChatNetworkManager::ClientConnection()
    25. {
    26. ChatNetworkServer *connection = new ChatNetworkServer(this);
    27. connection = qobject_cast<ChatNetworkServer *>(nextPendingConnection());
    28.  
    29. m_list.append(connection);
    30.  
    31. QObject::connect(connection, SIGNAL(disconnected()), this, SLOT(DisConnectedFromServer()));
    32. QObject::connect(connection, SIGNAL(DataRecieved(QString)), this, SLOT(SendToAll(QString)));
    33. }
    34.  
    35. void ChatNetworkManager::DisConnectedFromServer()
    36. {
    37. ChatNetworkServer *disconnection = qobject_cast<ChatNetworkServer *>(sender());
    38. if(disconnection == 0)
    39. {
    40. return;
    41. }
    42.  
    43. m_list.removeOne(disconnection);
    44. disconnection->deleteLater();
    45. }
    46.  
    47. void ChatNetworkManager::SendToAll(const QString &forward)
    48. {
    49. for(int i = 0; i < m_list.size(); i++)
    50. {
    51. m_list[i]->SendData(forward);
    52. }
    53. }
    To copy to clipboard, switch view to plain text mode 


    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.
    Last edited by teodori.serge; 6th November 2010 at 19:58.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTcpServer nextPendingConnection() problem

    1. Your naming sucks very hard!
    Do not call the server the manager and the client sockets the servers.
    If this doesn't confuse you, you're in a league of extraordinary gentlemen!

    2. What is the purpose of:
    Qt Code:
    1. ChatNetworkServer *connection = new ChatNetworkServer(this);
    2. connection = qobject_cast<ChatNetworkServer *>(nextPendingConnection());
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTcpServer QTcpSocket problem
    By jmsbc in forum Qt Programming
    Replies: 0
    Last Post: 20th November 2009, 17:42
  2. QTcpSocket, QTcpServer problem
    By frido in forum Qt Programming
    Replies: 3
    Last Post: 3rd April 2009, 23:16
  3. Qtcpserver problem
    By kingslee in forum Qt Programming
    Replies: 3
    Last Post: 3rd September 2008, 18:34
  4. Replies: 2
    Last Post: 23rd June 2007, 09:13
  5. Replies: 1
    Last Post: 18th June 2006, 10:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.