本文整理汇总了C++中QTcpSocket::waitForConnected方法的典型用法代码示例。如果您正苦于以下问题:C++ QTcpSocket::waitForConnected方法的具体用法?C++ QTcpSocket::waitForConnected怎么用?C++ QTcpSocket::waitForConnected使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTcpSocket
的用法示例。
在下文中一共展示了QTcpSocket::waitForConnected方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: connectToHost
bool ImapPrivate::connectToHost (const QString& host, quint16 port, bool useSsl)
{
#ifndef QT_NO_OPENSSL
if (useSsl)
socket = new QSslSocket;
else
socket = new QTcpSocket;
#else
Q_UNUSED(useSsl)
socket = new QTcpSocket;
#endif
#ifndef QT_NO_OPENSSL
if (useSsl) {
QSslSocket *sslSocket = static_cast<QSslSocket *>(socket);
sslSocket->connectToHostEncrypted(host, port);
return(sslSocket->waitForEncrypted());
} else {
socket->connectToHost(host, port);
return(socket->waitForConnected());
}
#else
socket->connectToHost(host, port);
return(socket->waitForConnected());
#endif
}
示例2: connectOne
void ConnectTrd::connectOne(QString ip)
{
QTcpSocket *socket = new QTcpSocket();
connect(socket,SIGNAL(readyRead()),this,SLOT(canRead()));
socket->connectToHost("192.168.1.240",AKHMI_PORT);
socket->waitForConnected();
qDebug()<< socket->waitForConnected()<< socket->errorString();
}
示例3: internetStatus
//-----------------------------------------------------------------------------
void
QGCCacheWorker::_testInternet()
{
/*
To test if you have Internet connection, the code tests a connection to
8.8.8.8:53 (google DNS). It appears that some routers are now blocking TCP
connections to port 53. So instead, we use a TCP connection to "github.com"
(80). On exit, if the look up for “github.com” is under way, a call to abort
the lookup is made. This abort call on Android has no effect, and the code
blocks for a full minute. So to work around the issue, we continue a direct
TCP connection to 8.8.8.8:53 on Android and do the lookup/connect on the
other platforms.
*/
#if defined(__android__)
QTcpSocket socket;
socket.connectToHost("8.8.8.8", 53);
if (socket.waitForConnected(2000)) {
qCDebug(QGCTileCacheLog) << "Yes Internet Access";
emit internetStatus(true);
return;
}
qWarning() << "No Internet Access";
emit internetStatus(false);
#else
if(!_hostLookupID) {
_hostLookupID = QHostInfo::lookupHost("www.github.com", this, SLOT(_lookupReady(QHostInfo)));
}
#endif
}
示例4: 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);
}
示例5: process
void FetchThread::process(QString phost)
{
QUdpSocket *udpSocket ;
udpSocket= new QUdpSocket(0);
udpSocket->bind(QHostAddress::LocalHost, 9999);
udpSocket->waitForConnected(250);
QTcpSocket socket;
socket.connectToHost("localhost", 4949);
socket.waitForConnected(500);
while (socket.waitForReadyRead(250));
QString t_hello = socket.readAll();
qDebug() << "READ: " << t_hello;
socket.write(QString("list\n").toStdString().c_str() );
while (socket.waitForReadyRead(250));
QString buf1 = socket.readAll();
qDebug() << "READ: " << buf1;
QStringList list_probe = buf1.split(QRegExp("\\s+"));
for (int z=0; z< list_probe.size(); z++)
{
QString probe=list_probe.at(z);
QString cmd = QString("fetch ").append(probe).append("\n");
qDebug() << "cmd : " << cmd;
socket.write(cmd.toStdString().c_str() );
while (socket.waitForReadyRead(250));
QString buf2 = socket.readAll();
qDebug() << "Rep fetch :" << buf2 << "\n";
QRegularExpression re("(\\w+).(\\w+) ([0-9.]+)\\n");
QRegularExpressionMatchIterator i = re.globalMatch(buf2);
re.setPatternOptions(QRegularExpression::MultilineOption);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString s_metric = match.captured(1);
QString s_value = match.captured(3);
QString s_mtr = "monit2influxdb,metric="+probe + "_" + s_metric + ",host=" + phost+ " value=" + s_value + " " + QString::number(1000000* QDateTime::currentMSecsSinceEpoch());
qDebug() << "metric: " << s_mtr.toLower();
udpSocket->writeDatagram(s_mtr.toStdString().c_str(), QHostAddress::LocalHost, 9999);
}
udpSocket->close();
}
}
示例6: run
void ThreadJoueurs::run()
{
QTcpSocket unSocket;
unSocket.setSocketDescriptor(m_socketDescriptor);
if(unSocket.waitForConnected(1000))
{
while(unSocket.ConnectedState) //.waitForReadyRead(1000))
{
baRXInfos=unSocket.read(unSocket.bytesAvailable());
if(baRXInfos.left(1) == "&") //code de connection des joueurs
{
unSocket.write(baTXInfos.append(cNoJ)); //assignation du numero
baTXInfos = TXInfosToJoueurs(m_tNouvellePartie,9); // trame de debut de partie (= NouvellePartie)
}
else
{
RXInfosFmJoueurs(baRXInfos); //recoit {'#', JnX, JnY}
baTXInfos = TXInfosToJoueurs(m_txInfos,9); //repond trame {code, balle X, balle Y, J1X, J1Y, J2X, J2Y, ScoreA, ScoreB}
} // code = '#' (normale), '$' (gagnant), '%' (Nouvelle Balle)
unSocket.write(baTXInfos);
unSocket.waitForBytesWritten(10);
}
}
unSocket.disconnectFromHost();
unSocket.close();
}
示例7: kDebug
NavigationWidget::NavigationWidget(KDevelop::DeclarationPointer declaration, KDevelop::TopDUContextPointer topContext, const QString& /* htmlPrefix */, const QString& /* htmlSuffix */)
{
kDebug() << "Navigation widget for Declaration requested";
m_topContext = topContext;
initBrowser(400);
DeclarationNavigationContext* context = new DeclarationNavigationContext(declaration, m_topContext);
m_startContext = context;
setContext(m_startContext);
m_fullyQualifiedModuleIdentifier = context->m_fullyQualifiedModuleIdentifier;
kDebug() << "Identifier: " << m_fullyQualifiedModuleIdentifier;
if ( m_fullyQualifiedModuleIdentifier.length() ) {
kDebug() << "Checking wether doc server is running";
QTcpSocket* sock = new QTcpSocket();
sock->connectToHost(QHostAddress::LocalHost, 1050, QTcpSocket::ReadOnly);
bool running = sock->waitForConnected(300);
if ( ! running ) {
kDebug() << "Not running, starting pydoc server";
QProcess::startDetached("/usr/bin/env", QStringList() << "python" << QString(INSTALL_PATH) + "/pydoc.py" << "-p" << "1050");
usleep(100000); // give pydoc server 100ms to start up
}
else {
sock->disconnectFromHost();
}
delete sock;
m_documentationWebView = new QWebView(this);
m_documentationWebView->load(QUrl("http://localhost:1050/" + m_fullyQualifiedModuleIdentifier + ".html"));
connect( m_documentationWebView, SIGNAL(loadFinished(bool)), SLOT(addDocumentationData(bool)) );
}
}
示例8: run
void mjpegThread::run()
{
QTcpSocket *socket = new QTcpSocket(this);
connect(socket, SIGNAL(connected()),this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
QUrl url(cfg->get("src1/url"), QUrl::TolerantMode);
socket->connectToHost(url.host(), 80);
if(!socket->waitForConnected(5000))
{
qDebug() << "Error: " << socket->errorString();
}
while (fRun)
{
sleep(1);
}
socket->close();
}
示例9: SendData
bool Server::SendData(QHostAddress ip_to, QString message)
{
bool result = false;
QTcpSocket* tcpSocket = sockets->value(ip_to.toIPv4Address(), 0);
if (tcpSocket && tcpSocket->state()==QAbstractSocket::ConnectedState && tcpSocket->isWritable()) {
emit write_message(tr("Sending data (size=%1) to %2. Content: \"%3\"").arg(message.length()).arg(ip_to.toString()).arg(message));
tcpSocket->write(message.toUtf8());
result = tcpSocket->waitForBytesWritten();
} else {
tcpSocket = new QTcpSocket();
tcpSocket->connectToHost(ip_to, remote_port, QIODevice::ReadWrite);
tcpSocket->waitForConnected(5000);
if (tcpSocket->state()==QAbstractSocket::ConnectedState) {
emit write_message(tr("Sending data (size=%1) from new socket to %2. Content: \"%3\"").arg(message.length()).arg(ip_to.toString()).arg(message));
tcpSocket->write(message.toUtf8());
result = tcpSocket->waitForBytesWritten(5000);
} else {
emit error(tr("Client \"%1\" not found").arg(ip_to.toString()));
}
tcpSocket->abort();
delete tcpSocket;
}
return result;
}
示例10: defined
//----------------------------------------------------------------------------------
void tst_QTcpServer::ipv6Server()
{
#if defined(Q_OS_SYMBIAN)
QSKIP("Symbian: IPv6 is not yet supported", SkipAll);
#endif
//### need to enter the event loop for the server to get the connection ?? ( windows)
QTcpServer server;
if (!server.listen(QHostAddress::LocalHostIPv6, 8944)) {
QVERIFY(server.serverError() == QAbstractSocket::UnsupportedSocketOperationError);
return;
}
QVERIFY(server.serverPort() == 8944);
QVERIFY(server.serverAddress() == QHostAddress::LocalHostIPv6);
QTcpSocket client;
client.connectToHost("::1", 8944);
QVERIFY(client.waitForConnected(5000));
QVERIFY(server.waitForNewConnection());
QVERIFY(server.hasPendingConnections());
QTcpSocket *serverSocket = 0;
QVERIFY((serverSocket = server.nextPendingConnection()));
}
示例11: write
void server::write(QString value)
{
QTcpSocket *client = Server->nextPendingConnection();
if (client->waitForConnected(3000))
{
qDebug() << "Client Connected: ...";
client->write("The connection is sucessfull");
client->waitForBytesWritten(1000);
client->waitForReadyRead(3000);
qDebug() << "Reading" << client->bytesAvailable();
qDebug() << client->readAll();
client->close();
}
else
{
qDebug() << "Connection Failed...";
}
}
示例12: refreshGames
void NewGame::refreshGames()
{
this->ui->listRefresh->setEnabled(false);
this->ui->listGame->clear();
unsigned int ip;
unsigned int netmask = 0xffffff00;
foreach (QNetworkInterface it, QNetworkInterface::allInterfaces())
foreach (QHostAddress addr, it.allAddresses())
if (addr.toIPv4Address() && 0xff000000 != 0x7f000000)
ip = addr.toIPv4Address();
unsigned int networkaddr = ip & netmask;
for (unsigned int i = 1; i < 256; i++)
{
unsigned int currentIp = networkaddr + i;
QTcpSocket* s = new QTcpSocket();
s->connectToHost(QHostAddress(currentIp), 1338);
static_cast<MainWindow*>(this->parent())->print_status("Scanning ...\tCurrent : " + QHostAddress(currentIp).toString());
if (s->waitForConnected(5))
{
this->ui->listGame->addItem(new QListWidgetItem(QHostAddress(currentIp).toString()));
QCoreApplication::processEvents();
// tell the slave this is just a search request
s->write("s", 1);
s->close();
}
}
static_cast<MainWindow*>(this->parent())->print_status("End of scan");
this->ui->listRefresh->setEnabled(true);
}
示例13: sendBase
void MT500::testConnection()
{
for(int i = 0; i < 6; i++) {
if(!ipArray[i].ip.isEmpty()){
QString msg = "[" + QDateTime::currentDateTime().toString("MM/dd/yyyy hh:mm:ss") + "] ";
QTcpSocket * test = new QTcpSocket;
test->connectToHost(QHostAddress(ipArray[i].ip), ipArray[i].port);
if(test->waitForConnected(2000)) {
msg += ipArray[i].ip + ": Test Message Sent";
ui->testBrowser->append("<font color=green>" + msg + "</font>");
test->abort();
}
else {
msg += ipArray[i].ip + ": Failed to Connect";
ui->testBrowser->append("<font color=red>" + msg + "</font>");
}
}
}
if(!initial) {
QString fips = fipsNo.trimmed().remove("VAZ");
QString msgToSend = "A 100 " + QDateTime::currentDateTime().toString("MM/dd/yyyy hh:mm:ss") + " " + QString::number(boxGID);
sendBase(msgToSend, true);
sendRaw(msgToSend);
sendRawStrtoIflows(QString(encode(msgToSend)));
}
else initial = false;
}
示例14: getSensorTemperature
int MainWindow::getSensorTemperature(){
// Get the server address and port from the settings dialog box
int serverPort = this->dialog->getServerPort(); // get from the dialog box
quint32 serverAddr = this->dialog->getIPAddress(); // from the dialog box
QTcpSocket *tcpSocket = new QTcpSocket(this); // create socket
tcpSocket->connectToHost(QHostAddress(serverAddr), serverPort); // connect
if(!tcpSocket->waitForConnected(1000)){ //wait up to 1s for a connection
statusBar()->showMessage("Failed to connect to server...");
return 1;
}
// Send the message "getTemperature" to the server
tcpSocket->write("getTemperature");
if(!tcpSocket->waitForReadyRead(1000)){ // wait up to 1s for the server
statusBar()->showMessage("Server did not respond...");
return 1;
}
// If the server has sent bytes back to the client
if(tcpSocket->bytesAvailable()>0){
int size = tcpSocket->bytesAvailable(); // how many bytes are ready?
char data[20]; // upper limit of 20 chars
tcpSocket->read(&data[0],(qint64)size); // read the number of bytes rec.
data[size]='\0'; // termintate the string
this->curTemperature = atof(data); // string -> float conversion
cout << "Received the data [" << this->curTemperature << "]" << endl;
}
else{
statusBar()->showMessage("No data available...");
}
return 0; // the on_updateTemperature() slot will update the display
}
示例15: send
bool Http::send(QString host, int port, QByteArray data, QByteArray &result, int timeout)
{
QByteArray response;
QTcpSocket socket;
int numRead = 0;
int numReadTotal = 0;
char buffer[50];
socket.connectToHost(QHostAddress(host),port);
if (!socket.waitForConnected(timeout)) {
return false;
}
socket.write(data);
if (!socket.waitForBytesWritten(timeout))
{
return false;
}
forever {
numRead = socket.read(buffer, 50);
numReadTotal += numRead;
response.append(buffer,numRead);
if (numRead == 0 && !socket.waitForReadyRead(timeout))
break;
}
socket.disconnectFromHost();
int header = response.indexOf("\r\n\r\n");
if (header != -1) {
result = response.mid(header+4);
}
return true;
}