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


C++ QTcpSocket::flush方法代码示例

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


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

示例1: run

void lsLogServitemThreated::run()
{
    QTcpSocket tcpSocket;

    if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
        emit error(tcpSocket.error());
        return;
    }

    while ( tcpSocket.isOpen() )
    {
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_4_0);
        out << (quint16)0;
        out << text;
        text.clear();
        out.device()->seek(0);
        out << (quint16)(block.size() - sizeof(quint16));

        tcpSocket.write(block);
        tcpSocket.flush();

        while ( tcpSocket.isOpen() && text.isEmpty() )
        {
            msleep(100);
        }
    }

    tcpSocket.disconnectFromHost();
    tcpSocket.waitForDisconnected();
}
开发者ID:audioprog,项目名称:UsbCopyManager,代码行数:32,代码来源:lsLogServitemThreated.cpp

示例2: process

/* perform the operations for a frame:
 *   - check to see if the connections are still alive (checkAlive)
 *      - this will emit connectionsChanged if there is a any change in the connection status
 *   - grab a text stream of the current model data ( stream << *subjectList )
 *   - put the text stream on the wire s->write(...)
*/
void MyServer::process()
{
    stopProfile("Other");

    working = true;

    startProfile("checkAlive");
    int alive = checkAlive();
    stopProfile("checkAlive");

    if(alive > 0)
    {
        startProfile("Serve");

        count++;
        QString buffer;
        QTextStream stream(&buffer);
        // The following operation is threadsafe.
        startProfile("Fetch");
        subjectList->read(stream, true);
        stopProfile("Fetch");

        startProfile("Wait");
        listMutex.lock();
        stopProfile("Wait");

        // for each connection
        for(QList<ServerConnection *>::iterator i =  connections.begin(); i != connections.end(); i++)
        {
            QTcpSocket *s = (*i)->socket;
            if(s->state() != QAbstractSocket::ConnectedState) continue;

            QString d = QString("%1\nEND\r\n").arg(buffer);

            startProfile("Write");
            int written = s->write(d.toUtf8());
            stopProfile("Write");

            if(written == -1)
            {
                emit outMessage(QString(" Error writing to %1").arg(s->peerAddress().toString()));
            }
            else
            {
                s->flush();
            }
        }

        listMutex.unlock();

        stopProfile("Serve");
    }

    working = false;


    startProfile("Other");


}
开发者ID:otri,项目名称:vive,代码行数:66,代码来源:server.cpp

示例3: connectToMaster

void ProtoConnect::connectToMaster(QString ipAddress, int serverPort, ProtocolHandler * protocolHandler){

    QTcpSocket *socket = new QTcpSocket(this);

    //making sure it is disconnectedclose();

    socket->connectToHost(ipAddress, serverPort);

    //wait for one second for connection
    if(!socket->waitForConnected(1000)){
        //error
        qDebug()<<"Error when connecting";
        return;
    }

    SocketClient *socketClient = new SocketClient(protocolHandler, socket);

    //connect(socket, SIGNAL(connected()), socketClient, SLOT(connected()));
    connect(socket, SIGNAL(readyRead()), socketClient, SLOT(readyRead()));
    connect(socket, SIGNAL(disconnected()), socketClient, SLOT(disconnected()));

    //write to the server to connect
    socket->write("||Hello AppMan||");
    socket->flush();

    //finally set the socket so that the network can use it
    protocolHandler->setSocket(socket);
}
开发者ID:hhvvkk,项目名称:COS301DistrobutedApplicationManger,代码行数:28,代码来源:protoconnect.cpp

示例4: streamLoop

