Results 1 to 3 of 3

Thread: QTcpServer with threaded clients problem

  1. #1
    Join Date
    Sep 2011
    Posts
    1

    Default QTcpServer with threaded clients problem

    Hi.

    I've writing TcpServer app in Qt and client in avr32 processor, but i got problem with server. My server should be multi-threaded, each of client on other thread. My problem is:

    When I've connected newConnection() signal with this slot:
    Qt Code:
    1. void CTcpServer::incomingConnection(int socketDescriptor)
    2. {
    3. //IF there are still empty slots
    4. if(numOfClients < MAX_CLIENTS)
    5. {
    6. //Check which one is empty
    7. for(int i=0; i<MAX_CLIENTS; i++)
    8. {
    9. //Empty slot found
    10. if( 0 == clients[i])
    11. {
    12. //got socket
    13. clients[i] = new CTargetThread(socketDescriptor,this,socket);
    14. connect(clients[i], SIGNAL(finished()), clients[i], SLOT(deleteLater()));
    15. clients[i]->start();
    16. //Client counter indicate how much clients are connected to server
    17. numOfClients++;
    18. break;
    19. }
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    it simply check is a client slot aviable and runs thread for connected client.
    and there is CTargetThreads run method:

    Qt Code:
    1. void CTargetThread::run()
    2. {
    3. socket=new QTcpSocket();
    4. if (!socket->setSocketDescriptor(iSocketDescriptor)) {
    5. //emit error(tcpSocket.error());
    6. qDebug("setSocketDescriptor failed");
    7. return;
    8. }
    9. qDebug("Socket granted");
    10. while(1)
    11. {
    12. socket->write("Sample message");
    13. msleep(50);
    14. }
    15. socket->disconnectFromHost();
    16. socket->waitForDisconnected();
    17. }
    To copy to clipboard, switch view to plain text mode 

    And problem is:
    my packet with "sample message" doesn't seems to be send, I use WireShark to look up for packets, and it does not shows that packet.

    Thread is created normally, setSocketDescriptor returns false, so it success, and when i write something, it just not working.

    When I write something on client(avr32) side, it can't be readed in thread, but i see in WireShark, that this packet have been sent.

    please, help me

  2. #2
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer with threaded clients problem

    How you reading? Try: socket->write("Sample message\n");

  3. #3
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpServer with threaded clients problem

    Quote Originally Posted by Witek View Post
    Thread is created normally, setSocketDescriptor returns false, so it success, and when i write something, it just not working.
    From the Qt doc :
    bool QAbstractSocket::setSocketDescriptor ( int socketDescriptor, SocketState socketState = ConnectedState, OpenMode openMode = ReadWrite )

    Initializes QAbstractSocket with the native socket descriptor socketDescriptor. Returns true if socketDescriptor is accepted as a valid socket descriptor; otherwise returns false.
    Regards,
    Marc

Similar Threads

  1. Replies: 4
    Last Post: 30th November 2010, 22:09
  2. Replies: 7
    Last Post: 10th May 2010, 12:26
  3. Extend QTcpServer to handle multiple clients
    By DiamonDogX in forum Qt Programming
    Replies: 5
    Last Post: 24th February 2010, 20:49
  4. Replies: 4
    Last Post: 23rd May 2008, 10:42
  5. Threaded Sever Problem
    By Moppel in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2006, 23:03

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.