Results 1 to 7 of 7

Thread: Connect to a server, send data and exit

  1. #1
    Join Date
    Mar 2007
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Connect to a server, send data and exit

    I want to make a console application that just connect to a server (using QTcpSocket), read some lines from it, send some data and exit.

    This has to be done synchronously in just one function (at the beginning the connection is opened, at the end all data should have been sent and the connection is closed).
    Can this be done?

    I'm trying to do it, but I think I lose data sometimes. Also I have problems reading the data from server.

    The code is more or less this way:

    Qt Code:
    1. QTcpSocket * socket = new QTcpSocket(this);
    2. QTextStream * ts = new QTextStream(socket);
    3.  
    4. socket->connectToHost( QHostAddress::LocalHost, port, QIODevice::ReadWrite);
    5. if (!socket->waitForConnected()) return false;
    6.  
    7. socket->waitForReadyRead();
    8.  
    9. if (socket->canReadLine()) {
    10. line = ts->readLine();
    11. }
    12.  
    13. // parse line
    14.  
    15. (*ts) << "Write some data\n";
    16. (*ts) << "More data\n";
    17.  
    18. ts->flush();
    19. socket->flush();
    20. socket->waitForBytesWritten();
    21.  
    22. socket->close();
    23. socket->disconnectFromHost();
    24. socket->waitForDisconnected();
    To copy to clipboard, switch view to plain text mode 

    Is this correct?

    It seems that sometimes the server doesn't get all lines.
    Is it necessary to use a QTextStream?
    Both QTextStream and QTcpSocket have a flush function, which one should I use?

    I'm having problems reading the text from the server. It seems that I only get the first line. What's the right way to read from the server?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect to a server, send data and exit

    Quote Originally Posted by Pepe View Post
    Is this correct?
    Not quite. What if data arrives in two parts? You need a while loop that that will call waitForReadyRead() and readAll() until you read all of the data. You will have detect yourself if you read all of the data, to break that loop.

    Quote Originally Posted by Pepe View Post
    It seems that I only get the first line.
    You get only one line, because you call readLine() once. You simply don't read the rest.

    Quote Originally Posted by Pepe View Post
    Is it necessary to use a QTextStream?
    No.

    Quote Originally Posted by Pepe View Post
    Both QTextStream and QTcpSocket have a flush function, which one should I use?
    You don't need the one from QTcpSocket. QTcpSocket::disconnectFromHost() should handle the unsent data. Also you should remove "socket->close()", because it's just an equivalent of disconnectFromHost().

    PS. Why do you create both text stream and the socket on the heap, if you need them only in one function?

  3. #3
    Join Date
    Mar 2007
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect to a server, send data and exit

    Quote Originally Posted by jacek View Post
    Not quite. What if data arrives in two parts? You need a while loop that that will call waitForReadyRead() and readAll() until you read all of the data. You will have detect yourself if you read all of the data, to break that loop.
    Mmm, that would make things harder. It would be nice if there were a waitForReadyReadLine().

    Quote Originally Posted by jacek View Post
    You get only one line, because you call readLine() once. You simply don't read the rest.
    In this example I only put only a readLine(), but I tried to read more lines and there were problems. My intention was to read a line from the server, send a command to it, read the response and if it everything is ok, send next command and so on.

    Anyway, even if I'm not reading properly the data from the server, I don't understand why when I try to read the 2nd line my data is not sent to the server. I send 4 lines, only the 1st one arrives. Why isn't my data sent?

    Quote Originally Posted by jacek View Post
    PS. Why do you create both text stream and the socket on the heap, if you need them only in one function?
    I read here, in this forum, that it's better to create QTcpSocket objects with new. Anyway, actually my code is not in only one function (it was at the beginning).

  4. #4
    Join Date
    Mar 2007
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect to a server, send data and exit

    It seems that the problem was in QTextStream::readLine().

    It first received two complete lines in the buffer, but QTextStream::readLine() read the first the line and the rest was discarded. I used instead QTcpSocket::readLine() and the problem has gone, now I can read from the server without problems.

    Now I'm wondering what's the advantage of using a QTextStream over a QTcpSocket...

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect to a server, send data and exit

    Quote Originally Posted by Pepe View Post
    I read here, in this forum, that it's better to create QTcpSocket objects with new.
    Where? If you need an object only within one function, the easiest approach is to allocate it on the stack. This way you won't forget to deallocate it.

    Quote Originally Posted by Pepe View Post
    Anyway, actually my code is not in only one function (it was at the beginning).
    In that case, it's OK, but if you create a new socket for each connection, remember to delete it when you don't need it (also in case of errors), so it won't occupy memory needlessly.

    Quote Originally Posted by Pepe View Post
    Now I'm wondering what's the advantage of using a QTextStream over a QTcpSocket...
    It's good when you want to send data, but reading is a bit tricky, because data arrives in fragments.

  6. #6
    Join Date
    Mar 2007
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect to a server, send data and exit

    Quote Originally Posted by jacek View Post
    Where? If you need an object only within one function, the easiest approach is to allocate it on the stack. This way you won't forget to deallocate it.
    I think I read it in this thread.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect to a server, send data and exit

    Quote Originally Posted by Pepe View Post
    I think I read it in this thread.
    In that thread the same QTcpSocket was used in several methods, while I understood that you want to use it only in one method and then get rid of it. In such situation it is better to create objects on the stack, because you don't have to take care to delete them.

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.