Results 1 to 9 of 9

Thread: Problems with QTcpServer

  1. #1
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry Problems with QTcpServer

    I am trying to create an XML based protocol, using Qt4, and I am having problems. The problem is that only 2*n+1 connection from the client gets into the server. The client is ok (I think), since I recoded it in C# and found the same problem.

    I am not sure why the server is "eating" some of the XML sent to it. Can anyone give me a hint? To debug this I used "nc -l" and I see that the client does send the correct data. When I use "nc" to emulate the client, I see that the server is not processing the datastream. The correct method is invoked but the XML is not read.

    Qt Code:
    1. /////////////////////////////////////
    2. // client code, parts of it at least
    3. /////////////////////////////////////
    4. MyClient::MyClient()
    5. {
    6. connection = new QTcpSocket(this);
    7. }
    8.  
    9. void MyClient::connect( QString host, int port )
    10. {
    11. connection->connectToHost(host,port);
    12. if (!connection->waitForConnected()){
    13. qDebug("Aborting, no connection");
    14. }
    15. }
    16.  
    17. void MyClient::sendConnectionString(QString s)
    18. {
    19. QXmlStreamWriter w(connection);
    20. // send some XML
    21. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. /////////////////////////////////////
    2. // this is the server
    3. /////////////////////////////////////
    4. MyServer::MyServer(QObject *parent): QObject(parent)
    5. {
    6. qDebug("Server started");
    7. server = new QTcpServer(this);
    8. connect(server, SIGNAL(newConnection()), this, SLOT(on_newConnection()));
    9. }
    10.  
    11. void MyServer::start()
    12. {
    13. server->listen(QHostAddress::Any,2008);
    14. qDebug("Started server");
    15. }
    16.  
    17. void MyServer::on_newConnection()
    18. {
    19. QTcpSocket *s = server->nextPendingConnection();
    20. connect(s,SIGNAL(readyRead()), this, SLOT(on_bytesArrived()));
    21. connect(s,SIGNAL(disconnected()), this, SLOT(on_connectionClosed()));
    22. qDebug("New connection from %s:%d", qPrintable(s->peerAddress().toString()), s->peerPort() );
    23. }
    24.  
    25. void MyServer::on_connectionClosed()
    26. {
    27. QTcpSocket *s = qobject_cast<QTcpSocket*>(sender());
    28. if ( s == NULL)
    29. return;
    30. qDebug("Connection from %s:%d closed", qPrintable(s->peerAddress().toString()), s->peerPort() );
    31. }
    32.  
    33. void MyServer::on_bytesArrived()
    34. {
    35. qDebug("1111111"); // I do see this
    36. QTcpSocket *s = qobject_cast<QTcpSocket*>(sender());
    37. if ( s == NULL) {
    38. qDebug("No socket");
    39. return;
    40. }
    41.  
    42. d.setContent(s);
    43. // I don't see this following line
    44. qDebug("read\n%s", qPrintable(d.toString(4)));
    45. }
    To copy to clipboard, switch view to plain text mode 

    PS: I do know I need to fix the code to use QSignalMapper. This will be done as soon as I find the problem here.

    EDIT:
    I see this thread, but I don't learn anything about my problem... Am I close...?
    http://www.qtcentre.org/threads/1991...Server-problem
    http://lists.trolltech.com/qt-intere...ad01624-0.html
    Last edited by elcuco; 13th March 2011 at 21:47. Reason: split the two classes into different tags, link to other threads

  2. #2
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: Problems with QTcpServer

    I think your app receive more than one packet. So you should read all received data and than create QDomDocument.
    Try read Qt documentation before ask stupid question.

  3. #3
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QTcpServer

    I know that. I another project I read a long, which means the amount of bytes to read, then I those bytes into the DomDocument.

    This time I want to do it the right way, maybe using QXmlStreamReader. The problem - I *do* need the DOM. How can I do this?

  4. #4
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: Problems with QTcpServer

    Read all and create QDomDocument or create class (that use or subclass QXmlStreamReader), that read data and create xml

    Or find other solution
    Try read Qt documentation before ask stupid question.

  5. #5
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QTcpServer

    Changing the code to

    Qt Code:
    1. QString str = s->readAll();
    2. d.setContent(str);
    To copy to clipboard, switch view to plain text mode 

    Does seem to fix the problem, but for some reason I am not happy about it.

  6. #6
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: Problems with QTcpServer

    QDomDocument can read from IODEVICE. But problem that QTcpSocket is stream device, so he should read all data (many readready signals), and after that he create QDomDocument
    Try read Qt documentation before ask stupid question.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with QTcpServer

    Read (append) all data to an additional buffer and try creating the dom tree out of that. Once QDomDocument::setContent() is successful, you know you received the full DOM tree. Then you just have to check how much of the data to discard from the buffer (there can be more data waiting in the buffer).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QTcpServer

    How can I do this? by reading one byte at a time?
    What happens if I read too much (and I have for example one full XML and part of the next one?)

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with QTcpServer

    Quote Originally Posted by elcuco View Post
    How can I do this? by reading one byte at a time?
    It's up to you to find a reasonable algorithm. You could try detecting the closure of the tag that was the first tag in the document.

    What happens if I read too much (and I have for example one full XML and part of the next one?)
    No idea. It will either work or fail, fake some small piece of xml and find out yourself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QTcpServer Problem
    By ionutdanila in forum Newbie
    Replies: 2
    Last Post: 27th December 2010, 12:18
  2. QTcpServer and Symbian
    By ManuMies in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 8th November 2010, 09:11
  3. QTcpServer and GDB
    By baray98 in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2009, 08:02
  4. QTcpServer + Authorization
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2008, 14:04
  5. Replies: 1
    Last Post: 18th June 2006, 10:12

Tags for this Thread

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.