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
{
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
class Client : public QObject
{
Q_OBJECT
public:
Client(QObject* parent = 0);
~Client();
void start(QString address, quint16 port);
// void Read(QString filename);
public slots:
void readData();
void sendData();
private:
QTcpSocket client;
QString line;
char *c_str2;
int LENGTH;
};
#endif // CLIENT_H
To copy to clipboard, switch view to plain text mode
client.cpp
{
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();
}
}
Client::Client(QObject* parent): QObject(parent)
{
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)
{
QHostAddress addr(address);
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);
QFile mfile(mfilename);
//if(!mfile.open(QFile::ReadOnly | QFile::Text))
if(mfile.open(QIODevice::ReadOnly | QIODevice::Text))
{
// qDebug() << "Could Not Open File For Reading!";
// return;
//}
QTextStream in(&mfile);
//QByteArray ba;
while (!in.atEnd()) {
line = in.readLine();
QByteArray ba = line.toLocal8Bit();
c_str2 = ba.data();
client.write(c_str2);
sleep(1);
qDebug() << line;
}
mfile.close();
}
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QtCore/QCoreApplication>
#include <client.h>
int main(int argc, char *argv[])
{
Client client;
client.start("112.165.1.2", 7350);
return a.exec();
}
#include <QtCore/QCoreApplication>
#include <client.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Client client;
client.start("112.165.1.2", 7350);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks