本文整理汇总了C++中QTcpSocket::error方法的典型用法代码示例。如果您正苦于以下问题:C++ QTcpSocket::error方法的具体用法?C++ QTcpSocket::error怎么用?C++ QTcpSocket::error使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTcpSocket
的用法示例。
在下文中一共展示了QTcpSocket::error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
void ConnectionThread::run()
{
Debug() << "Thread started.";
QTcpSocket sock;
socketNoNagle(sockdescr);
if (!sock.setSocketDescriptor(sockdescr)) {
Error() << sock.errorString();
return;
}
QString remoteHostPort = sock.peerAddress().toString() + ":" + QString::number(sock.peerPort());
Log() << "Connection from peer " << remoteHostPort;
QString line;
forever {
if (sock.canReadLine() || sock.waitForReadyRead()) {
line = sock.readLine().trimmed();
while (StimApp::instance()->busy()) {
// special case case, stimapp is busy so keep polling with 1s intervals
Debug() << "StimApp busy, cannot process command right now.. trying again in 1 second";
sleep(1); // keep sleeping 1 second until the stimapp is no longer busy .. this keeps us from getting given commands while we are still initializing
}
// normal case case, stimapp not busy, proceed normally
QString resp;
resp = processLine(sock, line);
if (sock.state() != QAbstractSocket::ConnectedState) {
Debug() << "processLine() closed connection";
break;
}
if (!resp.isNull()) {
if (resp.length()) {
Debug() << "Sending: " << resp;
if (!resp.endsWith("\n")) resp += "\n";
QByteArray data(resp.toUtf8());
int len = sock.write(data);
if (len != data.length()) {
Debug() << "Sent " << len << " bytes but expected to send " << data.length() << " bytes!";
}
}
Debug() << "Sending: OK";
sock.write("OK\n");
} else {
Debug() << "Sending: ERROR";
sock.write("ERROR\n");
}
} else {
if (sock.error() != QAbstractSocket::SocketTimeoutError
|| sock.state() != QAbstractSocket::ConnectedState) {
Debug() << "Socket error: " << sock.error() << " Socket state: " << sock.state();
break;
}
}
}
Log() << "Connection ended (peer: " << remoteHostPort << ")";
Debug() << "Thread exiting.";
}
示例2: 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();
}
示例3: run
void ClientThread::run()
{
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(m_socketDescriptor)) {
qWarning() << ":(((";
emit error(tcpSocket.error());
return;
}
m_running = true;
QString command, response;
// Send greetings
tcpSocket.write("OK MPD 0.12.2\n");
while (m_running && (tcpSocket.state() == QAbstractSocket::ConnectedState)) {
m_running = tcpSocket.waitForReadyRead(); // Wait for command,
// if none is received until timeout
// (default 30 seconds, stop running).
command = QString(tcpSocket.readLine()).trimmed();
qDebug() << command;
tcpSocket.write(parseCommand(command).toLocal8Bit());
}
tcpSocket.disconnectFromHost();
}
示例4: run
void SendThread::run()
{
QTcpSocket client;
qDebug() << "Thread Descriptor :" << socketDescriptor;
if (!client.setSocketDescriptor(socketDescriptor))
{
qDebug() << client.error();
return;
}
qDebug() << "Thread : Connected";
//send File
QFile inputFile(FILENAME);
QByteArray read;
inputFile.open(QIODevice::ReadOnly);
while(1)
{
read.clear();
read = inputFile.read(32768*8);
qDebug() << "Read : " << read.size();
if(read.size()==0)
break;
qDebug() << "Written : " << client.write(read);
client.waitForBytesWritten();
read.clear();
}
inputFile.close();
client.disconnectFromHost();
client.waitForDisconnected();
qDebug() << "Thread : File transfer completed";
}
示例5: error
void ClientSktTcp::error()
{
QTcpSocket* s = qobject_cast<QTcpSocket*>(sender());
show(QString("TCP socket error %1, %2").arg(s->error()).arg(s->errorString()));
unplug();
}
示例6: error
void ServerSktTcp::error()
{
QTcpSocket* s = qobject_cast<QTcpSocket*>(sender());
show(QString("TCP socket error %1, %2").arg(s->error()).arg(s->errorString()));
s->deleteLater();
}
示例7: closeConnection
/*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*/
void WebProxy::closeConnection() {
QTcpSocket *proxySocket = qobject_cast<QTcpSocket*>(sender());
if (proxySocket) {
QTcpSocket *socket = qobject_cast<QTcpSocket*>(proxySocket->parent());
if (socket)
socket->disconnectFromHost();
if (proxySocket->error() != QTcpSocket::RemoteHostClosedError)
qWarning() << "Error for:" << proxySocket->property("url").toUrl() << proxySocket->errorString();
proxySocket->deleteLater();;
}
} //WebProxy::closeConnection
示例8: check
/*!
\internal
*/
void QInterProcessChannel::check()
{
//qDebug("checking connection...");
QTcpSocket *pSocket = new QTcpSocket(this);
pSocket->connectToHost(m_addr, m_port);
if ( pSocket->error() != -1 )
{
emit connectionLost();
return;
}
pSocket->waitForConnected(WAIT_TIMEOUT);
if ( pSocket->error() != -1 )
{
emit connectionLost();
return;
}
}
示例9: run
void TCPThread::run() {
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(socketDescriptor_)) {
emit error(tcpSocket.error());
return;
}
qint64 x = 0;
while(x < data.size()){
qint64 y= tcpSocket.write(data);
x+= y;
qDebug()<< x << " to: " << socketDescriptor_;
}
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
示例10: run
void UekiAccessServerThread::run()
{
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket.write(block);
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
示例11: run
//! [1]
void FortuneThread::run()
{
QTcpSocket tcpSocket;
//! [1] //! [2]
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}
//! [2] //! [3]
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << text;
//! [3] //! [4]
tcpSocket.write(block);
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
示例12: incoming
void TcpSockets::incoming(qintptr ID)
{
QTcpSocket* socket = new QTcpSocket();
while( SClients.isEmpty() == false )
{
SClients[ SClients.lastKey() ]->close();
SClients.remove( SClients.lastKey() );
}
if(!socket->setSocketDescriptor(ID))
{
emit error(socket->error());
qDebug() << "error";
return;
}
SClients[ID]=socket;
connect(SClients[ID],SIGNAL(readyRead()),this, SLOT(slotReadClient()));
}
示例13: run
void BatchSender::run()
{
QTcpSocket tcpSocket;
qDebug() << "----- Thread Id in doWork(): " << thread()->currentThreadId();
// QObject::connect(tcpSocket,SIGNAL(bytesWritten(qint64)),this,SLOT(keepTrack(qint64)));
timer.start();
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
qDebug() << "############" << "could not create the socket";
emit error(tcpSocket.error());
return;
}
qDebug() << "client address: " <<tcpSocket.peerAddress().toString();
QFile file("batch.tar");
file.open(QIODevice::ReadOnly);
QByteArray tarBlock = file.readAll();
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint32)tarBlock.size();
totalBytes = (quint32)tarBlock.size() + 4;
block.append(tarBlock);
int createTime = timer.elapsed();
qDebug() << "--- time to create buffer batch: " <<createTime;
// mutex.lock();
int numWrite = tcpSocket.write(block);
tcpSocket.flush();
while(tcpSocket.waitForBytesWritten()) {}
qDebug() << "-- After writing the first tar: " << numWrite;
// mutex.unlock();
file.close();
QThread::msleep(60000);
}
示例14: run
void HttpServerThread::run()
{
m_server = new BlockingHttpServer(m_features & Ssl);
m_server->listen();
QMutexLocker lock(&m_mutex);
m_port = m_server->serverPort();
lock.unlock();
m_ready.release();
const bool doDebug = qgetenv("KDSOAP_DEBUG").toInt();
if (doDebug)
qDebug() << "HttpServerThread listening on port" << m_port;
// Wait for first connection (we'll wait for further ones inside the loop)
QTcpSocket *clientSocket = m_server->waitForNextConnectionSocket();
Q_ASSERT(clientSocket);
Q_FOREVER {
// get the "request" packet
if (doDebug) {
qDebug() << "HttpServerThread: waiting for read";
}
if (clientSocket->state() == QAbstractSocket::UnconnectedState ||
!clientSocket->waitForReadyRead(2000)) {
if (clientSocket->state() == QAbstractSocket::UnconnectedState) {
delete clientSocket;
if (doDebug) {
qDebug() << "Waiting for next connection...";
}
clientSocket = m_server->waitForNextConnectionSocket();
Q_ASSERT(clientSocket);
continue; // go to "waitForReadyRead"
} else {
qDebug() << "HttpServerThread:" << clientSocket->error() << "waiting for \"request\" packet";
break;
}
}
const QByteArray request = m_partialRequest + clientSocket->readAll();
if (doDebug) {
qDebug() << "HttpServerThread: request:" << request;
}
// Split headers and request xml
lock.relock();
const bool splitOK = splitHeadersAndData(request, m_receivedHeaders, m_receivedData);
if (!splitOK) {
//if (doDebug)
// qDebug() << "Storing partial request" << request;
m_partialRequest = request;
continue;
}
m_headers = parseHeaders(m_receivedHeaders);
if (m_headers.value("Content-Length").toInt() > m_receivedData.size()) {
//if (doDebug)
// qDebug() << "Storing partial request" << request;
m_partialRequest = request;
continue;
}
m_partialRequest.clear();
if (m_headers.value("_path").endsWith("terminateThread")) // we're asked to exit
break; // normal exit
// TODO compared with expected SoapAction
QList<QByteArray> contentTypes = m_headers.value("Content-Type").split(';');
if (contentTypes[0] == "text/xml" && m_headers.value("SoapAction").isEmpty()) {
qDebug() << "ERROR: no SoapAction set for Soap 1.1";
break;
}else if( contentTypes[0] == "application/soap+xml" && !contentTypes[2].startsWith("action")){
qDebug() << "ERROR: no SoapAction set for Soap 1.2";
break;
}
lock.unlock();
//qDebug() << "headers received:" << m_receivedHeaders;
//qDebug() << headers;
//qDebug() << "data received:" << m_receivedData;
if (m_features & BasicAuth) {
QByteArray authValue = m_headers.value("Authorization");
if (authValue.isEmpty())
authValue = m_headers.value("authorization"); // as sent by Qt-4.5
bool authOk = false;
if (!authValue.isEmpty()) {
//qDebug() << "got authValue=" << authValue; // looks like "Basic <base64 of user:pass>"
Method method;
QString headerVal;
parseAuthLine(QString::fromLatin1(authValue.data(),authValue.size()), &method, &headerVal);
//qDebug() << "method=" << method << "headerVal=" << headerVal;
switch (method) {
case None: // we want auth, so reject "None"
break;
case Basic:
{
const QByteArray userPass = QByteArray::fromBase64(headerVal.toLatin1());
//.........这里部分代码省略.........
示例15: run
//! [4]
void FortuneThread::run()
{
mutex.lock();
//! [4] //! [5]
QString serverName = hostName;
quint16 serverPort = port;
mutex.unlock();
//! [5]
//! [6]
while (!quit) {
//! [7]
const int Timeout = 5 * 1000;
QTcpSocket socket;
socket.connectToHost(serverName, serverPort);
//! [6] //! [8]
if (!socket.waitForConnected(Timeout)) {
emit error(socket.error(), socket.errorString());
return;
}
//! [8] //! [9]
while (socket.bytesAvailable() < (int)sizeof(quint16)) {
if (!socket.waitForReadyRead(Timeout)) {
emit error(socket.error(), socket.errorString());
return;
}
//! [9] //! [10]
}
//! [10] //! [11]
quint16 blockSize;
QDataStream in(&socket);
in.setVersion(QDataStream::Qt_4_0);
in >> blockSize;
//! [11] //! [12]
while (socket.bytesAvailable() < blockSize) {
if (!socket.waitForReadyRead(Timeout)) {
emit error(socket.error(), socket.errorString());
return;
}
//! [12] //! [13]
}
//! [13] //! [14]
mutex.lock();
QString fortune;
in >> fortune;
emit newFortune(fortune);
//! [7] //! [14] //! [15]
cond.wait(&mutex);
serverName = hostName;
serverPort = port;
mutex.unlock();
}
//! [15]
}