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