照着书上的例程敲了一个文件传输的tcp程序,但是却出现了Connection refused这个错误,我使用的环境是
win7+qt mingw
server.cpp
#include "server.h"
#include "ui_server.h"
#include <QtNetwork>
Server::Server(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Server)
{
ui->setupUi(this);
connect(&tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
}
Server::~Server()
{
delete ui;
}
void Server::start()
{
if(!tcpServer.listen(QHostAddress::LocalHost,6666))
{
qDebug() << "连接错误信息:"<<tcpServer.errorString();
close();
return;
}
ui->startButton->setEnabled(false);
totalBytes = 0;
bytesReceived = 0;
fileNameSize = 0;
ui->serverStatusLabel->setText(tr("监听"));
ui->serverProgressBar->reset();
}
void Server::acceptConnection()
{
qDebug() << "建立连接";
tcpServerConnection = tcpServer.nextPendingConnection();
connect(tcpServerConnection,SIGNAL(readyRead()),this,SLOT(updateServerProgress()));
connect(tcpServerConnection,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(displayError(QAbstractSocket::SocketError)));
ui->serverStatusLabel->setText(tr("接受连接"));
tcpServer.close();
}
void Server::updateServerProgress()
{
QDataStream in(tcpServerConnection);
in.setVersion(QDataStream::Qt_4_0);
if(bytesReceived <= sizeof(qint64) * 2)
{
if((tcpServerConnection->bytesAvailable() >= sizeof(qint64)*2)
&& (fileNameSize == 0))
{
in >> totalBytes >> fileNameSize;
bytesReceived += sizeof(qint64) *2;
}
}
if((tcpServerConnection->bytesAvailable() >= fileNameSize)
&& (fileNameSize != 0))
{
in >> fileName;
ui->serverStatusLabel->setText(tr("接受文件 %1 ...").arg(fileName));
bytesReceived += fileNameSize;
localFile = new QFile(fileName);
if(!localFile -> open(QFile::WriteOnly))
{
qDebug() << "Server :open file error!";
return ;
}
else
{
return ;
}
}
if(bytesReceived < totalBytes)
{
bytesReceived += tcpServerConnection -> bytesAvailable();
inBlock = tcpServerConnection->readAll();
localFile->write(inBlock);
inBlock.resize(0);
}
ui->serverProgressBar->setMaximum(totalBytes);
ui->serverProgressBar->setValue(bytesReceived);
if(bytesReceived == totalBytes)
{
tcpServerConnection ->close();
localFile->close();
ui->startButton->setEnabled(true);
ui->serverStatusLabel->setText(tr("接受文件 %1").arg(fileName));
}
}
void Server::displayError(QAbstractSocket::SocketError)
{
qDebug() << tcpServerConnection -> errorString();
tcpServerConnection->close();
ui->serverProgressBar->reset();
ui->serverStatusLabel->setText(tr("服务器端就绪"));
ui->startButton->setEnabled(true);
}
void Server::on_startButton_clicked()
{
qDebug() << "开始监听";
start();
}
client.cpp
#include "client.h"
#include "ui_client.h"
#include <QtNetwork>
#include <QFileDialog>
client::client(QWidget *parent) :
QDialog(parent),
ui(new Ui::client)
{
ui->setupUi(this);
payloadSize = 64 * 1024;
totalBytes = 0;
bytesWritten = 0;
bytesToWriten = 0;\
ui->hostLineEdit->setText("192.168.1.103");
ui->portLineEdit->setText("6666");
tcpClient = new QTcpSocket(this);
connect(tcpClient,SIGNAL(connected()),this,SLOT(startTransfer()));
connect(tcpClient,SIGNAL(bytesWritten(qint64)),this,SLOT(updateClientProgress(qint64)));
connect(tcpClient,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(displayError(QAbstractSocket::SocketError)));
ui->sendButton->setEnabled(false);
}
void client::openFile()
{
fileName = QFileDialog::getOpenFileName(this);
if(!fileName.isEmpty())
{
ui->sendButton->setEnabled(true);
ui->clientStatusLabel->setText(tr("打开文件 %1 成功!").arg(fileName));
}
}
void client::send()
{
ui->sendButton->setEnabled(true);
bytesWritten = 0;
ui->clientStatusLabel->setText(tr("连接中。。。"));
qDebug() << "ip:" <<ui->hostLineEdit->text()
<< "port:" << ui->portLineEdit->text().toInt();
tcpClient->connectToHost(ui->hostLineEdit->text().trimmed(),
ui->portLineEdit->text().toInt());
}
void client::startTransfer()
{
qDebug() << "连接槽!";
localFile = new QFile(fileName);
if(!localFile->open(QFile::ReadOnly))
{
qDebug() << "client : open file error!";
return ;
}
totalBytes = localFile->size();
QDataStream sendOut(&outBlock,QIODevice::WriteOnly);
sendOut.setVersion(QDataStream::Qt_4_0);
QString currentFileName = fileName.right(fileName.size() -
fileName.lastIndexOf('|')-1);
sendOut << qint64(0) << qint64(0) << currentFileName;
totalBytes += outBlock.size();
sendOut.device()->seek(0);
sendOut << totalBytes << qint64((outBlock.size()-sizeof(qint64)*2));
bytesToWriten = totalBytes - tcpClient->write(outBlock);
ui->clientStatusLabel->setText(tr("已连接"));
outBlock.resize(0);
}
void client::updateClientProgress(qint64 numBytes)
{
bytesWritten += (int)numBytes;
if(bytesToWriten > 0)
{
outBlock = localFile->read(qMin(bytesToWriten,payloadSize));
bytesToWriten -= (int)tcpClient->write(outBlock);
outBlock.resize(0);
}
else
{
localFile->close();
}
ui->clientProgressBar->setMaximum(totalBytes);
ui->clientProgressBar->setValue(bytesWritten);
if(bytesWritten == totalBytes)
{
ui->clientStatusLabel->setText(tr("传送文件 %1 成功").arg(fileName));
localFile->close();
tcpClient->close();
}
}
void client::displayError(QAbstractSocket::SocketError)
{
qDebug() << "客户端连接错误:"<<tcpClient->errorString();
tcpClient->close();
ui->clientProgressBar->reset();
ui->clientStatusLabel->setText(tr("客户端就绪"));
ui->sendButton->setEnabled(true);
}
void client::on_openButton_clicked()
{
ui->clientProgressBar->reset();
ui->clientStatusLabel->setText(tr("状态:等待打开文件!"));
openFile();
}
void client::on_sendButton_clicked()
{
send();
}
client::~client()
{
delete ui;
delete tcpClient;
}
原因:由于win7防火墙的关系,导致无法连接服务器
解决方案:将服务器的连接方式做修改
if(!tcpServer.listen(QHostAddress::LocalHost,6666))
{
qDebug() << "连接错误信息:"<<tcpServer.errorString();
close();
return;
}
修改为
if(!tcpServer.listen(QHostAddress::Any,6666))
{
qDebug() << "连接错误信息:"<<tcpServer.errorString();
close();
return;
}
修改后,服务器就可以连接了。