• 6284阅读
  • 1回复

Qt socket Client & Server 問題2 [复制链接]

上一主题 下一主题
离线nkc731210
 
只看楼主 倒序阅读 楼主  发表于: 2010-03-19
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
Dear All:

以下是socket Server 端,但是有一個問題:只能接收一次,收完後就要重新啟動Listen

有甚麼方法可以收完一個檔案後繼續Listen 下次要傳送的檔案,不需要再重新啟動
ServerDialog.cpp
  1. #include <QApplication>
  2. #include <QProgressBar>
  3. #include <QVBoxLayout>
  4. #include <QHostAddress>
  5. #include <QLabel>
  6. #include <QAbstractSocket>
  7. #include <QTcpSocket>
  8. #include <QMessageBox>
  9. #include <QFile>
  10. #include <iostream>
  11. using namespace std;
  12. #include "ServerDialog.h"
  13. ServerDialog::ServerDialog(QWidget *parent) : QDialog(parent) {
  14.     label = new QLabel("Received:");
  15.     QVBoxLayout *layout = new QVBoxLayout;
  16.     layout->addWidget(label);
  17.     this->setLayout(layout);
  18.     
  19.     connect(&server, SIGNAL(newConnection()),
  20.             this, SLOT(acceptConnection()));
  21. }
  22. void ServerDialog::setReceivedFileName(QString fileName) {
  23.     file = new QFile(fileName);
  24. }
  25. void ServerDialog::listen(quint16 port) {
  26.     server.listen(QHostAddress::Any, port);
  27. }
  28. void ServerDialog::acceptConnection() {
  29.     if (!file->open(QIODevice::WriteOnly)) {
  30.         cerr << "Unable to write the file" << endl;
  31.         delete file;
  32.         file = 0;
  33.         return;
  34.     }    
  35.     
  36.     clientConnection = server.nextPendingConnection();
  37.     
  38.     connect(clientConnection, SIGNAL(readyRead()),
  39.             this, SLOT(updateProgress()));
  40.     connect(clientConnection, SIGNAL(error(QAbstractSocket::SocketError)),
  41.             this, SLOT(displayError(QAbstractSocket::SocketError)));
  42.     
  43.     server.close();
  44.     
  45.     QApplication::setOverrideCursor(Qt::WaitCursor);
  46. }
  47. void ServerDialog::updateProgress() {
  48.     bytesReceived += (int) clientConnection->bytesAvailable();
  49.     file->write(clientConnection->readAll());
  50.     
  51.     QString txt = "Received %1MB";
  52.     
  53.     label->setText(txt.arg(bytesReceived / (1024 * 1024)));
  54. }
  55. void ServerDialog::displayError(QAbstractSocket::SocketError socketError) {
  56.     file->close();
  57.     
  58.     if (socketError == QTcpSocket::RemoteHostClosedError) {
  59.         QMessageBox::information(this,
  60.                "OK!", "Save file as " + file->fileName());
  61.     }
  62.     else {
  63.         QMessageBox::information(this, "Network error",
  64.            "The following error occurred: " + clientConnection->errorString());
  65.     }
  66.     
  67.     delete file;
  68.     file = 0;
  69.     
  70.     QApplication::restoreOverrideCursor();
  71. }



ServerDialog.h
  1. #ifndef SERVERDIALOG_H
  2. #define SERVERDIALOG_H
  3. #include <QDialog>
  4. #include <QTcpServer>
  5. class QLabel;
  6. class QFile;
  7. class QTcpSocket;
  8. class ServerDialog : public QDialog {
  9.    Q_OBJECT
  10.   
  11. public:
  12.     ServerDialog(QWidget *parent = 0);
  13.     void setReceivedFileName(QString fileName);
  14.     void listen(quint16 port);
  15.     
  16. public slots:
  17.     void acceptConnection();
  18.     void updateProgress();
  19.     void displayError(QAbstractSocket::SocketError socketError);
  20. private:
  21.     QLabel *label;
  22.     QTcpServer server;
  23.     QTcpSocket *clientConnection;
  24.     int bytesReceived;
  25.     QFile *file;
  26. };
  27. #endif


main.cpp
  1. ServerDialog *server = new ServerDialog;
  2. server->setReceivedFileName("f://received.dat");
  3. server->listen(9393);
  4. server->setWindowTitle("QTcpServer");
  5. server->resize(200, 10);
  6. server->show();
离线wangqy
只看该作者 1楼 发表于: 2010-03-22
void ServerDialog::acceptConnection() {
    if (!file->open(QIODevice::WriteOnly)) {
        cerr << "Unable to write the file" << endl;
        delete file;
        file = 0;
        return;
    }    
    
    clientConnection = server.nextPendingConnection();
    
    connect(clientConnection, SIGNAL(readyRead()),
            this, SLOT(updateProgress()));
    connect(clientConnection, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(displayError(QAbstractSocket::SocketError)));
    
   server.close();  //我觉得不应该有这一句。
    
    QApplication::setOverrideCursor(Qt::WaitCursor);
}
快速回复
限100 字节
 
上一个 下一个