wei243
8th March 2007, 07:29
special thx to wysota, help me solve most of my problem.
now i manage to send any file form QTcpSocket to QTcpServer within 1 standalone pc.
but if i transfer a file within a network, the receiver only receive PARTIALLY, except the file size is very small.
how to slove this problem?
below is my sender program with QTcpSocket:
void sender::sending()
{
tcpSocket1 = new QTcpSocket(this);
tcpSocket1->abort();
tcpSocket1->connectToHost(targetIP,targetPort);
file.setFileName(fileName);
file.open(QIODevice::ReadOnly);
QByteArray Data;
Data = file.readAll();
tcpSocket1->write(Data);
connect(tcpSocket1, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(tcpSocket1, SIGNAL(disconnected()),tcpSocket1, SLOT(deleteLater()));
connect(tcpSocket1, SIGNAL(disconnected()),this, SLOT(closeFile()));
}
void sender::closeFile()
{
file.close();
}
receiver program with QTcpServer:
void receiver::startserver()
{
tcpServer1 = new QTcpServer(this);
if (!tcpServer1->listen(QHostAddress(ownIP),0)){
QMessageBox::critical(0, tr("Listening...."),tr("Unable to start the server: ").arg(tcpServer1->errorString()));
}
timer = new QTimer(this);
connect(tcpServer1, SIGNAL(newConnection()), this, SLOT(startTimer()));
connect(timer, SIGNAL(timeout()), this, SLOT(readIncoming()));
}
void receiver::startTimer()
{
timer->start(500);
}
void receiver::readIncoming()
{
timer->stop();
QTcpSocket *clientConnection = tcpServer1->nextPendingConnection();
QApplication::setOverrideCursor(Qt::WaitCursor);
QByteArray Data = clientConnection->readAll();
file.setFileName(fileName);
file.open(QIODevice::WriteOnly);
file.write(Data);
connect(clientConnection, SIGNAL(disconnected()),clientConnection, SLOT(deleteLater()));
connect(clientConnection, SIGNAL(disconnected()),this, SLOT(closeFile()));
clientConnection->disconnectFromHost();
QApplication::restoreOverrideCursor();
}
void receiver::closeFile()
{
file.close();
}
now i manage to send any file form QTcpSocket to QTcpServer within 1 standalone pc.
but if i transfer a file within a network, the receiver only receive PARTIALLY, except the file size is very small.
how to slove this problem?
below is my sender program with QTcpSocket:
void sender::sending()
{
tcpSocket1 = new QTcpSocket(this);
tcpSocket1->abort();
tcpSocket1->connectToHost(targetIP,targetPort);
file.setFileName(fileName);
file.open(QIODevice::ReadOnly);
QByteArray Data;
Data = file.readAll();
tcpSocket1->write(Data);
connect(tcpSocket1, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(tcpSocket1, SIGNAL(disconnected()),tcpSocket1, SLOT(deleteLater()));
connect(tcpSocket1, SIGNAL(disconnected()),this, SLOT(closeFile()));
}
void sender::closeFile()
{
file.close();
}
receiver program with QTcpServer:
void receiver::startserver()
{
tcpServer1 = new QTcpServer(this);
if (!tcpServer1->listen(QHostAddress(ownIP),0)){
QMessageBox::critical(0, tr("Listening...."),tr("Unable to start the server: ").arg(tcpServer1->errorString()));
}
timer = new QTimer(this);
connect(tcpServer1, SIGNAL(newConnection()), this, SLOT(startTimer()));
connect(timer, SIGNAL(timeout()), this, SLOT(readIncoming()));
}
void receiver::startTimer()
{
timer->start(500);
}
void receiver::readIncoming()
{
timer->stop();
QTcpSocket *clientConnection = tcpServer1->nextPendingConnection();
QApplication::setOverrideCursor(Qt::WaitCursor);
QByteArray Data = clientConnection->readAll();
file.setFileName(fileName);
file.open(QIODevice::WriteOnly);
file.write(Data);
connect(clientConnection, SIGNAL(disconnected()),clientConnection, SLOT(deleteLater()));
connect(clientConnection, SIGNAL(disconnected()),this, SLOT(closeFile()));
clientConnection->disconnectFromHost();
QApplication::restoreOverrideCursor();
}
void receiver::closeFile()
{
file.close();
}