Well solved the problem.. going by the traditional way of reading chunks of data and writing to it.
Here's the full code:
Server Code:
----------------------
Server.cpp
#include "server.h"
#include <QtNetwork>
{
//connect(&server, SIGNAL(newConnection()),this, SLOT(acceptConnection()));
qDebug() << serverAddress() << serverPort() << serverError();
}
Server::~Server()
{
close();
}
void Server::incomingConnection(int socketDescriptor)
{
qDebug() << "File transfer started";
SendThread *thread = new SendThread(socketDescriptor,this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
qDebug() << "Thread called";
}
#include "server.h"
#include <QtNetwork>
Server::Server(QObject *parent) :
QTcpServer(parent)
{
//connect(&server, SIGNAL(newConnection()),this, SLOT(acceptConnection()));
listen(QHostAddress::Any, 8888);
qDebug() << serverAddress() << serverPort() << serverError();
}
Server::~Server()
{
close();
}
void Server::incomingConnection(int socketDescriptor)
{
qDebug() << "File transfer started";
SendThread *thread = new SendThread(socketDescriptor,this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
qDebug() << "Thread called";
}
To copy to clipboard, switch view to plain text mode
server.h
#ifndef SERVER_H
#define SERVER_H
#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include "sendthread.h"
{
Q_OBJECT
public:
explicit Server
(QObject *parent
= 0);
void ServerListener();
~Server();
protected:
void incomingConnection(int socketDescriptor);
signals:
public slots:
};
#endif // SERVER_H
#ifndef SERVER_H
#define SERVER_H
#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include "sendthread.h"
class Server : public QTcpServer
{
Q_OBJECT
public:
explicit Server(QObject *parent = 0);
void ServerListener();
~Server();
protected:
void incomingConnection(int socketDescriptor);
signals:
public slots:
};
#endif // SERVER_H
To copy to clipboard, switch view to plain text mode
SendThread.cpp
#include "sendthread.h"
#define FILENAME "/root/Tars/Qt_SDK_Win_offline_v1_2_en.exe"
SendThread
::SendThread(int socketDescriptor,
QObject *parent
) : QThread(parent
), socketDescriptor
(socketDescriptor
) {
}
void SendThread::run()
{
qDebug() << "Thread Descriptor :" << socketDescriptor;
if (!client.setSocketDescriptor(socketDescriptor))
{
qDebug() << client.error();
return;
}
qDebug() << "Thread : Connected";
//send File
QFile inputFile
(FILENAME
);
while(1)
{
read.clear();
read = inputFile.read(32768*8);
qDebug() << "Read : " << read.size();
if(read.size()==0)
break;
qDebug() << "Written : " << client.write(read);
client.waitForBytesWritten();
read.clear();
}
inputFile.close();
client.disconnectFromHost();
client.waitForDisconnected();
qDebug() << "Thread : File transfer completed";
}
#include "sendthread.h"
#define FILENAME "/root/Tars/Qt_SDK_Win_offline_v1_2_en.exe"
SendThread::SendThread(int socketDescriptor,QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor)
{
}
void SendThread::run()
{
QTcpSocket client;
qDebug() << "Thread Descriptor :" << socketDescriptor;
if (!client.setSocketDescriptor(socketDescriptor))
{
qDebug() << client.error();
return;
}
qDebug() << "Thread : Connected";
//send File
QFile inputFile(FILENAME);
QByteArray read;
inputFile.open(QIODevice::ReadOnly);
while(1)
{
read.clear();
read = inputFile.read(32768*8);
qDebug() << "Read : " << read.size();
if(read.size()==0)
break;
qDebug() << "Written : " << client.write(read);
client.waitForBytesWritten();
read.clear();
}
inputFile.close();
client.disconnectFromHost();
client.waitForDisconnected();
qDebug() << "Thread : File transfer completed";
}
To copy to clipboard, switch view to plain text mode
SendThread.h
#ifndef SENDTHREAD_H
#define SENDTHREAD_H
#include <QThread>
#include <QFile>
#include <QTcpSocket>
{
public:
SendThread
(int socketdescriptor,
QObject *parent
);
void run();
private:
int socketDescriptor;
};
#endif // SENDTHREAD_H
#ifndef SENDTHREAD_H
#define SENDTHREAD_H
#include <QThread>
#include <QFile>
#include <QTcpSocket>
class SendThread : public QThread
{
public:
SendThread(int socketdescriptor,QObject *parent);
void run();
private:
int socketDescriptor;
};
#endif // SENDTHREAD_H
To copy to clipboard, switch view to plain text mode
Client Code:-
------------------------
Client.cpp
#include "client.h"
{
client->abort();
connect(client, SIGNAL(readyRead()), this, SLOT(ReadData()));
connect(client, SIGNAL(disconnected()), this, SLOT(Completed()));
}
Client::~Client()
{
client->close();
}
{
filename = file;
client->connectToHost(addr, port);
qDebug() << client->socketDescriptor();
}
void Client::Completed()
{
qDebug() << "File transfer complete";
}
void Client::ReadData()
{
{
qDebug("File cannot be opened.");
exit(0);
}
QByteArray read
= client
->read
(client
->bytesAvailable
());
qDebug() << "Read : " << read.size();
file.write(read);
file.close();
}
#include "client.h"
Client::Client(QObject *parent) :
QObject(parent)
{
client = new QTcpSocket(this);
client->abort();
connect(client, SIGNAL(readyRead()), this, SLOT(ReadData()));
connect(client, SIGNAL(disconnected()), this, SLOT(Completed()));
}
Client::~Client()
{
client->close();
}
void Client::start(QString address, quint16 port, QString file)
{
QHostAddress addr(address);
filename = file;
client->connectToHost(addr, port);
qDebug() << client->socketDescriptor();
}
void Client::Completed()
{
qDebug() << "File transfer complete";
}
void Client::ReadData()
{
QFile file(filename);
if(!(file.open(QIODevice::Append)))
{
qDebug("File cannot be opened.");
exit(0);
}
QByteArray read = client->read(client->bytesAvailable());
qDebug() << "Read : " << read.size();
file.write(read);
file.close();
}
To copy to clipboard, switch view to plain text mode
client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <QHostAddress>
#include <QFile>
{
Q_OBJECT
public:
explicit Client
(QObject *parent
= 0);
~Client();
private:
signals:
public slots:
void ReadData();
void Completed();
};
#endif // CLIENT_H
#ifndef CLIENT_H
#define CLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <QHostAddress>
#include <QFile>
class Client : public QObject
{
Q_OBJECT
public:
explicit Client(QObject *parent = 0);
~Client();
QTcpSocket *client;
void start(QString address, quint16 port, QString file);
QString filename;
private:
signals:
public slots:
void ReadData();
void Completed();
};
#endif // CLIENT_H
To copy to clipboard, switch view to plain text mode
Call Client as:
Client c;
c.start("192.168.10.210",8888,"/root/file1");
Bookmarks