Results 1 to 6 of 6

Thread: QTcpSocekt dosent recive msg

  1. #1
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default QTcpSocekt dosent recive msg

    Hi

    I have very strange problem, QTcpSocket recive msg when i write something like that
    Qt Code:
    1. tcpSocket->write("P\n");
    To copy to clipboard, switch view to plain text mode 
    but if i write something like that
    Qt Code:
    1. tcpSocket->write("P"+t.toLatin1());
    To copy to clipboard, switch view to plain text mode 
    it dosent work
    where t is QString

    Sending function(server)
    Qt Code:
    1. void server::Twyslijdane()
    2. {
    3. QSettings settings("Users.ini",QSettings::IniFormat);
    4. keys = settings.allKeys();
    5. for (int i = 0; i < keys.size(); i = i+2) {
    6. t = keys.at(i);
    7. t.remove(QString("/Login"), Qt::CaseInsensitive);
    8. t.remove(QString("/pass"), Qt::CaseInsensitive);
    9.  
    10.  
    11. connection->write("D"+t.toLatin1());
    12.  
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    reciving function
    Qt Code:
    1. {
    2. qint64 bytes = buffer->write(tcpSocket->readAll());
    3. // go back as many bytes as we just wrote so that it can be read
    4. buffer->seek(buffer->pos() - bytes);
    5. // read only full lines, line by line
    6. while (buffer->canReadLine())
    7. {
    8. line = buffer->readLine();
    9. if(line[0] == 'D'){
    10. line.remove(0,1);
    11. dodajnicki(line);
    12. }
    13.  
    14. }
    15.  
    16.  
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    When i m sending tcpSocket->write("P"+t.toLatin1()); program dosent use
    this part of code
    Qt Code:
    1. while (buffer->canReadLine())
    2. {
    3. line = buffer->readLine();
    4. if(line[0] == 'D'){
    5. line.remove(0,1);
    6. dodajnicki(line);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Best Regards

  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: QTcpSocekt dosent recive msg

    What happens if you send "P" + t.toLatin1() + "\n"?

  3. The following user says thank you to jacek for this useful post:

    Zergi (16th January 2008)

  4. #3
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocekt dosent recive msg

    Qt Code:
    1. tcpSocket->write("P\n"); // "P\n" is a const char *
    2. tcpSocket->write("P"+t.toLatin1()); // "P" + t.toLatin1() is a QString
    To copy to clipboard, switch view to plain text mode 
    Does QIODevice::canReadLine() expect to read char* data? If so, you can use
    Qt Code:
    1. tcpSocket->write(QString("P" + t).toLatin1().data());
    To copy to clipboard, switch view to plain text mode 

  5. #4
    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: QTcpSocekt dosent recive msg

    Quote Originally Posted by bender86 View Post
    "P" + t.toLatin1() is a QString
    That's a QByteArray, not QString.

  6. #5
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocekt dosent recive msg

    May be:

    Qt Code:
    1. QByteArray block;
    2. QDataStream out(&block, QIODevice::WriteOnly);
    3. out.setVersion(QDataStream::Qt_4_0);
    4.  
    5. block = NULL;
    6. block.append("Some Msg");
    7. tcpSocket->write(block);
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocekt dosent recive msg

    Quote Originally Posted by jacek View Post
    That's a QByteArray, not QString.
    Right. So the problem is not that. Try how suggested by jacek.

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.