Results 1 to 4 of 4

Thread: client sending data line by line to server in Qt

  1. #1
    Join Date
    Jul 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default client sending data line by line to server in Qt

    Hi,
    I have a client as well as a server.I want to send .csv file to server line by line.I can do read the file line by line and write to the socket.But the problem is that server not receiving data line by line.it accept data after total reading of client's file.
    I want client read a LINE and send it to server and server read that line.Again client read next line and send to server.
    Any help???????
    My client code like this....

    client.h
    Qt Code:
    1. class Client : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. Client(QObject* parent = 0);
    6. ~Client();
    7. void start(QString address, quint16 port);
    8. // void Read(QString filename);
    9.  
    10. public slots:
    11. void readData();
    12. void sendData();
    13. private:
    14. QTcpSocket client;
    15. QString line;
    16. char *c_str2;
    17.  
    18. int LENGTH;
    19. };
    20.  
    21.  
    22. #endif // CLIENT_H
    To copy to clipboard, switch view to plain text mode 

    client.cpp
    Qt Code:
    1. Client::Client(QObject* parent): QObject(parent)
    2. {
    3. connect(&client, SIGNAL(connected()),this, SLOT(readData()),Qt::DirectConnection);
    4. connect(&client, SIGNAL(connected()),this, SLOT(sendData()),Qt::DirectConnection);
    5. LENGTH =8;
    6.  
    7. }
    8.  
    9. Client::~Client()
    10. {
    11. client.close();
    12. }
    13.  
    14. void Client::start(QString address, quint16 port)
    15. {
    16. QHostAddress addr(address);
    17. client.connectToHost(addr, port);
    18. }
    19. void Client::readData()
    20. {
    21. QString mfilename ="/home/client2/Desktop/akde.csv";
    22.  
    23. //Read(mfilename);
    24. // QByteArray ba = line.toLocal8Bit();
    25. // c_str2 = ba.data();
    26. // client.write(c_str2,5000);
    27. QFile mfile(mfilename);
    28.  
    29. //if(!mfile.open(QFile::ReadOnly | QFile::Text))
    30. if(mfile.open(QIODevice::ReadOnly | QIODevice::Text))
    31. {
    32. // qDebug() << "Could Not Open File For Reading!";
    33. // return;
    34. //}
    35. QTextStream in(&mfile);
    36.  
    37. //QByteArray ba;
    38. while (!in.atEnd()) {
    39.  
    40. line = in.readLine();
    41. QByteArray ba = line.toLocal8Bit();
    42. c_str2 = ba.data();
    43. client.write(c_str2);
    44. sleep(1);
    45.  
    46.  
    47. qDebug() << line;
    48.  
    49. }
    50.  
    51. mfile.close();
    52. }
    53. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <client.h>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. Client client;
    8. client.start("112.165.1.2", 7350);
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: client sending data line by line to server in Qt

    Qt networking is asynchronous: writing to a socket is non-blocking, and nothing is sent until your program reaches the Qt event loop. Your while() loop, with its misguided sleep() call, does not allow execution to reach the Qt event loop some time after the entire loop is completed. You need to design your program differently. There are plenty of examples in the docs and these forums.

  3. #3
    Join Date
    Jul 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: client sending data line by line to server in Qt

    Have your any suggestion regarding this???

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: client sending data line by line to server in Qt

    If by "suggestion" you mean "code I can copy and paste" then no. I have already suggested that you need to consult the docs/examples and rethink your design. I do not propose to do this for you. You will get help if you show how you are trying to rework it and ask specific questions.

Similar Threads

  1. server not getting client data
    By raj_iv in forum Qt Programming
    Replies: 0
    Last Post: 31st May 2011, 08:30
  2. QTcpSocket get disconnected when server is sending huge data
    By KernelCoder in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2011, 08:45
  3. Replies: 3
    Last Post: 13th August 2010, 11:50
  4. Sending string from QT client to Java server
    By seanmu13 in forum Qt Programming
    Replies: 10
    Last Post: 3rd July 2007, 12:52
  5. Sending string from QT client to Java server
    By seanmu13 in forum Newbie
    Replies: 3
    Last Post: 3rd July 2007, 12:20

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.