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
Code:
{
Q_OBJECT
public:
~Client();
void start
(QString address, quint16 port
);
// void Read(QString filename);
public slots:
void readData();
void sendData();
private:
char *c_str2;
int LENGTH;
};
#endif // CLIENT_H
client.cpp
Code:
{
connect(&client, SIGNAL(connected()),this, SLOT(readData()),Qt::DirectConnection);
connect(&client, SIGNAL(connected()),this, SLOT(sendData()),Qt::DirectConnection);
LENGTH =8;
}
Client::~Client()
{
client.close();
}
void Client
::start(QString address, quint16 port
) {
client.connectToHost(addr, port);
}
void Client::readData()
{
QString mfilename
="/home/client2/Desktop/akde.csv";
//Read(mfilename);
// QByteArray ba = line.toLocal8Bit();
// c_str2 = ba.data();
// client.write(c_str2,5000);
//if(!mfile.open(QFile::ReadOnly | QFile::Text))
{
// qDebug() << "Could Not Open File For Reading!";
// return;
//}
//QByteArray ba;
while (!in.atEnd()) {
line = in.readLine();
c_str2 = ba.data();
client.write(c_str2);
sleep(1);
qDebug() << line;
}
mfile.close();
}
}
main.cpp
Code:
#include <QtCore/QCoreApplication>
#include <client.h>
int main(int argc, char *argv[])
{
Client client;
client.start("112.165.1.2", 7350);
return a.exec();
}
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.
Re: client sending data line by line to server in Qt
Have your any suggestion regarding this???
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.