Results 1 to 2 of 2

Thread: Reading data from socket

  1. #1
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Reading data from socket

    I've been trying repeatly and rewritting it over billions of times it seems.

    But when I'm connecting to a irc server with my QTcpSocket. Some lines are malformed. where it is split to a new line.

    So I thought I could just do it to where check if the line starts without a ':' append it to the previous line. But apparently that causes some lines to be mixed together and what not. And I can't find hardly any resources on doing it correctly.

    Qt Code:
    1. void
    2. IrcSocket::readSocket() {
    3. if(!m_ircSocket->canReadLine()) {
    4. return;
    5. }
    6.  
    7. QTextStream stream(m_ircSocket);
    8. QString data;
    9.  
    10. do {
    11. data = stream.readLine();
    12.  
    13. if(data[0] != ':') {
    14. QStringList words = data.split(" ");
    15.  
    16. if(words.count() >= 2) {
    17. if(words[0] == "NOTICE" && words[1] == "AUTH") {
    18. QRegExp reg("^NOTICE AUTH :(.+)$");
    19.  
    20. if(reg.exactMatch(data)) {
    21. emit noticeAuth(reg.cap(1));
    22. }
    23. } else {
    24. data.append(stream.readLine());
    25. }
    26. } else {
    27. data.append(stream.readLine());
    28. }
    29. } else {
    30. QStringList words = data.split(" ");
    31.  
    32. switch(words[1].toUInt()) {
    33. case 001: {
    34. QRegExp reg("^[^ ]+ 001 [^ ]+ :(.+)$", Qt::CaseInsensitive);
    35.  
    36. if(reg.exactMatch(data)) {
    37. emit welcome(reg.cap(1));
    38. } else {
    39. data.append(stream.readLine());
    40. }
    41. }
    42. case ReplyCode::RPL_MOTDSTART: {
    43. QRegExp reg("^[^ ]+ 375 [^ ]+ :(.+)$", Qt::CaseInsensitive);
    44.  
    45. if(reg.exactMatch(data)) {
    46. emit motdStart(reg.cap(1));
    47. } else {
    48. data.append(stream.readLine());
    49. }
    50. break;
    51. }
    52. case ReplyCode::RPL_MOTD: {
    53. QRegExp reg("^[^ ]+ 327 [^ ]+ :(.+)$", Qt::CaseInsensitive);
    54. kDebug() << data;
    55. if(reg.exactMatch(data)) {
    56. kDebug() << "TEST: " << reg.cap(1);
    57. data.clear();
    58. } else {
    59. data.append(stream.readLine());
    60. }
    61. break;
    62. }
    63. default: {
    64. data.append(stream.readLine());
    65. break;
    66. }
    67. }
    68. }
    69.  
    70. kDebug() << data;
    71. } while(!stream.atEnd());
    72. }
    To copy to clipboard, switch view to plain text mode 
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Reading data from socket

    How about reading the data to a buffer whenever new data comes available, and reading complete lines from the buffer?
    J-P Nurmi

Similar Threads

  1. Replies: 7
    Last Post: 29th August 2008, 10:24
  2. Socket Reading Advice
    By tntcoda in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2008, 11:26
  3. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  4. problem with reading input data in qt
    By Ahmad in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2007, 10:58
  5. Reading from TCP Socket crashes program
    By OnionRingOfDoom in forum Qt Programming
    Replies: 26
    Last Post: 27th January 2006, 19:32

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.