本文整理汇总了C++中QLocalSocket::read方法的典型用法代码示例。如果您正苦于以下问题:C++ QLocalSocket::read方法的具体用法?C++ QLocalSocket::read怎么用?C++ QLocalSocket::read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QLocalSocket
的用法示例。
在下文中一共展示了QLocalSocket::read方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getRunningPid
qint64 QtLocalPeer::getRunningPid() {
if (!isClient())
return 0;
QLocalSocket socket;
bool connOk = false;
for (int i = 0; i < 2; i++) {
// Try twice, in case the other instance is just starting up
socket.connectToServer(socketName);
connOk = socket.waitForConnected(5000/2);
if (connOk || i)
break;
Sleep(250);
}
if (!connOk) return -1;
const char* msg = "qbt://pid";
QDataStream ds(&socket);
ds.writeBytes(msg, qstrlen(msg));
bool res = socket.waitForBytesWritten(5000) && socket.waitForReadyRead(5000);
if (!res) return -1;
DWORD pid;
qint64 pid_size = sizeof pid;
while (socket.bytesAvailable() < pid_size)
socket.waitForReadyRead();
if (socket.read((char *)&pid, pid_size) < pid_size)
return -1;
return pid;
}
示例2: 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;
}
示例3: sendMessage
bool QtLocalPeer::sendMessage(const QString &message, int timeout)
{
if (!isClient())
return false;
QLocalSocket socket;
bool connOk = false;
for (int i = 0; i < 2; i++) {
// Try twice, in case the other instance is just starting up
socket.connectToServer(socketName);
connOk = socket.waitForConnected(timeout/2);
if (connOk || i)
break;
int ms = 250;
#if defined(Q_OS_WIN)
Sleep(DWORD(ms));
#else
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
nanosleep(&ts, NULL);
#endif
}
if (!connOk)
return false;
QByteArray uMsg(message.toUtf8());
QDataStream ds(&socket);
ds.writeBytes(uMsg.constData(), uMsg.size());
bool res = socket.waitForBytesWritten(timeout);
res &= socket.waitForReadyRead(timeout); // wait for ack
res &= (socket.read(qstrlen(ack)) == ack);
return res;
}
示例4: 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;
}
示例5: qt_waitforinput
int qt_waitforinput(void)
{
#ifdef USE_MOUSE
fd_set read_fds;
int stdin_fd = fileno(stdin);
int socket_fd = qt_socket.socketDescriptor();
if (!qt_initialized || (socket_fd < 0) || (qt_socket.state() != QLocalSocket::ConnectedState))
return getchar();
// Gnuplot event loop
do
{
// Watch file descriptors
FD_ZERO(&read_fds);
FD_SET(socket_fd, &read_fds);
if (!paused_for_mouse)
FD_SET(stdin_fd, &read_fds);
// Wait for input
if (select(socket_fd+1, &read_fds, NULL, NULL, NULL) < 0)
{
fprintf(stderr, "Qt terminal communication error: select() error\n");
break;
}
// Terminal event coming
if (FD_ISSET(socket_fd, &read_fds))
{
qt_socket.waitForReadyRead(-1);
while (qt_socket.bytesAvailable() >= sizeof(gp_event_t))
{
struct gp_event_t event;
qt_socket.read((char*) &event, sizeof(gp_event_t));
/// @todo don't process mouse move events if others are in the queue
if (qt_processTermEvent(&event))
return '\0'; // exit from paused_for_mouse
}
}
} while (paused_for_mouse || (!paused_for_mouse && !FD_ISSET(stdin_fd, &read_fds)));
#endif
return getchar();
}
示例6: sendRawMessage
QString ControlPeer::sendRawMessage(const QString & msg, int timeout)
{
if (mode() != ModeClient) {
p->error = NotInClientModeError;
return QString();
}
QString controlSocketPath = Guzum::Config::controlSocketPath();
QLocalSocket socket;
bool res = false;
socket.connectToServer(controlSocketPath);
res = socket.waitForConnected(timeout);
if (!res) {
p->error = ReadFailedError;
return QString();
}
QByteArray bytes(msg.toUtf8());
QByteArray responseBytes;
QDataStream ds(&socket);
ds.writeBytes(bytes.constData(), bytes.size());
res = socket.waitForBytesWritten(timeout);
if (res) {
res &= socket.waitForReadyRead(timeout); // wait for ack
if (res) {
responseBytes = socket.read(qstrlen(ACK));
res &= (responseBytes == ACK);
}
}
if (!res) {
p->error = ReadFailedError;
return QString();
}
p->error = NoError;
return QString::fromUtf8(responseBytes.constData());
}
示例7: callback_write_file
size_t callback_write_file(void *ptr, size_t size, size_t nmemb, void *userp)
{
// qDebug()<<__FILE__<<__LINE__<<__FUNCTION__<<size<<nmemb<<userp;
int tlen = size * nmemb;
char *s = (char*)ptr;
int rlen = 0;
CurlFtp *ftp = static_cast<CurlFtp*>(userp);
QLocalSocket *router = ftp->getDataSock2();
QLocalSocket *suppler = ftp->getDataSock();
// qDebug()<<"here";
Q_ASSERT(router != NULL);
// qDebug()<<"here"<<router;
retry_read:
if (!ftp->isPutDataFinished() || router->bytesAvailable() > 0) {
if (router->bytesAvailable() > 0) {
} else {
router->waitForReadyRead(1000);
router = ftp->getDataSock2();
// qDebug()<<"wait for ready read..."<<router->errorString()
// <<router->isOpen();
if (router->bytesAvailable() == 0) {
suppler = ftp->getDataSock();
if (suppler == NULL) {
// suppler is finished.
// ftp->closeDataChannel2();
return 0;
}
// continue;
goto retry_read;
}
}
Q_ASSERT(router->bytesAvailable() > 0);
rlen = router->read(s, tlen);
if (rlen < 0) {
Q_ASSERT(rlen >= 0);
return 0;
} else if (rlen == 0) {
qDebug()<<"no data left";
return 0;
} else {
return rlen;
}
} else {
return 0;
}
return 0;
// for (;;) {
// router = ftp->getDataSock2();
// if (router->bytesAvailable() > 0) {
// } else {
// router->waitForReadyRead(1000);
// router = ftp->getDataSock2();
// // qDebug()<<"wait for ready read..."<<router->errorString()
// // <<router->isOpen();
// if (router->bytesAvailable() == 0) {
// suppler = ftp->getDataSock();
// if (suppler == NULL) {
// // suppler is finished.
// // ftp->closeDataChannel2();
// return 0;
// }
// continue;
// }
// }
// Q_ASSERT(router->bytesAvailable() > 0);
// rlen = router->read(s, tlen);
// if (rlen < 0) {
// Q_ASSERT(rlen >= 0);
// return 0;
// } else if (rlen == 0) {
// qDebug()<<"no data left";
// return 0;
// } else {
// return rlen;
// }
// }
// rlen = router->read(s, tlen);
// if (rlen < 0) {
// Q_ASSERT(rlen >= 0);
// return 0;
// } else if (rlen == 0) {
// return 0;
// } else {
// return rlen;
// }
if (gn == 0) {
gn ++;
} else {
return 0;
}
for (int i = 0 ; i < tlen ; i ++) {
//.........这里部分代码省略.........
示例8: qt_waitforinput
int qt_waitforinput(void)
{
#ifdef USE_MOUSE
fd_set read_fds;
int stdin_fd = fileno(stdin);
int socket_fd = qt_socket.socketDescriptor();
if (!qt_initialized || (socket_fd < 0) || (qt_socket.state() != QLocalSocket::ConnectedState))
return getchar();
// Gnuplot event loop
do
{
// Watch file descriptors
FD_ZERO(&read_fds);
FD_SET(socket_fd, &read_fds);
if (!paused_for_mouse)
FD_SET(stdin_fd, &read_fds);
// Wait for input
if (select(socket_fd+1, &read_fds, NULL, NULL, NULL) < 0)
{
// Display the error message except when Ctrl + C is pressed
if (errno != 4)
fprintf(stderr, "Qt terminal communication error: select() error %i %s\n", errno, strerror(errno));
break;
}
// Terminal event coming
if (FD_ISSET(socket_fd, &read_fds))
{
if (!(qt_socket.waitForReadyRead(-1))) {
// Must be a socket error; we need to restart qt_gnuplot
qDebug() << "Error: gnuplot_qt socket not responding";
qt_gnuplot_qtStarted = false;
return '\0';
}
// Temporary event for mouse move events. If several consecutive
// move events are received, only transmit the last one.
gp_event_t tempEvent;
tempEvent.type = -1;
if (qt_socket.bytesAvailable() < sizeof(gp_event_t)) {
qDebug() << "Error: short read from gnuplot_qt socket";
return '\0';
}
while (qt_socket.bytesAvailable() >= sizeof(gp_event_t))
{
struct gp_event_t event;
qt_socket.read((char*) &event, sizeof(gp_event_t));
// Delay move events
if (event.type == GE_motion)
tempEvent = event;
// Other events. Replay the last move event if present
else
{
if (tempEvent.type == GE_motion)
{
qt_processTermEvent(&tempEvent);
tempEvent.type = -1;
}
if (qt_processTermEvent(&event))
return '\0'; // exit from paused_for_mouse
}
}
// Replay move event
if (tempEvent.type == GE_motion)
qt_processTermEvent(&tempEvent);
}
} while (paused_for_mouse || (!paused_for_mouse && !FD_ISSET(stdin_fd, &read_fds)));
#endif
return getchar();
}