Bidirectional communication between qprocess processes

Source: Internet
Author: User

Remember to write a Linux C program, which uses Popen to open a sub-process, so that you can use Read/write and sub-process communication, and in the child process through the stdin read and write to the stdout to achieve the parent process communication. The underlying implementation of qprocess is similar in concept. The Qprocess class provides APIs that allow the parent process to easily read the data stdout by the child process, or to write data to the stdin of the child process easily. But there are all sorts of puzzling mysteries that need to be memo.

Test case

Two small programs, the parent process program name is server. It defines a qprocess that connects the qprocess Readyread signal to a slot function to receive the information on the client side stdout. Call Qprocess::write to send the text in the edit box when the Send to Client button is pressed.

The child process, named client, defines a qfile open stdin, which connects Readyread signals to the data that is written by the server side. Writes data to stdout when "Send message" is pressed.

Do you think this test case will work as we expected?

Problem analysis

This program is very simple in terms of structure, if the code does not write in a way that does not seem to be a problem.  The actual operation of the results is quite surprising, from the client side to the server to send data is normal, but in the client will never receive Readyread signal! Two-way communication becomes a one-way, which is really depressing. A closer look at Qfile's documentation includes a description of the Qiodevice, to see where the problem lies. Forced to turn to experts to pull, according to expert opinion, Qfile is not support readyread signal. This is a better understanding. But there seems to be no good QT API to solve this problem.

QT API design does not include the most low-level equipment processing class, this problem often see someone complain, such as serial communication and other functions will not be directly QT class available. However, I think this is not a problem, like this special low-level API super simple, open->ioctl->read/write->close on the finish, even take QT package a layer will not be more simple, why superfluous it, If any of the functions depend on QT Implementation, what do we programmers do?!

Here you need to use Qfile to manipulate stdin code to modify the direct open/read/write/close way. If you want to keep the original "action" is not difficult, this part can be used in QT Qsocketnotifier class to monitor the read and write events of FD, when the signal is received only to call Read/write.

Main code

Server-side-open process
if (! Pro)
{
Pro = new Qprocess (this);
Connect (Pro, SIGNAL (finished (int,qprocess::exitstatus)), this, SLOT (processfinished (int, qprocess::exitstatus)));
Connect (Pro, SIGNAL (Error (qprocess::P rocesserror)), this, SLOT (Processerror (qprocess::P rocesserror)));
Connect (Pro, SIGNAL (Readyread ()), this, SLOT (Readfromclient ()));
Pro->start ("./client");
}

Server-side Receive data
void Mainwin::readfromclient ()
{
if (!pro) return;
Qbytearray output = Pro->readallstandardoutput ();
Qwarning ()}

Server-Side Send data
void Mainwin::writetoclient ()
{
if (!pro) return;
Pro->write (Le->text (). ToLatin1 (). Constdata (), Le->text (). Length ());
Qwarning () text ();
}

Client Side monitoring stdin read and write messages
Filein.open (stdin, qiodevice::readonly);
qsocketnotifier* sn = new Qsocketnotifier (Filein.handle (), qsocketnotifier::read, this);
Connect (SN, SIGNAL (activated (int)), this, SLOT (readfromserver (int)));

Client side receive data
void mainwin::readfromserver (int fd)
{
if (FD! = Filein.handle ())
Return

Char buffer[256];
int count = Read (Filein.handle (), buffer, 256);
Le->settext ("from SERVER:" + QString (buffer). Left (count));
}

Client-side Send data
void Mainwin::writetoserver ()
{
QFile fileout;
Fileout.open (stdout, qiodevice::writeonly);
Fileout.write (Le->text (). ToLatin1 (). Constdata (), Le->text (). Length ()); Write to StdErr
Qwarning () text ();
Fileout.close ();
}

Reprint to: http://blog.chinaunix.net/uid-13830775-id-97752.html

Bidirectional communication between qprocess processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.