本文整理汇总了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();
}
示例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");
}
示例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);
}
示例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;
}
示例5: newConnection
void MyServer::newConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
socket->write("Bonjour est bienvenue!\r\n");
socket->flush();
socket->waitForBytesWritten(3000);
socket->close();
}
示例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();
}
}
}
示例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();
}
示例8: readData
void TcpServer::readData()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
qDebug() << socket->readAll();
QByteArray b(10,'a');
socket->write(b);
socket->flush();
}
示例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();
}
示例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: newConnection
void ConnectServer::newConnection(){
QTcpSocket * socket = server->nextPendingConnection();
socket->write("Connected to Mp3 App\r\n");
socket->flush();
socket->waitForBytesWritten(3000);
socket->close();
}
示例12: nConnection
void TCPServer::nConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
socket->write("hello client\r\n");
socket->flush();
socket->waitForBytesWritten(3000);
socket->close();
}
示例13:
GameWindowServeur::recupConnection()
{
QTcpSocket *socket = m_server->nextPendingConnection();
socket->write("Hello client");
socket->flush();
m_clients[m_numNextClient++] = socket;
//socket->close();
}
示例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();
}
}
示例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();
}