• 10283阅读
  • 3回复

QServerSocket和QSocket这样用为什么不对? [复制链接]

上一主题 下一主题
离线wangyue5826
 

只看楼主 倒序阅读 楼主  发表于: 2008-05-22
QServerSocket和QSocket这样用为什么不对?
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
class ServerInfo : public QVBox
{
    Q_OBJECT
public:
    ServerInfo()
    {
        server = new QServerSocket( 4242, 1, parent );
        if ( !server->ok() ) {
            qWarning("Failed to bind to port 4242");
            exit(1);
        }
        socket = new QSocket(this);
      void server->newConnection( int socket )
    {
        socket->setSocket( socket );
        connect( socket, SIGNAL(readyRead()), SLOT(readClient()) );
        connect( socket, SIGNAL(connectionClosed()), SLOT(connectionClosed()) );
        emit newConnect();
    }
       
        QString itext = QString(
                "This is a small server example.\n"
                "Connect with the client now."
                );
        QLabel *lb = new QLabel( itext, this );
        lb->setAlignment( AlignHCenter );
        infoText = new QTextView( this );
        QPushButton *quit = new QPushButton( "Quit" , this );

        connect( server, SIGNAL(newConnect()), SLOT(newConnection()) );
        connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
    }

    ~ServerInfo()
    {
    }

private slots:
    void readClient()
    { 
        line=0;
        while ( socket->canReadLine() ) {
/*          QTextStream os( this );
            os << line << ": " <<socket->readLine();
            line++; */
            infoText->append(socket->readLine());
        }
    }

    void connectionClosed()
    {
        delete this;
    }
   

    void newConnection()
    {
        infoText->append( "New connection\n" );
    }

signals:
    void newConnect();

private:
    int line;
    QTextView *infoText;
    QServerSocket *server;
    QSocket *socket;
};
离线sake
只看该作者 1楼 发表于: 2008-11-24
你的QT什么版本啊,怎么还有QVBOX这个 类
离线nliuser
只看该作者 2楼 发表于: 2008-12-24
分成*.h和*.cpp两个文件
离线wangshaohui
只看该作者 3楼 发表于: 2010-07-07
关注中……
快速回复
限100 字节
 
上一个 下一个