PDA

View Full Version : Data transfer using TCP Socket



rex
24th March 2011, 16:06
Hello friends i am working on a software where i need to transfer data from one system to another using TCP/IP . I used the fortune client and fortune server example code as base and made this program but i am having problem in in receiving after the first transfer there is some problem not able to receive the data properly.

Data Sending Side

tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
rowcnt=1;
tcpServer = new QTcpServer(this);
if (!tcpServer->listen()) {
QMessageBox::critical(this, tr("Data Server"),
tr("Unable to start the server: %1.")
.arg(tcpServer->errorString()));
close();
return;
}

statusLabel->setText(tr("The server is running on port %1.\n"
"Run the Fortune Client example now.")
.arg(tcpServer->serverPort()));

}

ControlPanel::~ControlPanel()
{

}
void ControlPanel::sendFortune()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << tempdata;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));

clientConnection->write(block);
//clientConnection->disconnectFromHost();
qDebug()<<tempdata;
tempdata.clear();
}

void ControlPanel::extract()
{
// Retrieving data from db and appending it to tempdata and then calling the function sendFortune()

sendFortune();
}
void ControlPanel::on_pushButton_clicked()
{
// I wanted to send data every Second so i am calling extract() every second.
connect(timerdraw, SIGNAL(timeout()), this, SLOT(extract()));
timerdraw->start(1000);
}

This is the receiver side of the program.


setupUi(this);
portLineEdit->setValidator(new QIntValidator(1, 65535, this));
tcpSocket = new QTcpSocket(this);
timerdraw = new QTimer(this);
timerreq = new QTimer(this);

connect(hostLineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(enablepushButton()));
connect(portLineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(enablepushButton()));
//connect(pushButton, SIGNAL(clicked()),
// this, SLOT(requestNewFortune()));
connect(pushButton_2, SIGNAL(clicked()), this, SLOT(close()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));


}
void TCP_DataReceiver::requestNewFortune()
{
pushButton->setEnabled(false);
blockSize = 0;
tcpSocket->abort();
tcpSocket->connectToHost(hostLineEdit->text(),
portLineEdit->text().toInt());
}
void TCP_DataReceiver::readFortune()
{
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);

if (blockSize == 0)
{
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;

in >> blockSize;
}

if (tcpSocket->bytesAvailable() < blockSize)
return;

QString nextFortune;
in >> nextFortune;

// if (nextFortune == currentFortune)
// {
// QTimer::singleShot(0, this, SLOT(requestNewFortune()));
// return;
// }

currentFortune = nextFortune;

statusLabel->setText(currentFortune);
textBrowser->append(currentFortune);
currentFortune.clear();
pushButton->setEnabled(true);
}

I am not able to figure out if my problem is on the Data receiving side or the sending side. I have connected two windows systems with a cross cable RJ45 connector. I wanted to send data every second to the other system where i run the receiving side of the program.. The connection breaks after some data is transferred to other system.. Pls tell me if i am going wrong some where. ?

Thank You