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


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

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


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

示例1: 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);
}
开发者ID:venkatarajasekhar,项目名称:Qt,代码行数:40,代码来源:tst_qabstractsocket.cpp

示例2: startTests

void LockTestClient::startTests()
{
    QTcpSocket socket;
    socket.connectToHost ( "localhost", 55555);
    char control;
    
    #define GetNextCommand()      if(socket.waitForReadyRead (-1) )\
                                                        {\
                                                            if(socket.bytesAvailable() > 1)\
                                                                qDebug()<<"Something is wrong here";\
                                                            socket.getChar(&control);\
                                                            if(control == 'a')\
                                                            {\
                                                                socket.disconnectFromHost();\
                                                                return;\
                                                            }\
                                                            if(control != 'n')\
                                                            { \
                                                                 qDebug()<<"Wrong control command";\
                                                            }\
                                                        }
    
    if(socket.waitForConnected (-1))
    {
        QFile file("lock.file");
        
        if(!file.open(QIODevice::ReadWrite))
        {
            qDebug()<<"Could not open lockfile";
            return;
        }
        
        if(1)
        {
            GetNextCommand();
            //Trying to readlock the same region
            QxtFileLock lock(&file,0x10,20,QxtFileLock::ReadLock);
            if(lock.lock())
                socket.putChar('s');    //s for success f for fail
            else
                socket.putChar('f');
            socket.waitForBytesWritten(-1);
        }
        
        if(1)
        {
            GetNextCommand();
             //Trying to lock the same region with different locks
            QxtFileLock lock(&file,0x10,20,QxtFileLock::WriteLock); 
            
            if(!lock.lock())
                socket.putChar('s');    //s for success f for fail
            else
                socket.putChar('f');
            socket.waitForBytesWritten(-1);
        }
        
        if(1)
        {
            GetNextCommand();
             //Trying to writelock the same region
            QxtFileLock lock(&file,0x10,20,QxtFileLock::WriteLock); 
            
            if(!lock.lock())
                socket.putChar('s');    //s for success f for fail
            else
                socket.putChar('f');
            socket.waitForBytesWritten(-1);
        }
        
        if(1)
        {
            GetNextCommand();
             //Trying to writelock different regions
            QxtFileLock lock(&file,0x10+21,20,QxtFileLock::WriteLock); 
            
            if(lock.lock())
                socket.putChar('s');    //s for success f for fail
            else
                socket.putChar('f');
            socket.waitForBytesWritten(-1);
        }
        
    }
}
开发者ID:develnk,项目名称:qxtweb-qt5,代码行数:85,代码来源:locktestclient.cpp


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