本文整理汇总了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");
}
示例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;
}
示例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?
}
示例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;
}
示例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;
}