2011-06-25 7 views
1

私は単純なサーバークライアントアプリケーションを実行しています。しかし、クライアントはサーバー側から未定義の動作を得ます。エラーコードを取得した後、サーバーが接続を切断したことがわかりました。Qt - QTcpserverが正常に動作しません。

これは、サーバー側main.cppに

#include <QApplication> 
#include <QFile> 
#include <QFileDialog> 
#include <QMessageBox> 
#include <QtNetwork/QTcpServer> 
#include <QtNetwork/QTcpSocket> 

class MyMessageBox:public QMessageBox 
{ 
public: 
    MyMessageBox(std::string message,QWidget *parent=0):QMessageBox(QMessageBox::NoIcon,QString("ErrorMessage"),QString(message.c_str()),QMessageBox::Ok,parent,Qt::Widget) 
    { 
    } 
}; 

class My_Server:public QTcpServer 
{ 
    Q_OBJECT 
public: 
    My_Server(); 
public slots: 
    void on_Connection(); 
}; 

My_Server::My_Server():QTcpServer() 
{ 
    connect(this,SIGNAL(newConnection()),this,SLOT(on_Connection())); 
} 

void My_Server::on_Connection() 
{ 
    MyMessageBox mm("Connection Established"); 
    mm.exec(); 
    QTcpSocket * my_Socket = this->nextPendingConnection(); 

    my_Socket->waitForBytesWritten(30000); 

    QByteArray block("Hi all"); 

    my_Socket->write(block); 
} 

int main(int argc,char * argv[]) 
{ 
    QApplication app(argc,argv); 

    My_Server tcp_Server; 
    tcp_Server.listen(QHostAddress("127.0.0.1"),15000); 

    return app.exec(); 
} 

#include "main.moc" 

これは、クライアント側main.cppに

#include <QApplication> 
#include <QDataStream> 
#include <QFile> 
#include <QFileDialog> 
#include <QHostAddress> 
#include <QMessageBox> 
#include <QtNetwork/QTcpServer> 
#include <QtNetwork/QTcpSocket> 

class MyMessageBox:public QMessageBox 
{ 
public: 
    MyMessageBox(std::string message,QWidget *parent=0):QMessageBox(QMessageBox::NoIcon,QString("ErrorMessage"),QString(message.c_str()),QMessageBox::Ok,parent,Qt::Widget) 
    { 
    } 
}; 

int main(int argc,char * argv[]) 
{ 
    QApplication app(argc,argv); 

    QTcpSocket client_Socket; 

    client_Socket.connectToHost(QHostAddress("127.0.0.1"),15000); 

    QDataStream in(&client_Socket); 
    in.setVersion(QDataStream::Qt_4_7); 

    client_Socket.waitForReadyRead(30000); 

    char buf[100]={'\0'}; 
    client_Socket.read(buf,(quint16)sizeof(buf)); 
    QString nothing(buf); 

    MyMessageBox mm((QString("++ ")+nothing+" ++").toStdString()); 
    mm.exec(); 

    MyMessageBox mn(QString::number(client_Socket.error()).toStdString()); 
    mn.exec(); 

    return app.exec(); 
} 

これは(どちらも同じ)のプロファイル

TEMPLATE = app 
TARGET = 
DEPENDPATH += . 
INCLUDEPATH += . 

# Input 
SOURCES += main.cpp 

QT += network 
です

私は、なぜサーバー側が接続を切断しているのか分かりません。誰かが原因を見つけるのを助けたら、私は彼らに感謝するでしょう。

注:私は、Windowsプラットフォームのサーバーは、それがそのメッセージを送ったの右後に終了しているため、接続がクローズなっている

答えて

1

でのQt 4.7.2を使用しています。

これはおそらくQApplicationを使用しているためですが、実際には長持ちのGUIウィジェットはありません。最初のダイアログボックスの表示が終わったら、イベントループは短時間停止します。

いずれかを試してみてください。

  • を代わりにサーバー側でQCoreApplicationを使用。 (メッセージボックスを起動しないでください.QCoreApplicationではGUIは許可されません)
  • あなたのサーバに、TCPサーバの起動を担当するメインのウィジェットを与えます。
+0

あなたの答えは "雄牛の目"です。私はすぐに終了することを避けるために、サーバー側のQTcpSocketで "waitForReadyRead"のような他のメカニズムを使用します。 GUIはこのアプリケーションの中心ですから。 – prabhakaran

0

QApplicationのquitOnLastWindowClosedプロパティをチェックすると、ダイアログを閉じるとアプリケーションが終了しなくなります。

また、サーバーは実際の書き込み前に「バイトが書き込まれるのを待っています」。 waitForBytesWrittenを呼び出すより先に書き込み(ブロック)する必要があります

関連する問題