本文整理汇总了C++中QTcpServer::nextPendingConnection方法的典型用法代码示例。如果您正苦于以下问题:C++ QTcpServer::nextPendingConnection方法的具体用法?C++ QTcpServer::nextPendingConnection怎么用?C++ QTcpServer::nextPendingConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTcpServer
的用法示例。
在下文中一共展示了QTcpServer::nextPendingConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: newConnection
void ServerSktTcp::newConnection()
{
QTcpServer* server = qobject_cast<QTcpServer*>(sender());
if (!server) return;
QTcpSocket* client = server->nextPendingConnection();
while (client)
{
Conn* conn = new Conn;
if (!conn)
{
client->deleteLater();
}
else
{
client->setProperty(PROP_CONN, qVariantFromValue((void*)conn));
conn->client = client;
conn->key = TK::ipstr(client->peerAddress(),client->peerPort(), true);
connect(client, SIGNAL(readyRead()), this, SLOT(newData()));
connect(client, SIGNAL(destroyed(QObject*)), this, SLOT(close(QObject*)));
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
connect(client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
setCookie(conn->key, conn);
}
client = server->nextPendingConnection();
}
}
示例2: defined
//----------------------------------------------------------------------------------
void tst_QTcpServer::ipv6Server()
{
#if defined(Q_OS_SYMBIAN)
QSKIP("Symbian: IPv6 is not yet supported", SkipAll);
#endif
//### need to enter the event loop for the server to get the connection ?? ( windows)
QTcpServer server;
if (!server.listen(QHostAddress::LocalHostIPv6, 8944)) {
QVERIFY(server.serverError() == QAbstractSocket::UnsupportedSocketOperationError);
return;
}
QVERIFY(server.serverPort() == 8944);
QVERIFY(server.serverAddress() == QHostAddress::LocalHostIPv6);
QTcpSocket client;
client.connectToHost("::1", 8944);
QVERIFY(client.waitForConnected(5000));
QVERIFY(server.waitForNewConnection());
QVERIFY(server.hasPendingConnections());
QTcpSocket *serverSocket = 0;
QVERIFY((serverSocket = server.nextPendingConnection()));
}
示例3: posixSockets
// test only for posix
void tst_QSocketNotifier::posixSockets()
{
QTcpServer server;
QVERIFY(server.listen(QHostAddress::LocalHost, 0));
int posixSocket = qt_safe_socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr;
addr.sin_addr.s_addr = htonl(0x7f000001);
addr.sin_family = AF_INET;
addr.sin_port = htons(server.serverPort());
qt_safe_connect(posixSocket, (const struct sockaddr*)&addr, sizeof(sockaddr_in));
QVERIFY(server.waitForNewConnection(5000));
QScopedPointer<QTcpSocket> passive(server.nextPendingConnection());
::fcntl(posixSocket, F_SETFL, ::fcntl(posixSocket, F_GETFL) | O_NONBLOCK);
{
QSocketNotifier rn(posixSocket, QSocketNotifier::Read);
connect(&rn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy readSpy(&rn, SIGNAL(activated(int)));
QVERIFY(readSpy.isValid());
// No write notifier, some systems trigger write notification on socket creation, but not all
QSocketNotifier en(posixSocket, QSocketNotifier::Exception);
connect(&en, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy errorSpy(&en, SIGNAL(activated(int)));
QVERIFY(errorSpy.isValid());
passive->write("hello",6);
passive->waitForBytesWritten(5000);
QTestEventLoop::instance().enterLoop(3);
QCOMPARE(readSpy.count(), 1);
QCOMPARE(errorSpy.count(), 0);
char buffer[100];
int r = qt_safe_read(posixSocket, buffer, 100);
QCOMPARE(r, 6);
QCOMPARE(buffer, "hello");
QSocketNotifier wn(posixSocket, QSocketNotifier::Write);
connect(&wn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy writeSpy(&wn, SIGNAL(activated(int)));
QVERIFY(writeSpy.isValid());
qt_safe_write(posixSocket, "goodbye", 8);
QTestEventLoop::instance().enterLoop(3);
QCOMPARE(readSpy.count(), 1);
QCOMPARE(writeSpy.count(), 1);
QCOMPARE(errorSpy.count(), 0);
// Write notifier may have fired before the read notifier inside
// QTcpSocket, give QTcpSocket a chance to see the incoming data
passive->waitForReadyRead(100);
QCOMPARE(passive->readAll(), QByteArray("goodbye",8));
}
qt_safe_close(posixSocket);
}
示例4: ServerSession
void SessionServer::Private::onNewTcpConnection() {
QTcpServer * server = dynamic_cast< QTcpServer * >( this->sender() );
assert( server || !"QTcpServer casting failed" );
while( server->hasPendingConnections() ) {
QTcpSocket * socket = server->nextPendingConnection();
ServerSession * session = new ServerSession( socket, this );
this->pendingSessions.push( session );
}
emit this->newConnection();
}
示例5: posixSockets
void tst_QSocketNotifier::posixSockets()
{
#ifndef Q_OS_UNIX
QSKIP("test only for posix", SkipAll);
#else
QTcpServer server;
QVERIFY(server.listen(QHostAddress::LocalHost, 0));
int posixSocket = qt_safe_socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr;
addr.sin_addr.s_addr = htonl(0x7f000001);
addr.sin_family = AF_INET;
addr.sin_port = htons(server.serverPort());
qt_safe_connect(posixSocket, (const struct sockaddr*)&addr, sizeof(sockaddr_in));
QVERIFY(server.waitForNewConnection(5000));
QScopedPointer<QTcpSocket> passive(server.nextPendingConnection());
::fcntl(posixSocket, F_SETFL, ::fcntl(posixSocket, F_GETFL) | O_NONBLOCK);
{
QSocketNotifier rn(posixSocket, QSocketNotifier::Read);
connect(&rn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy readSpy(&rn, SIGNAL(activated(int)));
QSocketNotifier wn(posixSocket, QSocketNotifier::Write);
connect(&wn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy writeSpy(&wn, SIGNAL(activated(int)));
QSocketNotifier en(posixSocket, QSocketNotifier::Exception);
connect(&en, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy errorSpy(&en, SIGNAL(activated(int)));
passive->write("hello",6);
passive->waitForBytesWritten(5000);
QTestEventLoop::instance().enterLoop(3);
QCOMPARE(readSpy.count(), 1);
writeSpy.clear(); //depending on OS, write notifier triggers on creation or not.
QCOMPARE(errorSpy.count(), 0);
char buffer[100];
qt_safe_read(posixSocket, buffer, 100);
QCOMPARE(buffer, "hello");
qt_safe_write(posixSocket, "goodbye", 8);
QTestEventLoop::instance().enterLoop(3);
QCOMPARE(readSpy.count(), 1);
QCOMPARE(writeSpy.count(), 1);
QCOMPARE(errorSpy.count(), 0);
QCOMPARE(passive->readAll(), QByteArray("goodbye",8));
}
qt_safe_close(posixSocket);
#endif
}
示例6: serverOK
void NetworkDialog::serverOK()
{
QTcpServer* server = qobject_cast<QTcpServer*>(sender());
Q_ASSERT(server);
m_socket = server->nextPendingConnection();
Q_ASSERT(m_socket);
// refuse all other connections
while (server->hasPendingConnections()) {
delete server->nextPendingConnection();
}
// reparent socket, so that we can safely destroy the server
m_socket->setParent(this);
delete server;
// we're done
accept();
}
示例7: handleNewConnection
void ClientHandler::handleNewConnection()
{
QTcpServer *server = qobject_cast<QTcpServer *>(sender());
if ( server )
{
QTcpSocket *sock = server->nextPendingConnection();
QObject::connect(sock, SIGNAL(readyRead()),
this, SLOT(handleReadyRead()));
}
}
示例8: slotNewConnection
void QXmppSocksServer::slotNewConnection()
{
QTcpServer *server = qobject_cast<QTcpServer*>(sender());
if (!server)
return;
QTcpSocket *socket = server->nextPendingConnection();
if (!socket)
return;
// register socket
m_states.insert(socket, ConnectState);
connect(socket, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
}
示例9: maxPendingConnections
//----------------------------------------------------------------------------------
void tst_QTcpServer::maxPendingConnections()
{
QFETCH_GLOBAL(bool, setProxy);
if (setProxy) {
QFETCH_GLOBAL(int, proxyType);
if (proxyType == QNetworkProxy::Socks5Proxy) {
QSKIP("With socks5 only 1 connection is allowed ever", SkipAll);
}
}
//### sees to fail sometimes ... a timing issue with the test on windows
QTcpServer server;
server.setMaxPendingConnections(2);
QTcpSocket socket1;
QTcpSocket socket2;
QTcpSocket socket3;
QVERIFY(server.listen());
socket1.connectToHost(QHostAddress::LocalHost, server.serverPort());
socket2.connectToHost(QHostAddress::LocalHost, server.serverPort());
socket3.connectToHost(QHostAddress::LocalHost, server.serverPort());
QVERIFY(server.waitForNewConnection(5000));
QVERIFY(server.hasPendingConnections());
QVERIFY(server.nextPendingConnection());
QVERIFY(server.hasPendingConnections());
QVERIFY(server.nextPendingConnection());
QVERIFY(!server.hasPendingConnections());
QCOMPARE(server.nextPendingConnection(), (QTcpSocket*)0);
QVERIFY(server.waitForNewConnection(5000));
QVERIFY(server.hasPendingConnections());
QVERIFY(server.nextPendingConnection());
}
示例10: 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");
}
示例11: serverDisconnectWithBuffered
// Test buffered socket being properly closed on remote disconnect
void tst_QAbstractSocket::serverDisconnectWithBuffered()
{
QTcpServer tcpServer;
#ifndef QT_NO_SSL
QSslSocket testSocket;
#else
QTcpSocket testSocket;
#endif
QVERIFY(tcpServer.listen(QHostAddress::LocalHost));
testSocket.connectToHost(tcpServer.serverAddress(), tcpServer.serverPort());
// Accept connection on server side
QVERIFY(tcpServer.waitForNewConnection(5000));
QTcpSocket *newConnection = tcpServer.nextPendingConnection();
// Send one char and drop link
QVERIFY(newConnection != NULL);
QVERIFY(newConnection->putChar(0));
QVERIFY(newConnection->flush());
delete newConnection;
QVERIFY(testSocket.waitForConnected(5000)); // ready for write
QVERIFY(testSocket.state() == QAbstractSocket::ConnectedState);
QSignalSpy spyStateChanged(&testSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
QSignalSpy spyDisconnected(&testSocket, SIGNAL(disconnected()));
QVERIFY(testSocket.waitForReadyRead(5000)); // have one char already in internal buffer
char buf[128];
QCOMPARE(testSocket.read(buf, sizeof(buf)), Q_INT64_C(1));
if (testSocket.state() != QAbstractSocket::UnconnectedState) {
QVERIFY(testSocket.waitForDisconnected(5000));
QVERIFY(testSocket.state() == QAbstractSocket::UnconnectedState);
}
// Test signal emitting
QVERIFY(spyDisconnected.count() == 1);
QVERIFY(spyStateChanged.count() > 0);
QVERIFY(qvariant_cast<QAbstractSocket::SocketState>(spyStateChanged.last().first())
== QAbstractSocket::UnconnectedState);
}
示例12: handleIncomming
/**
* Appelé quand un client se connecte au serveur.
* Envoie la connexion au serveur si celle-ci
* n'est pas filtrée
*/
void ClientHandler::handleIncomming()
{
//On recupère le socket server
QTcpServer *server = qobject_cast<QTcpServer*>(sender());
//Si ce n'est pas un socket qui a envoyé
if (server == NULL)
{
qDebug() << "ClientHandler::handleIncomming: Erreur, socket serveur incorrect";
return;
}
//On recupère la socket
QTcpSocket *socket = server->nextPendingConnection();
//Si la socket n'existe pas, on retourne
if (socket == NULL)
{
return;
}
//On indique qu'un client s'est connecté.
qDebug() << "Nouvelle connexion en provenance de " << socket->peerAddress().toString();
//Sinon, on procède à des vérifications
//On vérifie que le nombre maximal de joueurs n'est pas atteint
if (getClientsOnline() >= _maxPlayers)
{
//On affiche un message dans la console
qDebug() << "Trop de joueurs dans le serveur.";
//On deconnecte le client
socket->close();
return;
}
//TODO: Programmer la véfification du client.
//On connecte le signal pour la deconnexion
connect(socket,SIGNAL(disconnected()),this,SLOT(handleDisconnection()));
//On envoie le signal pour dire qu'il y'a un nouveau client
emit newClient(socket);
}
示例13: out
TEST_F(SocketTest, socketReceivesIncomingDataUsingQDataStream_Test)
{
QTcpServer server;
server.listen(HOST, PORT);
_socket->connectToHost(HOST, PORT);
bool timedOut = false;
EXPECT_TRUE(server.waitForNewConnection(TCP_TIMEOUT_MS, &timedOut));
EXPECT_FALSE(timedOut);
QTcpSocket* serverSocket = server.nextPendingConnection();
EXPECT_THAT(serverSocket, NotNull());
flushEvents();
EXPECT_EQ(QAbstractSocket::ConnectedState, _socket->state());
EXPECT_EQ(QAbstractSocket::ConnectedState, serverSocket->state());
QDataStream out(serverSocket);
out << static_cast<quint32>(42);
EXPECT_TRUE(serverSocket->waitForBytesWritten(TCP_TIMEOUT_MS));
EXPECT_TRUE(_socket->ioDevice()->waitForReadyRead(TCP_TIMEOUT_MS));
quint32 i = 0;
QDataStream in(_socket->ioDevice());
in >> i;
EXPECT_EQ(static_cast<quint32>(42), i);
}
示例14: unexpectedDisconnection
void tst_QSocketNotifier::unexpectedDisconnection()
{
/*
Given two sockets and two QSocketNotifiers registered on each
their socket. If both sockets receive data, and the first slot
invoked by one of the socket notifiers empties both sockets, the
other notifier will also emit activated(). This results in
unexpected disconnection in QAbstractSocket.
The use case is that somebody calls one of the
waitFor... functions in a QSocketNotifier activated slot, and
the waitFor... functions do local selects that can empty both
stdin and stderr while waiting for fex bytes to be written.
*/
QTcpServer server;
QVERIFY(server.listen(QHostAddress::LocalHost, 0));
NATIVESOCKETENGINE readEnd1;
readEnd1.initialize(QAbstractSocket::TcpSocket);
readEnd1.connectToHost(server.serverAddress(), server.serverPort());
QVERIFY(readEnd1.waitForWrite());
QVERIFY(readEnd1.state() == QAbstractSocket::ConnectedState);
QVERIFY(server.waitForNewConnection());
QTcpSocket *writeEnd1 = server.nextPendingConnection();
QVERIFY(writeEnd1 != 0);
NATIVESOCKETENGINE readEnd2;
readEnd2.initialize(QAbstractSocket::TcpSocket);
readEnd2.connectToHost(server.serverAddress(), server.serverPort());
QVERIFY(readEnd2.waitForWrite());
QVERIFY(readEnd2.state() == QAbstractSocket::ConnectedState);
QVERIFY(server.waitForNewConnection());
QTcpSocket *writeEnd2 = server.nextPendingConnection();
QVERIFY(writeEnd2 != 0);
writeEnd1->write("1", 1);
writeEnd2->write("2", 1);
writeEnd1->waitForBytesWritten();
writeEnd2->waitForBytesWritten();
writeEnd1->flush();
writeEnd2->flush();
UnexpectedDisconnectTester tester(&readEnd1, &readEnd2);
QTimer timer;
timer.setSingleShot(true);
timer.start(30000);
do {
// we have to wait until sequence value changes
// as any event can make us jump out processing
QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
QVERIFY(timer.isActive()); //escape if test would hang
} while(tester.sequence <= 0);
QVERIFY(readEnd1.state() == QAbstractSocket::ConnectedState);
QVERIFY(readEnd2.state() == QAbstractSocket::ConnectedState);
QCOMPARE(tester.sequence, 2);
readEnd1.close();
readEnd2.close();
writeEnd1->close();
writeEnd2->close();
server.close();
}
示例15: onNewTcpConnection
void Tunnelc::onNewTcpConnection()
{
QTcpServer *tcpsrv = (QTcpServer*)sender();
QTcpSocket *sock = tcpsrv->nextPendingConnection();
int idx = this->m_tcpsrvs[tcpsrv];
ToxTunRecord rec = this->m_cfg->m_recs[idx];
QObject::connect(sock, &QTcpSocket::readyRead, this, &Tunnelc::onTcpReadyRead, Qt::QueuedConnection);
QObject::connect(sock, &QTcpSocket::disconnected, this, &Tunnelc::onTcpDisconnected, Qt::QueuedConnection);
//// channels
QString friendId = "D62E57FCBDC04080C5F78875AB24DB2AE4C797B5C5A9AC69DB5924FD972E363AF2038D5B7A44";
friendId = rec.m_remote_pubkey;
ToxTunChannel *chan = new ToxTunChannel();
chan->m_sock = sock;
chan->m_enhost = m_encli;
// chan->m_enpeer = peer;
chan->m_peer_pubkey = friendId;
chan->m_host = "127.0.0.1";
chan->m_port = rec.m_remote_port;
chan->m_conid = nextConid();
//
ENetAddress eaddr = {0};
enet_address_set_host(&eaddr, "127.0.0.1"); // tunip: 10.0.5.x
eaddr.port = 7767;
ENetPeer *peer = enet_host_connect(this->m_encli, &eaddr, 2, chan->m_port);
if (peer == NULL) {
qDebug()<<"error: can not connect to enet, maybe exceed max connection:"
<<m_encli->peerCount<<enet_host_used_peer_size(m_encli);
// cleanup and close socket
// sock->disconnect();
// this->m_sock_chans.remove(sock);
// sock->close();
}
qDebug()<<peer<<peer->connectID
<<"tol:"<<peer->timeoutLimit<<"tomin:"<<peer->timeoutMinimum
<<"tomax:"<<peer->timeoutMaximum;
// peer->timeoutLimit *= 10;
// peer->timeoutMinimum *= 10;
// peer->timeoutMaximum *= 10;
enet_peer_timeout(peer, ENET_PEER_TIMEOUT_LIMIT, ENET_PEER_TIMEOUT_MINIMUM, ENET_PEER_TIMEOUT_MAXIMUM);
enet_peer_ping_interval(peer, ENET_PEER_PING_INTERVAL);
if (peerChansCount(peer) > 0) {
qDebug()<<peer->incomingPeerID;
ToxTunChannel *tchan = peerLastChan(peer);
qDebug()<<tchan<<tchan->m_conid
<<tchan->sock_closed<<tchan->peer_sock_closed<<tchan->enet_closed;
// promiseChannelCleanup(tchan);
// 无条件把相关联的chan清理掉
if (tchan->sock_closed == true && tchan->peer_sock_closed == false) {
qDebug()<<"need force cleanup:"<<tchan->m_conid;
tchan->peer_sock_closed = true;
tchan->force_closed = true;
promiseChannelCleanup(tchan);
}
else
if (tchan->sock_closed == false && tchan->peer_sock_closed == false) {
qDebug()<<"need force cleanup:"<<tchan->m_conid;
tchan->peer_sock_closed = true;
tchan->sock_closed = true;
tchan->force_closed = true;
promiseChannelCleanup(tchan);
}
assert(peerChansCount(peer) == 0);
// assert(1 == 2);
}
peerAddChan(peer, chan);
chan->m_enpeer = peer;
this->m_sock_chans[sock] = chan;
// this->m_enpeer_chans[peer] = chan;
this->m_conid_chans[chan->m_conid] = chan;
}