PDA

View Full Version : QProcess again ..



cmeliak
24th May 2006, 16:14
hey guys.
i'm already really confused ...:crying: :eek: :crying:
i have read very nice tutorial to client - server application.
http://www.codeskipper.com/article/3/
I can understand it well. Now I would like to implement on the server side following feature:

-i send a command from the client: for example ipconfig
-server side recieve this text, recognize it as some command, for example using if() and sends it to the console
-than the stdout output will be send to the client back


I have tried system("ipconfig") - but it still executes command window(under windows xp)
I have tried qprocess ( i guess that is the way) but may be I can just use this class, it doesn't work.

Could anybody help me please with this or give me some idea? May be I did not understood how to use streams or how to send it to the socket or I don't know. I'm really :confused:

thank you guys.

i use qt3

jacek
24th May 2006, 16:29
QProcess does work. See the "process" example that comes with Qt 3.

cmeliak
24th May 2006, 16:42
I have had a look on it. Process is started correctly, but the stdout data are not transfered to the string.


void ServerMainWindow::readFromStdout(){
QByteArray data = proc->readStdout();
lineEdit1->setText(QString(data));
sendToClients(QString(data));
}

jacek
24th May 2006, 16:51
Is that ServerMainWindow::readFromStdout() method being invoked at all?

cmeliak
24th May 2006, 16:54
Yes, I had a QMessage there

jacek
24th May 2006, 17:11
What is the size of that "data" byte array after readStdout()? What signal did you connect that ServerMainWindow::readFromStdout() slot to?

cmeliak
24th May 2006, 18:13
signal is coorect i think, here is a constructor for the main class:


ServerMainWindow::ServerMainWindow(QWidget* parent, const char* name)
: MainWindowBase(parent, name),
m_server(0)
{
m_clients.setAutoDelete(true);


proc = new QProcess(this);

proc->addArgument("ipconfig");


QObject::connect(proc,SIGNAL(readyReadStdout()),th is,SLOT(readFromStdout()));
QObject::connect(m_start, SIGNAL(clicked()), this, SLOT(slotStartClicked()));
QObject::connect(m_stop, SIGNAL(clicked()), this, SLOT(slotStopClicked()));
}




size of the byte array ... i think i cant understand... if you are asking regarding how much data will flow throuh ... just write ipconfig in you console :)

actually i could send you files if you like ... i have no problem with

cmeliak
24th May 2006, 18:20
may be question: when should I execute proc->start()

-is my proc->readStdout() still ready to read stdout?

im :confused:

jacek
24th May 2006, 18:29
size of the byte array ... i think i cant understand...
I wanted you to check if you actually read something with readStdout():

void ServerMainWindow::readFromStdout(){
QByteArray data = proc->readStdout();
qWarning( "%d", data.size() );
lineEdit1->setText(QString(data));
sendToClients(QString(data));
}


when should I execute proc->start()
After you connect to readyReadStdout() signal.

cmeliak
24th May 2006, 18:52
oou ... i always started process before i connected it to the signal ...

now i am able to see output in my server application window

jacek i still can't see output from qWarning ... should be in a cmd window? i am sure it is executed, because it runs line with lineEdit1..


void ServerMainWindow::readFromStdout(){
QByteArray data = proc->readStdout();
qWarning( "%d", data.size() );
lineEdit1->setText(QString(data));
sendToClients(QString(data));
}


and there is maybe issue with sendToClients ...


void ServerMainWindow::sendToClients(const QString& text)
{
if (text.isNull()) return;


// iterate over all clients and send them the text
QPtrDictIterator<Client> iter(m_clients);
for (; iter.current() != 0; ++iter)
{
QSocket* sock = iter.current()->socket();
QTextStream stream(sock);
stream << text;
}
}

i call this function from ServerMainWindow::readFromStdout(), is it correct overloaded?

cmeliak
24th May 2006, 18:54
bardzo diekuju za twoja pomoc :) ;) :D

cmeliak
24th May 2006, 19:53
it works fine now:


ServerMainWindow::ServerMainWindow(QWidget* parent, const char* name)
: MainWindowBase(parent, name),
m_server(0)
{
m_clients.setAutoDelete(true);


proc = new QProcess(this);

proc->addArgument("ipconfig");
proc->addArgument("-all");


QObject::connect(proc,SIGNAL(readyReadStdout()),th is,SLOT(readFromStdout()));
QObject::connect(m_start, SIGNAL(clicked()), this, SLOT(slotStartClicked()));
QObject::connect(m_stop, SIGNAL(clicked()), this, SLOT(slotStopClicked()));
proc->start();
}

////////////////////////////////////////////////////////////////////////////////

void ServerMainWindow::readFromStdout(){
// QByteArray data = proc->readStdout();
QString data = proc->readStdout();
textEdit1->setText(data);
sendToClients(data);
}
////////////////////////////////////////////////////////////////////////////////

ServerMainWindow::~ServerMainWindow()
{
slotStopClicked();
}

////////////////////////////////////////////////////////////////////////////////

void ServerMainWindow::sendToClients(const QString& text)
{
if (text.isNull()) return;


// iterate over all clients and send them the text
QPtrDictIterator<Client> iter(m_clients);
for (; iter.current() != 0; ++iter)
{
QSocket* sock = iter.current()->socket();
QTextStream stream(sock);
stream << text;
}
}


but still my client doesn't recive stdout from ipconfig
function sendToClients(data); should send QString to void ServerMainWindow::sendToClients(const QString& text)

is the function sendToClients(const QString& text) correct? or could be there some issue in the client application?

client function that recieves data from server app is:

void ClientMainWindow::slotRead()
{
QString text;
while (m_socket->canReadLine())
text += m_socket->readLine();

if (!text.isNull())
appendText(text, Output);
}


where



void ClientMainWindow::appendText(const QString& text, Mode mode)
{
switch (mode)
{
case System:
m_textEdit->setColor(Qt::blue);
break;

case Error:
m_textEdit->setColor(Qt::red);
break;

default:
m_textEdit->setColor(Qt::black);
break;
}

m_textEdit->append(text);
}

cmeliak
24th May 2006, 20:02
OK I found it already :D
Thank you jacek 4 your time ;)

jacek
24th May 2006, 20:56
i still can't see output from qWarning ... should be in a cmd window?
If you use windows, you should add "CONFIG += console" to the .pro file.


bardzo diekuju za twoja pomoc
Nie ma za co ;)