本文整理汇总了C++中QLocalSocket::write方法的典型用法代码示例。如果您正苦于以下问题:C++ QLocalSocket::write方法的具体用法?C++ QLocalSocket::write怎么用?C++ QLocalSocket::write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QLocalSocket
的用法示例。
在下文中一共展示了QLocalSocket::write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: QLocalSocket
bool N810GpsPlugin::sendToGpsDriverCtrl(QString cmd)
{
QLocalSocket *gpsCtrlSocket;
gpsCtrlSocket = new QLocalSocket(this);
QByteArray socketPath = ( "/var/lib/gps/gps_driver_ctrl");
gpsCtrlSocket->connectToServer(socketPath.data());
if(gpsCtrlSocket->waitForConnected()) {
qLog(Hardware)<< __PRETTY_FUNCTION__ << "connected" << cmd;
QByteArray data;
data.append(cmd);
data.append("\n");
gpsCtrlSocket->write(data);
gpsCtrlSocket->flush();
gpsCtrlSocket->disconnectFromServer();
return true;
} else {
qLog(Hardware) << __PRETTY_FUNCTION__ << "Could not connect to socket" << gpsCtrlSocket;
}
return false;
}
示例2: getKeyValue
int QmKeysPrivate::getKeyValue(const struct input_event &query) {
// Try to connect to qmkeyd.
QLocalSocket socket;
socket.connectToServer(SERVER_NAME);
if (!socket.waitForConnected(1000)) {
return -1;
}
// Query for the given key
if (socket.write((char*)&query, sizeof(query)) != sizeof(query)) {
return -1;
}
if (!socket.waitForReadyRead(1000)) {
return -1;
}
struct input_event response;
int ret = 0;
// A loop because we might receive other events as well.
do {
ret = socket.read((char*)&response, sizeof(response));
if (ret == sizeof(response)) {
if (response.type == query.type && response.code == query.code) {
break;
}
}
} while (ret == sizeof(response));
socket.disconnect();
return response.value;
}
示例3: hasPrevious
bool SingleInstance::hasPrevious(const QString &name, const QStringList &args)
{
qDebug() << "Checking for previous instance...";
QLocalSocket socket;
socket.connectToServer(name, QLocalSocket::ReadWrite);
if (socket.waitForConnected())
{
qDebug() << "Connection found!";
qDebug() << "Forwarding argument to existing instance...";
QByteArray buffer;
for (auto item : args)
{
buffer.append(item+"\n");
}
qDebug() << "Forwading buffer=" << buffer;
socket.write(buffer);
return true;
}
qDebug() << socket.errorString();
qDebug() << "No connection found";
return false;
}
示例4: QLocalSocket
RKGraphicsDeviceBackendTransmitter* RKGraphicsDeviceBackendTransmitter::instance () {
if (_instance) return _instance;
RK_TRACE (GRAPHICS_DEVICE);
QLocalSocket *con = new QLocalSocket ();
con->connectToServer (RKRBackendProtocolBackend::rkdServerName ());
con->waitForConnected (2000);
if (con->state () == QLocalSocket::ConnectedState) {
con->write (RKRBackendTransmitter::instance ()->connectionToken ().toLocal8Bit ().data ());
con->write ("\n");
con->waitForBytesWritten (1000);
_instance = new RKGraphicsDeviceBackendTransmitter (con, true);
return _instance;
}
return 0;
}
示例5: main
int main(void)
{
QLocalSocket socket;
socket.connectToServer(SOCK_PATH);
socket.open(QIODevice::ReadWrite);
printf("Connected.\n");
char str[100];
while(printf("> "), fgets(str, 100, stdin), !feof(stdin)) {
if (socket.write(str, strlen(str)) == -1) {
perror("send");
return EXIT_FAILURE;
}
int t;
if ((t = socket.read(str, 100)) > 0) {
str[t] = '\0';
printf("echo> %s", str);
} else {
if (t < 0)
perror("recv");
else
printf("Server closed connection.\n");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
示例6: sendMessages
bool KNSingletonApplication::sendMessages(const QString &uniqueKey,
const QStringList &messages)
{
//Create sender client.
QLocalSocket client;
//Link to the server which is listening to the unique key.
client.connectToServer(uniqueKey, QIODevice::WriteOnly);
//If connecting failed, return false.
if(!client.waitForConnected(TimeoutLimit))
{
qDebug("Cannot connect to the local server.");
//Disconnect from the server.
client.disconnectFromServer();
return false;
}
//Generate the message data.
QByteArray messageData;
QDataStream dataWriter(&messageData, QIODevice::WriteOnly);
dataWriter << messages;
//Send the data to local server.
client.write(messageData);
//Check sending status.
if(!client.waitForBytesWritten(TimeoutLimit))
{
qDebug("Send arguments failed.");
client.disconnectFromServer();
return false;
}
//Send the arguments success.
client.disconnectFromServer();
return true;
}
示例7: sendData
void sendData(QLocalSocket& sock, int num) {
qDebug("send %d", num);
QByteArray block;
QDataStream ds(&block, QIODevice::WriteOnly);
ds << num;
sock.write(block);
}
示例8: saveCommand
// Process incoming IPC command. First check if monero-wallet-gui is
// already running. If it is, send it to that instance instead, if not,
// queue the command for later use inside our QML engine. Returns true
// when queued, false if sent to another instance, at which point we can
// kill the current process.
bool IPC::saveCommand(QString cmdString){
qDebug() << QString("saveCommand called: %1").arg(cmdString);
QLocalSocket ls;
QByteArray buffer;
buffer = buffer.append(cmdString);
QString socketFilePath = this->socketFile().filePath();
ls.connectToServer(socketFilePath, QIODevice::WriteOnly);
if(ls.waitForConnected(1000)){
ls.write(buffer);
if (!ls.waitForBytesWritten(1000)){
qDebug() << QString("Could not send command \"%1\" over IPC %2: \"%3\"").arg(cmdString, socketFilePath, ls.errorString());
return false;
}
qDebug() << QString("Sent command \"%1\" over IPC \"%2\"").arg(cmdString, socketFilePath);
return false;
}
if(ls.isOpen())
ls.disconnectFromServer();
// Queue for later
this->SetQueuedCmd(cmdString);
return true;
}
示例9: receiveConnection
void QtLocalPeer::receiveConnection()
{
QLocalSocket* socket = server->nextPendingConnection();
if (!socket)
return;
while (socket->bytesAvailable() < (int)sizeof(quint32))
socket->waitForReadyRead();
QDataStream ds(socket);
QByteArray uMsg;
quint32 remaining;
ds >> remaining;
uMsg.resize(remaining);
int got = 0;
char* uMsgBuf = uMsg.data();
do {
got = ds.readRawData(uMsgBuf, remaining);
remaining -= got;
uMsgBuf += got;
} while (remaining && got >= 0 && socket->waitForReadyRead(2000));
if (got < 0) {
qWarning("QtLocalPeer: Message reception failed %s", socket->errorString().toLatin1().constData());
delete socket;
return;
}
QString message(QString::fromUtf8(uMsg));
socket->write(ack, qstrlen(ack));
socket->waitForBytesWritten(1000);
delete socket;
emit messageReceived(message); //### (might take a long time to return)
}
示例10: ipcSendCommandLine
//
// Sending to the server is done synchronously, at startup.
// If the server isn't already running, startup continues,
// and the items in savedPaymentRequest will be handled
// when uiReady() is called.
//
bool PaymentServer::ipcSendCommandLine()
{
bool fResult = false;
for (const QString& r : savedPaymentRequests)
{
QLocalSocket* socket = new QLocalSocket();
socket->connectToServer(ipcServerName(), QIODevice::WriteOnly);
if (!socket->waitForConnected(BITCOIN_IPC_CONNECT_TIMEOUT))
{
delete socket;
socket = nullptr;
return false;
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << r;
out.device()->seek(0);
socket->write(block);
socket->flush();
socket->waitForBytesWritten(BITCOIN_IPC_CONNECT_TIMEOUT);
socket->disconnectFromServer();
delete socket;
socket = nullptr;
fResult = true;
}
return fResult;
}
示例11: pqdbg_send_message
void pqdbg_send_message(int lvl, const QString &msg, const QString &title)
{
#ifdef PQDEBUG
static QMutex mutex;
mutex.lock();
QLocalSocket *debugSocket = PHPQt5::debugSocket();
if(debugSocket->isOpen()) {
/*
QByteArray data;
QDataStream out(&data, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << reinterpret_cast<quint64>(PQEngine::pqeEngine);
out << PQEngine::pqeCoreName;
out << lvl;
out << title;
out << msg;
out.device()->reset();
// = QString("%1:::%2:::%3:::%4:::%5:::%")
// .arg(reinterpret_cast<quint64>(PQEngine::pqeEngine))
// .arg(PQEngine::pqeCoreName)
// .arg(lvl)
// .arg(title)
// .arg(msg).toUtf8();
debugSocket->write(data);
debugSocket->waitForBytesWritten();
*/
QString str = QString("%1:::%2:::%3:::%4:::%5:::%6:::%")
.arg(reinterpret_cast<quint64>(PQEngine::pqeEngine))
.arg(PQEngine::pqeCoreName)
.arg(reinterpret_cast<quint64>(QThread::currentThread()))
.arg(lvl)
.arg(title)
.arg(msg).toUtf8();
/*
QByteArray block;
QDataStream sendStream(&block, QIODevice::ReadWrite);
sendStream << quint16(0) << str;
sendStream.device()->seek(0);
sendStream << (quint16)(block.size() - sizeof(quint16));
*/
debugSocket->write(str.toUtf8());
debugSocket->waitForBytesWritten();
}
mutex.unlock();
#else
Q_UNUSED(lvl);
Q_UNUSED(msg);
Q_UNUSED(title);
#endif
}
示例12: receiveConnection
void ControlPeer::receiveConnection()
{
QLocalSocket* socket = p->server->nextPendingConnection();
if (!socket) {
return;
}
while (socket->bytesAvailable() < (int)sizeof(quint32)) {
socket->waitForReadyRead();
}
QDataStream ds(socket);
QByteArray uMsg;
quint32 remaining;
ds >> remaining;
uMsg.resize(remaining);
int got = 0;
char* uMsgBuf = uMsg.data();
do {
got = ds.readRawData(uMsgBuf, remaining);
remaining -= got;
uMsgBuf += got;
} while (remaining && got >= 0 && socket->waitForReadyRead(2000));
if (got < 0) {
qWarning("Guzum.ControlPeer: Message reception failed %s", socket->errorString().toLatin1().constData());
delete socket;
return;
}
QString message(QString::fromUtf8(uMsg));
socket->write(ACK, qstrlen(ACK));
socket->waitForBytesWritten(1000);
delete socket;
// split message into the tokens, the format is the following:
// <method_name>\n<arg0>\n<arg1> etc
QStringList tokens = message.split("\n");
QString methodName = tokens[0];
if (methodName == SHOW_DIALOG_METHOD) {
showFileSelectorDialog();
} else if (methodName == OPEN_FILE_METHOD) {
if (tokens.size() == 2) {
// just open file using default gnupg home
QString filename = tokens[1];
editFile(filename);
} else if (tokens.size() == 3) {
// use file and custom gnupg home
QString filename = tokens[1];
QString gnupgHome = tokens[2];
editFile(filename, gnupgHome);
}
QString filename = message.mid(qstrlen(OPEN_FILE_METHOD)+1);
}
}
示例13: receiveConnection
void QtLocalPeer::receiveConnection()
{
QLocalSocket* socket = server->nextPendingConnection();
if (!socket)
return;
while (socket->bytesAvailable() < (int)sizeof(quint32))
socket->waitForReadyRead();
QDataStream ds(socket);
QByteArray uMsg;
quint32 remaining;
ds >> remaining;
uMsg.resize(remaining);
int got = 0;
char* uMsgBuf = uMsg.data();
do {
got = ds.readRawData(uMsgBuf, remaining);
remaining -= got;
uMsgBuf += got;
} while (remaining && got >= 0 && socket->waitForReadyRead(2000));
if (got < 0) {
qWarning() << "QtLocalPeer: Message reception failed" << socket->errorString();
delete socket;
return;
}
QString message(QString::fromUtf8(uMsg));
#ifdef Q_OS_WIN
if (message == "qbt://pid") {
qint64 pid = GetCurrentProcessId();
socket->write((const char *)&pid, sizeof pid);
} else {
socket->write(ack, qstrlen(ack));
}
#else
socket->write(ack, qstrlen(ack));
#endif
socket->waitForBytesWritten(1000);
delete socket;
#ifdef Q_OS_WIN
if (message == "qbt://pid")
return;
#endif
emit messageReceived(message); //### (might take a long time to return)
}
示例14: serverSendMessage
bool QtLocalPeer::serverSendMessage(const QString &message, int client_id, int timeout)
{
qDebug()<<__FILE__<<__LINE__<<__FUNCTION__<<client_id;
QLocalSocket *socket = NULL;
QHash<QLocalSocket*, int>::iterator it;
if (client_id == -1) {
// broadcast
for (it = this->clients.begin(); it != this->clients.end(); it++) {
socket = it.key();
// QByteArray uMsg(message.toUtf8());
// QDataStream ds(socket);
// ds.writeBytes(uMsg.constData(), uMsg.size());
socket->write(message.toAscii());
bool res = socket->waitForBytesWritten(timeout);
}
} else {
for (it = this->clients.begin(); it != this->clients.end(); it++) {
if (it.value() == client_id) {
socket = it.key();
}
}
if (socket) {
// qDebug()<<__FILE__<<__LINE__<<__FUNCTION__<<client_id;
// QByteArray uMsg(message.toUtf8());
// QDataStream ds(socket);
// ds.writeBytes(uMsg.constData(), uMsg.size());
socket->write(message.toAscii());
bool res = socket->waitForBytesWritten(timeout);
} else {
qDebug()<<"can not find socket for client:"<<client_id;
}
}
return true;
}
示例15: sendData
static bool sendData(const QByteArray &out, QByteArray *in)
{
#if USE_LOCAL_SOCKETS
QLocalSocket socket;
socket.connectToServer("gdrdeamon");
#else
QTcpSocket socket;
socket.connectToHost("127.0.0.1", 15001);
#endif
if (!socket.waitForConnected(1000))
return false;
qint32 size = out.size();
socket.write((char*)&size, sizeof(qint32));
socket.write(out);
while (socket.bytesToWrite() && socket.waitForBytesWritten())
;
while (socket.waitForReadyRead())
in->append(socket.readAll());
return true;
}