当前位置: 首页>>代码示例>>C++>>正文


C++ QTcpServer::isListening方法代码示例

本文整理汇总了C++中QTcpServer::isListening方法的典型用法代码示例。如果您正苦于以下问题:C++ QTcpServer::isListening方法的具体用法?C++ QTcpServer::isListening怎么用?C++ QTcpServer::isListening使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QTcpServer的用法示例。


在下文中一共展示了QTcpServer::isListening方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: clientServerLoop

//----------------------------------------------------------------------------------
void tst_QTcpServer::clientServerLoop()
{
    QTcpServer server;

    QSignalSpy spy(&server, SIGNAL(newConnection()));

    QVERIFY(!server.isListening());
    QVERIFY(!server.hasPendingConnections());
    QVERIFY(server.listen(QHostAddress::Any, 11423));
    QVERIFY(server.isListening());

    QTcpSocket client;

    QHostAddress serverAddress = QHostAddress::LocalHost;
    if (!(server.serverAddress() == QHostAddress::Any))
        serverAddress = server.serverAddress();

    client.connectToHost(serverAddress, server.serverPort());
    QVERIFY(client.waitForConnected(5000));

    QVERIFY(server.waitForNewConnection(5000));
    QVERIFY(server.hasPendingConnections());

    QCOMPARE(spy.count(), 1);

    QTcpSocket *serverSocket = server.nextPendingConnection();
    QVERIFY(serverSocket != 0);

    QVERIFY(serverSocket->write("Greetings, client!\n", 19) == 19);
    serverSocket->flush();

    QVERIFY(client.waitForReadyRead(5000));
    QByteArray arr = client.readAll();
    QCOMPARE(arr.constData(), "Greetings, client!\n");

    QVERIFY(client.write("Well, hello to you!\n", 20) == 20);
    client.flush();

    QVERIFY(serverSocket->waitForReadyRead(5000));
    arr = serverSocket->readAll();
    QCOMPARE(arr.constData(), "Well, hello to you!\n");
}
开发者ID:husninazer,项目名称:qt,代码行数:43,代码来源:tst_qtcpserver.cpp

示例2: Listen

bool CHttpServer::Listen(int Port)
{
	QTcpServer* pListener = new QTcpServer(this);
	pListener->listen(QHostAddress::Any, Port);
    if (!pListener->isListening()) 
	{
		delete pListener;
		return false;
	}
	connect(pListener, SIGNAL(newConnection()), this, SLOT(OnConnection()));

	m_Listners.insert(Port, pListener);
	return true;
}
开发者ID:bratao,项目名称:NeoLoader,代码行数:14,代码来源:HttpServer.cpp

示例3: constructing

void tst_QTcpServer::constructing()
{
    QTcpServer socket;

    // Check the initial state of the QTcpSocket.
    QCOMPARE(socket.isListening(), false);
    QCOMPARE((int)socket.serverPort(), 0);
    QCOMPARE(socket.serverAddress(), QHostAddress());
    QCOMPARE(socket.maxPendingConnections(), 30);
    QCOMPARE(socket.hasPendingConnections(), false);
    QCOMPARE(socket.socketDescriptor(), -1);
    QCOMPARE(socket.serverError(), QAbstractSocket::UnknownSocketError);

    // Check the state of the socket layer?
}
开发者ID:husninazer,项目名称:qt,代码行数:15,代码来源:tst_qtcpserver.cpp

示例4: createServerSocketAndListen

        QTcpServer* DccCommon::createServerSocketAndListen( QObject* parent, QString* failedReason, int minPort, int maxPort )
        {
            QTcpServer* socket = new QTcpServer( parent );

            if ( minPort > 0 && maxPort >= minPort )  // ports are configured manually
            {
                // set port
                bool found = false;                       // whether succeeded to set port
                for ( int port = minPort; port <= maxPort ; ++port )
                {
                    bool success = socket->listen( QHostAddress::Any, port );
                    if ( ( found = ( success && socket->isListening() ) ) )
                        break;
                    socket->close();
                }
                if ( !found )
                {
                    if ( failedReason )
                        *failedReason = i18n( "No vacant port" );
                    delete socket;
                    return 0;
                }
            }
            else
            {
                // Let the operating system choose a port
                if ( !socket->listen() )
                {
                    if ( failedReason )
                        *failedReason = i18n( "Could not open a socket" );
                    delete socket;
                    return 0;
                }
            }

            return socket;
        }