void streamLoop(qintptr socketDesc, QQueue<QByteArray> &queue, bool& streaming) {

    QTcpSocket* socket = new QTcpSocket();
    // TCP_NODELAY + disable Nagle's algorithm
    socket->setSocketOption(QAbstractSocket::LowDelayOption, QVariant::fromValue(1));
    // Internetwork control
    socket->setSocketOption(QAbstractSocket::TypeOfServiceOption, QVariant::fromValue(192));
    socket->setSocketDescriptor(socketDesc);
    socket->readAll();

    QByteArray ContentType = ("HTTP/1.1 200 OK\r\n" \
                              "Server: test\r\n" \
                              "Cache-Control: no-cache\r\n" \
                              "Cache-Control: private\r\n" \
                              "Connection: close\r\n"\
                              "Pragma: no-cache\r\n"\
                              "Content-Type: multipart/x-mixed-replace; boundary=--boundary\r\n\r\n");

    socket->write(ContentType);

    while((socket->state() != QAbstractSocket::ClosingState ||
           socket->state() != QAbstractSocket::UnconnectedState) &&
           socket->state() == QAbstractSocket::ConnectedState &&
           streaming) {

        if(queue.empty()) { // no new frame available
            continue;
        }

        // make sure that the queue doesn't grow too big or
        // the OOM killer will kick in
        if(queue.length() > 20) {
            queue.clear();
            continue;
        }

        QByteArray boundary = ("--boundary\r\n" \
                               "Content-Type: image/jpeg\r\n" \
                               "Content-Length: ");

        QByteArray img  = queue.dequeue();
        boundary.append(QString::number(img.length()));
        boundary.append("\r\n\r\n");

        socket->write(boundary);
        socket->waitForBytesWritten();
        boundary.clear();
        socket->write(img);
        socket->waitForBytesWritten();
        img.clear();
    }

    socket->flush();
    socket->abort();
    socket->deleteLater();
    streaming = false;
    queue.clear();
    return;
}
开发者ID:kimmoli,项目名称:SailCast,代码行数:59,代码来源:screenprovider.cpp

示例5: newConnection

void MyServer::newConnection()
{
    QTcpSocket *socket = server->nextPendingConnection();

    socket->write("Bonjour est bienvenue!\r\n");
    socket->flush();
    socket->waitForBytesWritten(3000);
    socket->close();
}
开发者ID:konovagit,项目名称:IHM_Reflow_Oven,代码行数:9,代码来源:myserver.cpp

示例6: incomingConnection

void ConnectionListener::incomingConnection(qintptr socketDescriptor)
{
    if (m_accept) {
        emit log("Listener", "New connection: accepted.");
        m_accept = false;
        emit clientSuccessfullyConnected();

        m_proxy.reset(new Proxy(m_mapData,
                                m_pathMachine,
                                m_prespammedPath,
                                m_groupManager,
                                m_mumeClock,
                                m_mapCanvas,
                                socketDescriptor,
                                this));

        if (getConfig().connection.proxyThreaded) {
            m_thread.reset(new QThread);
            m_proxy->moveToThread(m_thread.get());

            // Proxy destruction stops the thread which then destroys itself on completion
            connect(m_proxy.get(), &QObject::destroyed, m_thread.get(), &QThread::quit);
            connect(m_thread.get(), &QThread::finished, m_thread.get(), &QObject::deleteLater);
            connect(m_thread.get(), &QObject::destroyed, this, [this]() {
                m_accept = true;
                m_proxy.release();
                m_thread.release();
            });

            // Make sure if the thread is interrupted that we kill the proxy
            connect(m_thread.get(), &QThread::finished, m_proxy.get(), &QObject::deleteLater);

            // Start the proxy when the thread starts
            connect(m_thread.get(), &QThread::started, m_proxy.get(), &Proxy::start);
            m_thread->start();

        } else {
            connect(m_proxy.get(), &QObject::destroyed, this, [this]() {
                m_accept = true;
                m_proxy.release();
            });
            m_proxy->start();
        }

    } else {
        emit log("Listener", "New connection: rejected.");
        QTcpSocket tcpSocket;
        if (tcpSocket.setSocketDescriptor(socketDescriptor)) {
            QByteArray ba("\033[1;37;41mYou can't connect to MMapper more than once!\r\n"
                          "Please close the existing connection.\033[0m\r\n");
            tcpSocket.write(ba);
            tcpSocket.flush();
            tcpSocket.disconnectFromHost();
            tcpSocket.waitForDisconnected();
        }
    }
}
开发者ID:MUME,项目名称:MMapper,代码行数:57,代码来源:connectionlistener.cpp

示例7: processNewDevice

