Results 1 to 4 of 4

Thread: Network Threaded server problem

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Network Threaded server problem

    Hi,

    I have a class "CServer" that inherits from "QTcpServer". When it recives an incoming connection it creates a new QThread that recives the socketDescriptor.

    Qt Code:
    1. void CServer::incomingConnection(int iSocketDescriptor)
    2. {
    3. CThread* pCThread = new CThread(this,iSocketDescriptor);
    4. bool bC = connect(pCThread,SIGNAL(finished()),pCThread,SLOT(deleteLater()));
    5. pCThread->start();
    6. }
    To copy to clipboard, switch view to plain text mode 

    The Thread creates a Socket into "run()" method:
    Qt Code:
    1. void CThread::run()
    2. {
    3. //Create the socket without parent
    4. m_pqSocket = new QTcpSocketThread();
    5. if (!m_pqSocket->setSocketDescriptor(m_iSocketDescriptor))
    6. {
    7. emit error(m_pqSocket->error());
    8. return;
    9. }
    10. //Enter the eventLoop
    11. exec();
    12.  
    13. //The client disconnects
    14. m_pqSocket->waitForDisconnected();
    15. delete (m_pqSocket);
    16. }
    To copy to clipboard, switch view to plain text mode 

    The clients are sending data to the client, so the server has a thread that is reciving the data from the client.

    One problem is that the, trying to connect 2 clients, the server creates 2 threads, but only the first thread that is responding tho the first client is responding. When the first client close the connection that makes the server thread to exit, then the second thread recives communication.
    Also, the second thread don't recives all the data.

    Am I doing something wrong on the code above?

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Network Threaded server problem

    Hi,

    Every Thread has a QTcpSocket, so why the socket of the first connection recives data through the socket and the second one don't recive any data?

    The Thread is doing a job that maybe consumes a lot of memory. Could this be a problem when the second connection tries to create the second thread?


    Thanks,
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Network Threaded server problem

    A simple question:
    Are you sure that the run() funciotn is called for second Thread?

    try this:

    Qt Code:
    1. void CThread::run()
    2. {
    3. qDebug("I'm Here");
    4.  
    5. //Create the socket without parent
    6. m_pqSocket = new QTcpSocketThread();
    7. ....
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    Another question:
    where do you set connections with m_pqSocket signals?
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Network Threaded server problem

    Hi,

    It's not a memory problem. I have changed the process code to "msleep" and the second Thread was locked.


    I've changed the way that I was doing it work and now it works well.
    Òscar Llarch i Galán

Similar Threads

  1. Threaded TCP server
    By Sysace in forum Newbie
    Replies: 4
    Last Post: 21st February 2008, 12:37
  2. Threaded server.
    By nithinin2001 in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2007, 16:37
  3. Problem getting an image from a web server
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 12th January 2007, 23:05

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.