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(); }
void CommunicationTcpSocketServer::newConnection() { QTcpSocket *newSocket = serverSocket.nextPendingConnection(); if (newSocket) { if (newSocket->isOpen()) { connect(newSocket, SIGNAL(disconnected()), this, SLOT(disconnected())); } setSocket(newSocket); qWarning() << "Új kapcsolat létesült.\n"; QByteArray ba; ba[0]=0x00; ba[1]=0x55; ba[2]=100; ba[3]=122; send(ba); ba[0]=0x00; ba[1]=0xAA; ba[2]=128; ba[3]=14; send(ba); ba[0]=0x00; ba[1]=0xCB; ba[2]=15; ba[3]=192; send(ba); } }
void TCPReceiveRawNode::frameStart( qint64 pTimeStamp ) { if( !mStream ) { return; } QTcpSocket *S = qobject_cast<QTcpSocket *>( mStream->device() ); if( !S->isOpen() ) { delete mStream; delete S; mStream = nullptr; return; } if( !S->bytesAvailable() ) { return; } fugio::Performance Perf( mNode, "frameStart", pTimeStamp ); mValOutputBuffer->setVariant( S->readAll() ); pinUpdated( mPinOutputBuffer ); }
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(); } }
int main(int argc, char** argv) { QCoreApplication app(argc, argv); const QStringList args = app.arguments(); QString arg_port; QString arg_server; QString arg_xmlFile; bool arg_crash = false; bool arg_garbage = false; uint arg_wait = 0; const QProcessEnvironment sysEnv = QProcessEnvironment::systemEnvironment(); arg_xmlFile = sysEnv.value("QCIT_INPUT_FILE"); for (int i = 1; i < args.size(); ++i) { const QString& arg = args.at(i); if (arg.startsWith(QLatin1String("--xml-socket="))) { arg_server = arg.mid(13, arg.indexOf(':') - 13); arg_port = arg.mid(13 + arg_server.length() + 1); } else if (args.size() > i + 1 && (args.at(i) == QLatin1String("-i") || args.at(i) == QLatin1String("--xml-input"))) { arg_xmlFile = args.at(i+1); ++i; } else if (arg == QLatin1String("-c") || arg == QLatin1String("--crash")) { arg_crash = true; } else if (arg == QLatin1String("-g") || arg == QLatin1String("--garbage")) { arg_garbage = true; } else if (args.size() > i + 1 && (arg == QLatin1String("-w") || arg == QLatin1String("--wait"))) { bool ok; arg_wait = args.at(i+1).toUInt(&ok); if (!ok) { qerr << "ERROR: invalid wait time given" << args.at(i+1) << endl; usage(qerr); return 4; } } else if (args.at(i) == QLatin1String("--help") || args.at(i) == QLatin1String("-h")) { usage(qout); return 0; } } if (arg_xmlFile.isEmpty()) { qerr << "ERROR: no XML input file given" << endl; usage(qerr); return 1; } if (arg_server.isEmpty()) { qerr << "ERROR: no server given" << endl; usage(qerr); return 2; } if (arg_port.isEmpty()) { qerr << "ERROR: no port given" << endl; usage(qerr); return 3; } QFile xmlFile(arg_xmlFile); if (!xmlFile.exists() || !xmlFile.open(QIODevice::ReadOnly)) { qerr << "ERROR: invalid XML file" << endl; usage(qerr); return 10; } bool ok = false; quint16 port = arg_port.toUInt(&ok); if (!ok) { qerr << "ERROR: invalid port" << endl; usage(qerr); return 30; } QTcpSocket socket; socket.connectToHost(arg_server, port, QIODevice::WriteOnly); if (!socket.isOpen()) { qerr << "ERROR: could not open socket to server:" << arg_server << ":" << port << endl; usage(qerr); return 20; } if (!socket.waitForConnected()) { qerr << "ERROR: could not connect to socket: " << socket.errorString() << endl; return 21; } OutputGenerator generator(&socket, &xmlFile); QObject::connect(&generator, SIGNAL(finished()), &app, SLOT(quit())); generator.setCrashRandomly(arg_crash); generator.setOutputGarbage(arg_garbage); generator.setWait(arg_wait); return app.exec(); }
void CHttpServer::readClient(void) { QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender()); CTcpRequestEvent* event_ = new CTcpRequestEvent(); CHttpHeader header; try { if(isPaused()) throw("output disabled"); if(socket->bytesAvailable() <= 0 && socket->bytesAvailable() >= (1 << 20)) // hard limit of 1MB input throw("Illegal request size."); if(!socket->canReadLine()) throw("Invalid request"); if(!header.read(socket)) return; switch(hashed_t(header.method())) { // HTTP 1.0 case "GET"_hash: if(header.path() == "/favicon.ico") { static QByteArray favicon; if(favicon.isEmpty()) favicon = CFileCache::get(1); socket->write("HTTP/1.1 200 OK\r\n" "Content-Type: image/png\r\n" "Connection: close\r\n\r\n"); socket->write(favicon); // favicon throw("favicon"); } if(header.path() == "/") { socket->write("HTTP/1.1 301 Moved Permanently\r\n" "Location: /Main_Page\r\n"); throw("forward to Main_Page"); } if(header.path().contains("%20")) { QByteArray path = header.path(); path = path.replace("%20", "_"); socket->write("HTTP/1.1 301 Moved Permanently\r\nLocation: "); socket->write(path); socket->write("\r\n"); throw("forward to proper page name"); } break; case "HEAD"_hash: break; case "POST"_hash: break; // HTTP 1.1 case "OPTIONS"_hash: break; case "PUT"_hash: break; case "DELETE"_hash: break; case "TRACE"_hash: break; case "CONNECT"_hash: break; case "PATCH"_hash: break; // HTTP 2.x (?) case "MOVE"_hash: break; case "PURGE"_hash: break; case "REFRESH"_hash: break; // WebDAV // HTTP 1.1 case "PROPFIND"_hash: break; case "PROPPATCH"_hash: break; case "MKCOL"_hash: break; case "COPY"_hash: break; case "LOCK"_hash: break; case "UNLOCK"_hash: break; // HTTP 2.x case "VERSION-CONTROL"_hash: break; case "REPORT"_hash: break; case "CHECKIN"_hash: break; case "CHECKOUT"_hash: break; case "UNCHECKOUT"_hash: break; case "UPDATE"_hash: break; case "MKWORKSPACE"_hash: break; case "LABEL"_hash: break; case "MERGE"_hash: break; case "BASELINE-CONTROL"_hash: break; case "MKACTIVITY"_hash: break; case "BIND"_hash: break; case "SEARCH"_hash: break; default: qDebug() << "unknown header type"; break; } event_->setHeader(header); event_->setSocket(socket); QCoreApplication::postEvent(qApp, event_, Qt::RealTimeEventPriority); // set network request event QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::X11ExcludeTimers); // process the network request event QtServiceBase::instance()->logMessage("Wrote to client"); } catch(const char* error_message) { QtServiceBase::instance()->logMessage(error_message); } if(socket->isOpen()) socket->close(); if(socket->state() == QTcpSocket::UnconnectedState) { delete socket; QtServiceBase::instance()->logMessage("Connection closed"); } }