开发者ID:wordlet,项目名称:mykonvi,代码行数:37,代码来源:dcccommon.cpp

示例5: run


//.........这里部分代码省略.........

    if (!pSocket)
        WTHROW1("ViewerHandler: invalid socket - terminating");

    WDEBUG1("ViewerHandler: entering main loop");
	initialized();
    // Main request processing loop
    for(;;) {

        if (pSocket->bytesAvailable() > 0 || pSocket->waitForReadyRead(10000)) {

            WDEBUG1("ViewerHandler: reading data from buffer");

            //Reads the whole buffer
            QString qsData = pSocket->readAll();

			// TODO: it might be that this is not the only request in the string
			//       -> the request should be handled properly
			if(qsData.contains("Connector.InitiateShutdown.1")) {
				WDEBUG1("ViewerHandler: Connector.InitiateShutdown.1 received - doing nothing");
                //break;
			}

            //Splits the buffer in several requests
            QStringList& rlRequests = qsData.split("\n\n\n", QString::SkipEmptyParts);

            //Counts number of requests
            int count = rlRequests.size();

			WDEBUG2("ViewerHandler: %d request(s) received", count);

            //For each request
            for (i=0; i<count; i++) {
				WDEBUG3("ViewerHandler: Sending request %d to be handled: \n%s\n", i, rlRequests[i].toAscii().data());

                //handles it
                if(this->request->handle(rlRequests[i])) {
					WDEBUG1("ViewerHandler: error while handling request - next request");
                    continue;
                }
            }
        //If there're no requests received
        } else {
            //Checks if socket is still valid and disconnect if not
            WDEBUG2("Socket state: %d", (int) pSocket->state());
            if(pSocket->state() != QAbstractSocket::ConnectedState) {
                WDEBUG1("Socket is no more connected - closing program");
				WWRITE1 ("Socket no more connected - shutting down");

                break;
            }
        }
    }

    WEND:
				
	WDEBUG1("ViewerHandler: sending quit to the main application");
	qApp->quit();

	while (!is_shutdown()) {
		//wait to receive shutdown from main thread
		WDEBUG1("ViewerHandler: waiting to receive shutdown");
		QThread::msleep(1000);
		//vhSleep(1000);
	}
	WDEBUG1("ViewerHandler: shutdown received");

    WDEBUG1("ViewerHandler: stopping VoiceClient...");
    voiceClient.end();

    voiceClient.wait();
	WDEBUG1("Viewer Handler: VoiceClient has terminated");

    //WDEBUG1("Stopping talkingThread...");
    //if(this->tt->isRunning())this->tt->quit();
    WDEBUG1("ViewerHandler: stopping TalkingThread...");
    //voiceClient.end();

	if(tt != 0) {
		tt->end();
		tt->wait();
		delete tt;
		tt=0;
		WDEBUG1("Viewer Handler: TalkingThread has terminated");
	}

    WDEBUG1("ViewerHandler: finishing...");

	if (pSocket){
		pSocket->close();

		WDEBUG2("ViewerHandler: pSocket status: %s", pSocket->state() == QAbstractSocket::UnconnectedState ? "Unconnected" : "Still Up");
		pSocket = 0;
	}
	server.close();

	WDEBUG2("ViewerHandler: server status: %s", server.isListening() ? "Listening" : "Shutdown");

    return;
}
开发者ID:vgaessler,项目名称:whisper_client,代码行数:101,代码来源:wViewerHandler.cpp


注:本文中的QTcpServer::isListening方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。