void ServerCore::processNewDevice(QVariantMap& dataMap)
{
	bool isSelfBroadcast = dataMap.contains(JSON_KEY_COMMAND) && dataMap[JSON_KEY_COMMAND] == CMD_QUERY
		&& (!dataMap.contains(JSON_KEY_UID) || !dataMap.contains(JSON_KEY_DISPLAYNAME)
			|| !dataMap.contains(JSON_KEY_IP));

	if (isSelfBroadcast)
	{
		return;
	}

	QString deviceUID = dataMap[JSON_KEY_UID].toString();
	QString deviceName = dataMap[JSON_KEY_DISPLAYNAME].toString();
	QString deviceIP = dataMap[JSON_KEY_IP].toString();
	QString deviceType = dataMap[JSON_KEY_TYPE].toString();

	DeviceInfo devInfo(deviceUID, deviceName, deviceIP, deviceType);
	DeviceManagerModule()->AddDevice(deviceUID, devInfo);

	QVariantMap paramMap;
	paramMap.insert(JSON_KEY_SEQ, 1234);

	QTcpSocket *socket = new QTcpSocket(this);

	QObject::connect(socket, &QTcpSocket::readyRead, [=]
	{
		QByteArray byteArray = socket->readAll();
		parseSupportCmds(deviceUID, byteArray);
		Q_EMIT deviceAdded(deviceUID);
		socket->disconnectFromHost();
	});

	QObject::connect(socket, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error),
		[=](QAbstractSocket::SocketError socketError)
	{
		qDebug() << "Socket Error" << socketError;
	});

	QObject::connect(socket, &QTcpSocket::disconnected, [=]
	{
		socket->deleteLater();
	});

	socket->connectToHost(deviceIP, PORT_SERVER_BROADCAST, QIODevice::ReadWrite);

	socket->waitForConnected();

	QString jsonCmd = JsonGenerator::GenerateJsonCommand(TCP_COMMAND_TYPE::QUERY_DEVICE_SUPPORT_CMDS, paramMap);

	socket->write(jsonCmd.toLatin1());
	socket->flush();

	socket->waitForBytesWritten();
	socket->waitForReadyRead();

	
}
开发者ID:cyril0108,项目名称:samidevice,代码行数:57,代码来源:servercore.cpp

示例8: readData

void TcpServer::readData()
{
    QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());

    qDebug() << socket->readAll();

    QByteArray b(10,'a');
    socket->write(b);
    socket->flush();
}
开发者ID:DemoHn,项目名称:ss-libQ,代码行数:10,代码来源:tcpserver.cpp

示例9: sendData

bool NetworkManager::sendData(int connectionID, const unsigned char* data, int size)
{
    if(connectionID < 0)
        return false;

    QTcpSocket* socket = mConnections[connectionID];
    socket->write((const char*)data, size);

    return socket->flush();
}
开发者ID:Mikycrazy,项目名称:pb173-project,代码行数:10,代码来源:NetworkManager.cpp

示例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");
}
开发者ID:husninazer,项目名称:qt,代码行数:43,代码来源:tst_qtcpserver.cpp

示例11: newConnection

void ConnectServer::newConnection(){


    QTcpSocket * socket = server->nextPendingConnection();
    socket->write("Connected to Mp3 App\r\n");
    socket->flush();

    socket->waitForBytesWritten(3000);
    socket->close();

}
开发者ID:JDLowe7,项目名称:Mp3Search,代码行数:11,代码来源:ConnectServer.cpp

示例12: nConnection

void TCPServer::nConnection()
{
    QTcpSocket *socket = server->nextPendingConnection();

    socket->write("hello client\r\n");
    socket->flush();

    socket->waitForBytesWritten(3000);

    socket->close();
}
开发者ID:Ladeia,项目名称:SD,代码行数:11,代码来源:tcpserver.cpp

示例13:

GameWindowServeur::recupConnection()
{
    QTcpSocket *socket = m_server->nextPendingConnection();

    socket->write("Hello client");
    socket->flush();

    m_clients[m_numNextClient++] = socket;

    //socket->close();
}
开发者ID:Kyiio,项目名称:TP4,代码行数:11,代码来源:gamewindowserveur.cpp

示例14: sendMessage

void QtNetworkPeerPrivate::sendMessage(const QByteArray &message)
{
    qDebug() << "sendMessage" << message;

    foreach (const QHostAddress &address, m_acceptedOutboundConnections.keys()) {
        qDebug() << "sendMessage" << address;
        QTcpSocket *socket = m_acceptedOutboundConnections.value(address);
        qDebug() << "socket" << socket->isValid() << socket->isOpen();
        socket->write(message);
        socket->flush();
    }
}
开发者ID:msorvig,项目名称:qtnetworkpeer,代码行数:12,代码来源:qtnetworkpeer_p.cpp

示例15: run

void TaskForTcpServer::run()
{
    if(!socketDescriptor) return;

        QTcpSocket socket;
        socket.setSocketDescriptor(socketDescriptor);
        //to do

        socket.write("From server: hello world");
        socket.flush();
        socket.waitForBytesWritten();
        socket.close();
}
开发者ID:vadimG2015i,项目名称:Cplus,代码行数:13,代码来源:taskfortcpserver.cpp


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